Zend_auth_adapter_interface provides an interface that we need to implement ourselves
The code is as follows:
The code is as follows |
Copy Code |
<?php Require_once ' zend/auth/adapter/interface.php '; Class Auth implements zend_auth_adapter_interface{ Private $_useraccount; Private $_password; Private $_db; /** * constructor sets user name and password data connection object * * @return void */ Public function __construct ($useraccount, $password, $db) { $this->_useraccount = $useraccount; $this->_password = $password; $this->_db = $db; }
/** * For Certification * @throws zend_auth_adapter_exception * @return Zend_auth_result * */ Public function Authenticate () { Authentication failed by default $authResult = Array ( ' Code ' => zend_auth_result::failure,//details: zend_auth_result ' Identity ' => ', ' Info ' => Array () );
Get Login User Information $result = $this->_getaccountdata ();
if (Isset ($result) &&!empty ($result)) {//authenticated successfully, store user information in session $authResult = Array ( ' Code ' => zend_auth_result::success, ' Identity ' => $result [' name '], ' Info ' => array (' status ' => $result [' status ']) ); Role Store Personal cache space $namespace = Zend_auth::getinstance ()//single case mode -> GetStorage () -> GetNamespace (); $_session[$namespace] [' role '] = $result [' group_id '];//member group $_session[$namespace] [' userInfo '] = $result; $_session[$namespace] [' userInfo '] [' lastlogintime '] = $result [' Login_time ']; $_session[$namespace] [' userInfo '] [' lastloginip '] = $result [' login_ip ']; $_session[$namespace] [' userInfo '] [' password '] = $result [' Password '];//password is important, do not write to the session }
return new Zend_auth_result ($authResult [' Code '], $authResult [' identity '], $authResult [' info ']); } /** * User Password encryption * @param $pwd Original password * @return string with encrypted password * */ static public Function EncryptionType ($pwd =null) { $PWD = MD5 ($PWD); return $pwd; } /** * Get User Data structure * * @todo public classes to organize passwords */ Private Function _getaccountdata () { $RESARR = Array (); $SYSUSEROBJ = Base_dao_factory::getobject (' Admin_models_user '); Log in to ordinary member account first $data = Array ( ' Login_name ' => $this->_useraccount, ' Login_pwd ' => $this->encryptiontype ($this->_password) ); $result = $SYSUSEROBJ->login ($data); To determine whether there is data, is the assignment if ($result) { if (!empty ($result [0])) { $RESARR = $result [0]; } } return $RESARR; } } |
Explanation: In the implementation code of the Authenticate method, return a Zend_auth_result object instance and view the source code of the Zend_auth_result, which requires three parameters to be passed when instantiating:
@param the results of an int $code authentication (e.g., zend_auth_result::success)
@param mixed $identity identifiers for authentication (for example, login name (John))
Array $messages authentication Failures @param array
Once the authentication is successful, the information is stored in the session variable.