Symfony 3 中带有 Guard 身份验证系统的 LDAP

LDAP with Guard Authentication System in Symfony 3

我假装要做的是将内部用户的 LDAP 包含在由 ddbb 配置的 Guard 身份验证系统中。 由于 https://knpuniversity.com/screencast/symfony-security.

,我已经构建了我的 Guard 身份验证系统并且工作得非常好

但我还需要尝试以前通过 LDAP 模式登录。更准确地说,功能必须是这样的:

用户尝试登录配置了数据库的 Guard 系统身份验证来自 MySQL 和:

1- 检查 MySQL 的 table 用户中是否存在该用户。如果存在,我们转到第 2 步。如果不存在 return false 以错误消息进行身份验证。

2-检查用户是否存在于LDAP模式。存在则进行第3步,不存在则进行第4步

3-尝试使用用户名和密码通过 LDAP 登录。如果身份验证正常,则登录。如果无法通过 LDAP 匹配密码,return false 到身份验证并显示错误消息。

4-选中 LDAP 选项后,我们将尝试通过 Guard 身份验证系统登录。如果身份验证没问题,则用户已登录。如果无法通过 Guard 将密码与 MySQL 用户 table、return false 匹配并显示错误消息。

在 LoginFormAuthenticator 文件中,我终于可以管理我想要的行为,如下一个代码所示。

<?php

namespace AppBundle\Security;

use ...
use Zend\Ldap\Ldap;
use Zend\Ldap\Exception\LdapException;

class LoginFormAuthenticator extends AbstractFormLoginAuthenticator
{
    use TargetPathTrait;

    private $em;
    private $router;
    private $passwordEncoder;
    private $csrfTokenManager;

    public function __construct(...
    }

    public function getCredentials(Request $request)
    {
    ...
    }

    public function getUser($credentials, UserProviderInterface $userProvider)
    {
        $username = $credentials['username'];
        $ldapPassword = $credentials['password'];
        $ldaphost = 'ldap.example.com';    // your ldap servers
        $baseDn = 'dc=example,dc=es';

        $options = [
            'host'              => $ldaphost,
            'username'          => $username,
            'password'          => $ldapPassword,
            'bindRequiresDn'    => false,
            'accountDomainName' => 'example.es',
            'baseDn'            => $baseDn,
        ];


        $userInterface = $this->em->getRepository('AppBundle:User')
            ->findOneBy(['email' => $username]);
        $ldap = new Ldap($options);

        try {
            $ldap->bind();
            $userInterface->setIsAuthenticationLDAP(true);
        } catch (LdapException $zle){
            $userInterface->setIsAuthenticationLDAP(false);
        }

        return $userInterface;
    }

    public function checkCredentials($credentials, UserInterface $user)
    {
        $password = $credentials['password'];

        if($user->isAuthenticationLDAP()){
            $user->setLoginAttempts(0);
            $this->em->persist($user);
            $this->em->flush();
            return true;
        } else {
           if($this->passwordEncoder->isPasswordValid($user, $password)) {
               $user->setLoginAttempts(0);
               $this->em->persist($user);
               $this->em->flush();
               return true;
           } else {
               if($user->getLoginAttempts() == '0') $user->setFirstLoginAttempt(new \DateTime('now'));
               $user->setLoginAttempts($user->getLoginAttempts() + 1);
               if($user->getLoginAttempts() >= 5) {
                   $user->setLockedDateTime(new \DateTime('now'));
                   $user->setLoginAttempts(0);
               }
               $this->em->persist($user);
               $this->em->flush();
           }
       }

       return false;
    }

    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
    {
       ....
    }

    protected function getLoginUrl()
    {
        return $this->router->generate('fos_user_security_login');
    }
}

希望大家能喜欢这个回答。