Logon authentication for Zend_Auth and Zend_Acl in php and permission control based on user roles (1/4) _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
In php, Zend_Auth and Zend_Acl are used for logon authentication and permission control based on user roles (14 ). Zend_Auth and Zend_Acl are used for logon authentication and permission control based on user roles. For more information, see. Zend_Auth_Adapter_Interface provides an interface. we need to perform logon authentication from Zend_Auth and Zend_Acl and perform permission control based on the user role. For more information, see.

Zend_Auth_Adapter_Interface provides an interface, which needs to be implemented by ourselves.

The code is as follows:

The code is as follows:

Require_once 'zend/Auth/Adapter/Interface. php ';
Class Auth implements Zend_Auth_Adapter_Interface {
Private $ _ useraccount;
Private $ _ password;
Private $ _ db;
/**
* The Constructor sets the user name and password data connection object.
*
* @ Return void
*/
Public function _ construct ($ useraccount, $ password, $ db ){
$ This-> _ useraccount = $ useraccount;
$ This-> _ password = $ password;
$ This-> _ db = $ db;
}

/**
* Perform authentication
* @ Throws Zend_Auth_Adapter_Exception
* @ Return Zend_Auth_Result
*
*/
Public function authenticate ()
{
// Authentication fails by default
$ AuthResult = array (
'Code' => Zend_Auth_Result: FAILURE, // detailed parameter: Zend_Auth_Result
'Identity '=> '',
'Info' => array ()
);

// Obtain logon user information
$ Result = $ this-> _ getAccountData ();

If (isset ($ result )&&! Empty ($ result) {// If authentication succeeds, the user information is stored in the session
$ AuthResult = array (
'Code' => Zend_Auth_Result: SUCCESS,
'Identifi' => $ result ['name'],
'Info' => array ('status' => $ result ['status'])
);
// Role storage personal cache space
$ Namespace = Zend_Auth: getInstance () // Singleton mode
-> GetStorage ()
-> GetNamespace ();
$ _ SESSION [$ namespace] ['role'] = $ result ['group _ id']; // 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']; // The password is very important and should not be written to the session.
}

Return new Zend_Auth_Result ($ authResult ['code'], $ authResult ['auth'], $ authResult ['info']);
}
/**
* User password encryption
* @ Param $ pwd original password
* @ Return string the encrypted password string
*
*/
Static public function encryptionType ($ pwd = null ){
$ Pwd = md5 ($ pwd );
Return $ pwd;
}
/**
* Obtain the user data structure
*
* @ Todo public classes for password sorting
*/
Private function _ getAccountData (){
$ ResArr = array ();
$ SysUserObj = Base_Dao_Factory: getObject ('admin _ Models_User ');
// Log on to the ordinary member account first
$ Data = array (
'Login _ name' => $ this-> _ useraccount,
'Login _ pwd' => $ this-> encryptionType ($ this-> _ password)
);
$ Result = $ sysUserObj-> login ($ data );
// Determines whether data exists. If yes, the value is assigned.
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 Zend_Auth_Result. it is known that three parameters need to be input during instantiation:

@ Param int $ code ID authentication result (for example, Zend_Auth_Result: SUCCESS)

@ Param mixed $ identity indicates the identity used for identity authentication (for example, logon name (John ))

@ Param array $ array of causes of messages authentication failure

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

1 2 3 4

For more information, see. Zend_Auth_Adapter_Interface provides an interface...

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.