Symfony 4动态数据库连接和FOS用户

时间:2018-12-17 16:08:56

标签: php fosuserbundle symfony4 symfony-security

使用 Symfony 4 ,我们正在构建一个多客户应用程序,其中每个客户都有一个专用的数据库。创建新客户时,将自动按需创建客户数据库。在此过程中,我们将创建唯一的额外.env文件(特定于客户),该文件包含数据库连接凭据。

我们有2个数据库连接和2个实体管理器-commoncustomercustomer实体管理器实际上是默认的。显然,customer实体管理器起初没有正确的连接参数。 我们根据内核请求相应地(例如基于域)加载额外的.env文件,并相应地设置customer连接和实体管理器:

$connection = $this->entityManager->getConnection();
$connection->close();

$reflectionConn   = new \ReflectionObject($connection);
$reflectionParams = $reflectionConn->getProperty('params');
$reflectionParams->setAccessible(true);

$params             = $reflectionParams->getValue($connection);
$params['dbname']   = $database; // read from the customer specific .env
$params['user']     = $username; // read from the customer specific .env
$params['password'] = $password; // read from the customer specific .env

$reflectionParams->setValue($connection, $params);
$reflectionParams->setAccessible(false);

$this->entityManager = $this->entityManager->create(
    $this->entityManager->getConnection(),
    $this->entityManager->getConfiguration()
);

这很好!

问题是安全上下文(?)-我们正在使用FosUserBundle,它无法授权用户。我猜上面的代码不足以影响安全上下文。

使用标准FosUserBundle实现动态数据库连接+用户授权的目标的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

该问题根本与安全上下文无关。问题(很傻)是我的内核请求侦听器具有默认优先级(0),这使登录侦听器发生后整个客户配置加载和连接设置得以实现。

将其设置为512(应该足够)以在SessionListener之前启动

相关问题