2017-04-03 8 views
1

ホイールを再開発する前に、まずZF2がサポートしているかどうか、またはサードパーティのライブラリを使用しているかどうかを確認したいと思います。この特定の使用例は、管理者log in as他のユーザー、またはassumeのID。ZF2 - 他のユーザーの身元を確認する

もし私がZF2の内部設計に精通していないとして、これを実装するにはどうすればよいですか?唯一の制約はシステムが既に構築されていることです。コンポーネント(コントローラ、認証サービス、など)をサポートするために。

私が最初に考えたのは、セッションストレージに保存されているログされたユーザー情報を、自分のIDを引き継ぐものに切り替える仕組みを作ることでした。次に、元のユーザー情報(admin)を別の名前空間でセッションに書き戻して元に戻すことができます。

このアプローチでは、Zend\Authentication\AuthenticationServiceのようなコンポーネントが、私の身元を想定しているユーザーを返すと期待しています。だから、$this->identity()->getId()(IDは、AuthenticationServiceのコントローラプラグインであり、それは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を使用することをお勧めします。

リソースがZF3のためのものですが、私は思う:私は、次のリソースを見てお勧めだろうかそれをすべて一緒プラグに表示するには

/** 
* 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(); 
    } 

} 

ユーザーの認証と認証の管理はzf2と非常によく似ています。

関連する問題