2009-07-29 10 views
4

ZV MVCでのユーザーWebサイト/ REST認証のベストプラクティスは何ですか?方法とコードをZFフレームワークに入れる場所はどこですか?私にコード例を教えてもらえますか?Zend Frameworkユーザー認証

私はウェブサイトとZend Frameworkで書かれRESTサーバーが、実装されていないユーザーセッションのジェットを持っています。

THX!

+0

これで運がいいですか?私は同じ状況にいる。 –

答えて

1

認証は、ブートストラップファイルの_initAutoloadに設定されます。

if(Zend_Auth::getInstance()->hasIdentity()) { 
    Zend_Registry::set('role', Zend_Auth::getInstance() 
         ->getStorage()->read()->role); 
}else{ 
    Zend_Registry::set('role', 'guests'); 
} 

REST認証の場合は、フォームを使用してログインするのではなく、ログインパラメータを渡すだけで認証する必要があります。

だから、それはあなたのAuthenticationControllerで次のようになります。

private function getAuthAdapter() { 
    $authAdapter = new Zend_Auth_Adapter_DbTable(
         Zend_Db_Table::getDefaultAdapter()); 
    $authAdapter->setTableName('users') // the db table where users are stored 
       ->setIdentityColumn('email')      
       ->setCredentialColumn('password') 
       ->setCredentialTreatment('SHA1(CONCAT(?,salt))'); 

    return $authAdapter; 
} 

public function logoutAction() { 
    Zend_Auth::getInstance()->clearIdentity(); 
    $this->_redirect('index/index'); 
} 

public function loginAction(){ 
    if (Zend_Auth::getInstance()->hasIdentity()){ 
     $this->_redirect('index/index'); 
    } 
    if ($request->isPost()){ 
     $username = $request->getPost('username'); 
     $password = $request->getPost('password'); 

     if ($username != "" && $password != "") { 
      $authAdapter = $this->getAuthAdapter(); 
      $authAdapter->setIdentity($username) 
         ->setCredential($password); 
      $auth = Zend_Auth::getInstance(); 
      $result = $auth->authenticate($authAdapter); 

      if($result->isValid()){ 
       $identity = $authAdapter->getResultRowObject(); 
       $authStorage = $auth->getStorage(); 
       $authStorage->write($identity); 
       $this->_redirect ('index/index'); 
      } 
     } 
    } 
} 

あなたはzend_authzend_acl上でより多くの助けが必要な場合は、このhow toを見ているかもしれません。