-1

認証目的で Zend_auth を使用しています。同じコードは次のとおりです。

             $authAdapter = $this->getAuthAdapter();
            $authAdapter->setIdentity($username)
                    ->setCredential($password);
            $auth = Zend_Auth::getInstance();
            $result = $auth->authenticate($authAdapter);
            # is the user a valid one?
            if ($result->isValid()) {
                # all info about this user from the login table
                # ommit only the password, we don't need that
                $userInfo = $authAdapter->getResultRowObject(null, 'password');

                # the default storage is a session with namespace Zend_Auth
                $authStorage = $auth->getStorage();
                $authStorage->write($userInfo);
                $emp_id = $userInfo->employee_id;
                $userInfo = Zend_Auth::getInstance()->getStorage()->read();
                $array_db = new Application_Model_SetMstDb();
                $array_name = $array_db->getName($emp_id);

                foreach ($array_name as $name) :
                    $fname = $name['first_name'];
                    $lname = $name['last_name'];
                endforeach;

                $firstname = new stdClass;
                $lastname = new stdClass;
                $userInfo->firstname = $fname;
                $userInfo->lastname = $lname;

                $privilege_id = $userInfo->privilege_id;
                echo 'privilege in Login: ' . $privilege_id;
                $this->_redirect('index/index');
            } else {
                $errorMessage = "Invalid username or password";
                $this->view->error = $errorMessage;
              }

getAuthAdapter() は次のようになります。

     protected function getAuthAdapter() {
    $dbAdapter = Zend_Db_Table::getDefaultAdapter();
    $authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);

    $authAdapter->setTableName('credentials')
            ->setIdentityColumn('employee_id')
            ->setCredentialColumn('password');


    return $authAdapter;
}

セッションのタイムアウトを設定したい。タイムアウトを 5 分間に設定したい。ユーザーが 5 分間アクティブでない場合は、セッションを期限切れにする必要があります。つまり、ログアウト アクションを呼び出す必要があります。そのコードは次のとおりです。

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

前もって感謝します。助けてください。緊急です。

私が使うとき

    $session = new Zend_Session_Namespace( 'Zend_Auth' ); 
    $session->setExpirationSeconds( 60 ); 

コントロールは 60 秒後に自動的にログイン ページにリダイレクトしますが、アプリケーションのユーザーが 60 秒間非アクティブである場合は、それのみがリダイレクトされるようにしたいと考えています。

4

2 に答える 2

0

私は今これのための私のコードを見ています。このスニペットは、フロント コントローラー プラグインからのものです。認証されたユーザーがページをリクエストするたびに、セッションの有効期限をリセットして、最後に「アクティブ」になってから 60 分経過するようにします。

    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request) {

    //check whether the client is authenticated
    if (Zend_Auth::getInstance()->hasIdentity()) {

        $session = $this->_getAuthSession();

        //update session expiry date to 60mins from NOW
        $session->setExpirationSeconds(60*60);

        return;
    }

余談ですが、現在の「認証されていません」というメッセージではなく、「セッションの有効期限が切れました」というメッセージをユーザーに表示する方法について、このコードを調べています。

于 2012-06-19T23:36:26.220 に答える
0

これには init() を使用しません。オブジェクトの状態を設定するには、init() を使用する必要があります。

preDispatch() を使用します。ただし、すべてのコントローラーを使用したり、ベースコントローラーを作成してから拡張したりすることは避けてください。プラグインを作成して Bootstrap に追加できます。

class YourControllerPlugin extends Zend_Controller_Plugin_Abstract {
   public function preDispatch() {
        //check if expired
       if(hasExpired()) {
          //logout and redirect
       }
   }
}

Bootstrap に追加するには:

public function __initYourPlugin () {
    $this->bootstrap('frontController');

    $plugin = new YourControllerPlugin();

    $front = Zend_Controller_Front::getInstance();
    $front->registerPlugin($plugin);

    return $plugin;
}
于 2012-04-23T11:22:23.583 に答える