2017-11-07 2 views
0

ddbb가 구성한 Guard 인증 시스템에 내부 사용자 용 LDAP를 포함시키는 것입니다. 이미 Guard Monitor System을 구축했으며 https://knpuniversity.com/screencast/symfony-security 덕분에 정말 좋은 결과를 얻었습니다.Symfony 3의 Guard 인증 시스템이있는 LDAP

하지만 LDAP 모드를 통해 이전에 로그인을 시도해야합니다. 더 정확하게, 기능은 다음과 같이해야합니다 : MySQL의에서 테이블 사용자에 사용자가 존재하는 경우

1 체크 :

사용자 것은 MySQL의에서 데이터베이스로 구성 가드 시스템 인증에 로그인을 시도 . 존재하는 경우 2 단계로 이동합니다. 존재하지 않으면 오류 메시지와 함께 인증에 false를 반환합니다.

2 - 사용자가 LDAP 모드에 있는지 확인하십시오. 존재하는 경우 3 단계로 이동하십시오. 4 단계로 이동하십시오.

3 - 사용자 이름과 암호로 LDAP를 통해 로그인하십시오. 인증이 정상이면 로그인합니다. LDAP를 통해 비밀번호와 일치 할 수없는 경우 오류 메시지와 함께 인증에 false를 반환합니다.

4 - LDAP 옵션을 확인한 후 Guard Authentication System을 통해 로그인을 시도합니다. 인증이 성공적이면 사용자가 로그인합니다. Guard를 통해 MySQL 사용자 테이블과 암호를 일치시킬 수 없으면 오류 메시지와 함께 인증에 false를 반환합니다.

답변

0

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'); 
    } 
} 

누구든지이 답변을 누리 길 바랍니다.