PHP Zend_auth and Zend_acl for login authentication and permission control based on user Role (section) _php Tutorial

Source: Internet
Author: User
Zend_auth and Zend_acl for login authentication and permissions control according to user role have a friend to refer to.

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

Require_once ' zend/auth/adapter/interface.php ';
Class Auth implements zend_auth_adapter_interface{
Private $_useraccount;
Private $_password;
Private $_db;
/**
* Constructor set 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 logged in User information
$result = $this->_getaccountdata ();

if (Isset ($result) &&!empty ($result)) {//authentication is successful, the user information is stored in the 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 ()//Singleton mode
GetStorage ()
GetNamespace ();
$_session[$namespace [' role '] = $result [' group_id '];//the owning user 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 of encrypted password string
*
*/
static public Function EncryptionType ($pwd =null) {
$PWD = MD5 ($PWD);
return $pwd;
}
/**
* Get User Data structure
*
* @todo public class to organize passwords
*/
Private Function _getaccountdata () {
$RESARR = Array ();
$SYSUSEROBJ = Base_dao_factory::getobject (' Admin_models_user ');
Login to the normal member account first
$data = Array (
' Login_name ' = $this->_useraccount,
' Login_pwd ' = $this->encryptiontype ($this->_password)
);
$result = $SYSUSEROBJ->login ($data);
Determine if there is data, then assign a value
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 look at the source code of the Zend_auth_result, knowing that the instantiation needs to pass in three parameters:

@param the result of an int $code authentication (for example: zend_auth_result::success)

@param mixed $identity identifiers used for authentication (e.g. login name (Zhang San))

@param array $messages reason for authentication failure

Once the authentication is successful, the information is stored in the session variable.

1 2 3 4

http://www.bkjia.com/PHPjc/444673.html www.bkjia.com true http://www.bkjia.com/PHPjc/444673.html techarticle Zend_auth and Zend_acl for login authentication and permissions control according to user role have a friend to refer to. Zend_auth_adapter_interface provides an interface that we need from ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.