PHP zend_auth and Zend_acl login authentication and control according to user role permissions (1/4)

Source: Internet
Author: User
Tags auth

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.

home 1 2 3 4 last page

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.