3

我在我的User数据设备中使用 LDAP,我不想硬编码 LDAP 登录选项。最初,我尝试了这个:

$options = array(
            'host' => '%ldap_host%',
            'port' => '%ldap_port%',
            'useSsl' => true,
            'username' => '%ldap_username%',
            'password' => '%ldap_password%',
            'baseDn' => '%ldap_baseDn_users%'
        ); 

但这没有用。我做了一些研究并意识到我需要将容器包含在我的固定装置中。然而,就在这一点上,我不确定我的下一步是什么。

据我了解,我需要使用容器及其get方法来获取包含参数的服务,但我不知道那是什么:

$this->container->get('parameters');

不起作用,所以我想知道我应该使用什么。

我的完整数据夹具如下:

class LoadFOSUsers extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
{
    /**
     * @var ContainerInterface
     */
    private $container;

    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
    }


    public function load(ObjectManager $manager)
    {
        $this->container->get('parameters');

        // Not sure how to access param values. 
        $options = array(
            'host' => '%ldap_host%',
            'port' => '%ldap_port%',
            'useSsl' => true,
            'username' => '%ldap_username%',
            'password' => '%ldap_password%',
            'baseDn' => '%ldap_baseDn_users%'
        );

        $ldap = new Ldap($options);
        $ldap->bind();

        $baseDn = '%ldap_baseDn_users%';
        $filter = '(&(&(ObjectClass=user))(samaccountname=*))';
        $attributes=['samaccountname', 'dn', 'mail','memberof'];
        $result = $ldap->searchEntries($filter, $baseDn, Ldap::SEARCH_SCOPE_SUB, $attributes);

        foreach ($result as $item) {
            echo $item["dn"] . ': ' . $item['samaccountname'][0] . PHP_EOL;
        }
    }

    public function getOrder()
    {
        // the order in which fixtures will be loaded
        // the lower the number, the sooner that this fixture is loaded
        return 8;
    }
}
4

1 回答 1

4

您只需通过从容器中取出它们getParameter('name')或通过将它们全部放入袋子中getParameterBag()

所以:

    $options = array(
        'host' => $this->container->getParameter('ldap_host'),
        'port' => $this->container->getParameter('ldap_port'),
        'useSsl' => true,
        'username' => $this->container->getParameter('ldap_username'),
        'password' => $this->container->getParameter('ldap_password'),
        'baseDn' => $this->container->getParameter('ldap_baseDn_users')
    );

等等

于 2017-03-30T03:43:35.970 回答