Common authentication code for Auth Authentication

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn how to use Auth. class. php to authenticate the system. authentication is frequently used, and improvements have been made on the basis of the above.
After successful login, save the current user's id to session ('uid', $ curUserId );
Configure in Config. php: 'Not _ AUTH_MODULE '=> 'public, Index, test', // no authentication module is required by default.
'Not _ AUTH_ACTION '=> '', // no authentication is required by default.
'Superadmin _ uid' => array (1, 2 ),
function authcheck($url,$relation='or'){

$linkArr=explode('/',$url);
if(!(count($linkArr)==2||count($linkArr)==3))return;

if(count($linkArr)==2){
$CUR_MODULE_NAME=$linkArr[0];
$CUR_ACTION_NAME=$linkArr[1];
}else{
$CUR_MODULE_NAME=$linkArr[1];
$CUR_ACTION_NAME=$linkArr[2];
}
$notAuth=in_array($CUR_MODULE_NAME, explode(",", C("NOT_AUTH_MODULE"))) || in_array($CUR_ACTION_NAME, explode(",", C("NOT_AUTH_ACTION")));

$AUTH_CONFIG=C('AUTH_CONFIG');
if(!$AUTH_CONFIG['AUTH_ON']||$notAuth)return true;
else{
if(!session("?uid"))return false;

if(in_array(session('uid'),C('SUPERADMIN_UID')))return true;
else{
import('ORG.Util.Auth');
$auth=new Auth();
if($auth->check($CUR_MODULE_NAME.'/'.$CUR_ACTION_NAME,session('uid'),$relation))return true;
else return false;
}
}
}
All actions must inherit CommonAction. class. php and be used in the _ initialize () method of Common: If (! Authcheck (MODULE_NAME. '/'. ACTION_NAME) $ this-> error ('You have no authorization', U ('index/Index '));In the background, store the name field in the think_auth_rule data table to the operation url, for example, User/index, User/addRecord ......

If you have any questions, you are welcome to discuss them.

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.