2017-04-03 3 views
1

휠 재발견에 들어가기 전에 먼저 ZF2가 기본 또는 타사 라이브러리를 지원하는지 확인하고 싶습니다.이 특별한 경우는 관리자 log in as입니다. 다른 사용자 또는 assume의 신원입니다.ZF2 - 다른 사용자의 신원 가정

그렇지 않은 경우 ZF2 내부 설계에 익숙하지 않으므로 시스템을 이미 구축 했으므로 구성 요소를 변경할 수 없습니다 (컨트롤러, 인증 서비스 , 등)을 지원합니다.

나의 첫 번째 생각은 세션 기억 장치에 저장된 기록 된 사용자 정보를 전환 할 수있는 메커니즘을 만드는 것입니다. 그런 다음 다른 네임 스페이스의 세션에 되돌릴 수 있도록 원래 사용자 정보 (admin)를 작성합니다.

이 방법을 사용하면 Zend\Authentication\AuthenticationService과 같은 구성 요소가 사용자의 신원을 가정 할 것으로 예상됩니다. 따라서 $this->identity()->getId() (인증은 IdentityService의 컨트롤러 플러그인이며 다른 컨트롤러에서는 User을 반환 함)로 호출하면 비즈니스 로직이 정상적으로 작동합니다.

이이 질문이 될 것이라고 말했다 데 :

  1. 솔루션이 이미 있습니까?
  2. 내 접근법은 세션 저장소를 덮어 쓰면 다른 사용자 ID를 가정하고 그에 따라 ZF2 구성 요소가 작동한다고 가정 할 수 있습니까? 아니면 ZF2 내부 설계/인프라와 관련하여 고려해야 할 사항이 있습니까? ?
  3. 아마도 더 좋은 방법이 있을까요?

답변

1

고유 한 AuthenticationAdaptor를 만들어야한다고 생각합니다.

class AdminUserLoginAsUser implements \Zend\Authentication\Adapter\AdapterInterface 
{ 

    /** 
    * @var User 
    */ 
    private $userToLoginAs; 

    /** 
    * @var AdminUser 
    */ 
    private $adminUser; 

    public function __construct(User $userToLoginAs, AdminUser $adminUser) 
    { 

     $this->userToLoginAs = $userToLoginAs; 
     $this->adminUser = $adminUser; 
    } 

    /** 
    * Performs an authentication attempt 
    * 
    * @return \Zend\Authentication\Result 
    * @throws \Zend\Authentication\Adapter\Exception\ExceptionInterface If authentication cannot be performed 
    */ 
    public function authenticate() 
    { 
     return new \Zend\Authentication\Result(
      Result::SUCCESS, $this->user, [ 
       'You have assumed control of user.', 
      ] 
     ); 
    } 
} 

위의 클래스를 사용하면 Zend의 AuthenticationService 클래스와 함께 다른 사용자로 로그인 할 수 있습니다.

Zend의 AuthenticationService 클래스를 사용하는 방법이 필요하며 AuthenticationService를 감싸는 AuthManager를 사용하는 것이 좋습니다.

/** 
* The AuthManager service is responsible for user's login/logout and simple access 
* filtering. The access filtering feature checks whether the current visitor 
* is allowed to see the given page or not. 
*/ 
class AuthManager 
{ 
    /** 
    * Authentication service. 
    * @var \Zend\Authentication\AuthenticationService 
    */ 
    private $authService; 

    /** 
    * Session manager. 
    * @var Zend\Session\SessionManager 
    */ 
    private $sessionManager; 

    /** 
    * Contents of the 'access_filter' config key. 
    * @var array 
    */ 
    private $config; 

    /** 
    * Constructs the service. 
    */ 
    public function __construct($authService, $sessionManager, $config) 
    { 
     $this->authService = $authService; 
     $this->sessionManager = $sessionManager; 
     $this->config = $config; 
    } 

    /** 
    * Performs a login attempt. If $rememberMe argument is true, it forces the session 
    * to last for one month (otherwise the session expires on one hour). 
    */ 
    public function login($email, $password, $rememberMe) 
    { 
     // Check if user has already logged in. If so, do not allow to log in 
     // twice. 
     if ($this->authService->getIdentity()!=null) { 
      throw new \Exception('Already logged in'); 
     } 

     // Authenticate with login/password. 
     $authAdapter = $this->authService->getAdapter(); 
     $authAdapter->setEmail($email); 
     $authAdapter->setPassword($password); 
     $result = $this->authService->authenticate(); 

     // If user wants to "remember him", we will make session to expire in 
     // one month. By default session expires in 1 hour (as specified in our 
     // config/global.php file). 
     if ($result->getCode()==Result::SUCCESS && $rememberMe) { 
      // Session cookie will expire in 1 month (30 days). 
      $this->sessionManager->rememberMe(60*60*24*30); 
     } 

     return $result; 
    } 

    public function loginAsUser($user) 
    { 
     // Check if user has already logged in. If so, do not allow to log in 
     // twice. 
     if ($this->authService->getIdentity() !== null) { 
      throw new \Exception('Not logged in.'); 
     } 

     // First need to logout of current user 
     $this->authService->clearIdentity(); 

     $authAdapter = $this->authService->setAdapter(new AdminUserLoginAsUser($user, $this->authService->getIdentity())); 
     return $this->authService->authenticate(); 

    } 

    /** 
    * Performs user logout. 
    */ 
    public function logout() 
    { 
     // Allow to log out only when user is logged in. 
     if ($this->authService->getIdentity()==null) { 
      throw new \Exception('The user is not logged in'); 
     } 

     // Remove identity from session. 
     $this->authService->clearIdentity(); 
    } 

} 

나는 다음과 같은 자원을보고하는 것이 좋습니다 것 모두 함께 연결하는 방법을 참조하십시오 :

자원