Auth权限类使用示例
程序员文章站
2024-01-11 21:11:22
...
看了下官网上还没有一个Auth权限类实例应用,今天抽空写了下,希望对大家有所帮助
数据库如下
mc_admin_group
Field Type Allow Null Default Value
id mediumint(8) UNSIGNED No
title char(100) No
status tinyint(1) No 1
rules char(80) No
mc_admin_group_access
Field Type Allow Null Default Value
uid mediumint(8) UNSIGNED No
group_id mediumint(8) UNSIGNED No
mc_admin_menu
Field Type Allow Null Default Value
id smallint(6) No
name char(40) No
title varchar(50) No
condition varchar(60) No
pid smallint(6) No 0
g char(20) No
m char(20) No
a char(20) No
data char(30) No
listorder smallint(6) No 0
display enum('1','0') No '1'
status enum('1','0') No '1'
AdministratorAction.class.php
数据库如下
mc_admin_group
Field Type Allow Null Default Value
id mediumint(8) UNSIGNED No
title char(100) No
status tinyint(1) No 1
rules char(80) No
mc_admin_group_access
Field Type Allow Null Default Value
uid mediumint(8) UNSIGNED No
group_id mediumint(8) UNSIGNED No
mc_admin_menu
Field Type Allow Null Default Value
id smallint(6) No
name char(40) No
title varchar(50) No
condition varchar(60) No
pid smallint(6) No 0
g char(20) No
m char(20) No
a char(20) No
data char(30) No
listorder smallint(6) No 0
display enum('1','0') No '1'
status enum('1','0') No '1'
AdministratorAction.class.php
class AdministratorAction extends Action{
/**
* 初始化
*/
public function _initialize(){
parent::_initialize();
if($this->isLogin()){
$this->checkRule();
}
}
/**
* 判断是否登录
* @return bool
*/
public function isLogin(){
if(!session(C('ADMIN_KEY'))){
$this->redirect(C('ADMIN_GATEWAY'));
}
return true;
}
/**
* 检测是否有权限
* @return bool
*/
public function checkRule(){
if( in_array(GROUP_NAME,array('Admin')) && in_array(MODULE_NAME,array('Index')) || session(C('ADMINISTRATOR'))) return true;
$ruleName = '';
$menu = S('menu')?S('menu'):D('AdminMenu')->cache_menu();
foreach($menu as $v){
if(GROUP_NAME == $v['g'] && MODULE_NAME == $v['m'] && ACTION_NAME == $v['a']){
$ruleName = $v['name'];
}
}
$auth = new Auth();
if(!$auth->check($ruleName,session(C('ADMIN_KEY')))){
$this->error(L('LANG_NO_PERMISSION'),U(C('ADMIN_GATEWAY')));
}
}
}
AD:真正免费,域名+虚机+企业邮箱=0元
下一篇: js取得url地址参数实例_基础知识