#2 xiuluozhou
关键是类及方法的注释。
@docheck 为是否进行鉴权,
@fatherAction 为继承某个RUL的权限(目前只能有二级)
@AclSubjectName 在用户权限管理中显示的权限名称
下面是baseController.php
class baseController extends spController{
function __construct(){
parent::__construct();
$this->spc = parent;
$this->spModel = spClass('spModel');
$this->basectl = $this;
$this->syskey = $GLOBALS['SYS_DEFINE']['SYSKEY'];
switch($this->syskey){
case '_MC_':$this->initMC();break;
case '_ADMIN_':$this->initAdmin();break;
default://$this->initANPO();break;
}
//print_r($this->giveAllAcl());
//print_r($this->Aclarr);
//print_r($this->UserAcl);exit;
}
function initAdmin(){
$this->getRouter($this->syskey);
$this->Acl = $this->spModel->getCaches('_SYS'.$this->syskey.'ACL_TREE'); //用户管理权限tree使用
$this->Aclarr = $this->spModel->getCaches('_SYS'.$this->syskey.'ACL_ARR'); //所有需要鉴权的栏目
$this->Router = $this->spModel->getCaches('_SYS'.$this->syskey.'ACL'); //全局用户权限鉴权使用
$this->UserAcl = empty($_SESSION[$this->syskey.'userinfo'])?array(md5('c_index_a_index'.$this->syskey)):unserialize($_SESSION[$this->syskey.'userinfo']['user_rule']);
//print_r($this->Router);exit;
$this->UserInfo = $_SESSION[$this->syskey.'userinfo'];//print_r($_SESSION['bms_userinfo']);
$this->checkLogin($this->syskey);
$this->checkAcl();
$this->initUI();
$this->HTML = spClass('MakeHTML');
$this->initTemplateFunc();
}
function initMC(){
$this->getRouter($this->syskey);
$this->Acl = $this->spModel->getCaches('_SYS'.$this->syskey.'ACL_TREE'); //用户管理权限tree使用
$this->Aclarr = $this->spModel->getCaches('_SYS'.$this->syskey.'ACL_ARR'); //所有需要鉴权的栏目
$this->Router = $this->spModel->getCaches('_SYS'.$this->syskey.'ACL'); //全局用户权限鉴权使用
$this->UserAcl = empty($_SESSION[$this->syskey.'userinfo'])?array(md5('c_index_a_index'.$this->syskey)):unserialize($_SESSION[$this->syskey.'userinfo']['user_rule']);
$this->UserInfo = $_SESSION[$this->syskey.'userinfo'];//print_r($_SESSION['mcenter_userinfo']);
$this->checkLogin($this->syskey);
$this->checkAcl();
$this->initUI();
$this->HTML = spClass('MakeHTML');
$this->initTemplateFunc();
}
function initUI(){
$a = $this->spArgs('a','index');
$c = $this->spArgs('c','index');
$this->ListPageTabName = 'c_'.$c.'_a_'.$a;
}
function initTemplateFunc(){
spAddViewBlock("getads", array('Sblock', 'do_translation'));
//spAddViewFunction('getads', array( 'Sblock', 'get_ad'));
}
function setUserInfo($userinfo){
$this->UserInfo = $userinfo;
$_SESSION[$this->syskey.'userinfo'] = $userinfo;
$this->UserAcl = unserialize($userinfo['user_rule']);
}
function getCaches($name){
if($this->cacheTime==0)return false;
return spAccess('r', $name);
}
function setCaches($name,$value,$always=false){
if($always===true)spAccess('w', $name, $value, -1);
else spAccess('w', $name, $value, $this->cacheTime);
}
function checkLogin(){
$a = $this->spArgs('a','index');
$c = $this->spArgs('c','index');
//print_r($_SESSION);exit;
if(empty($this->UserInfo['user_id'])){
if('c_'.$c.'_a_'.$a!='c_index_a_login'){
$this->goahead("请先登录",spUrl("index","login"));
exit;
}
}
}
//查询父级权限
function getFaherKey($key){
$ACL = $this->spModel->getCaches('_SYS'.$this->syskey.'ACL_TREE');
foreach($ACL as $keys=>$value){
foreach($value['items'] as $k=>$v){
if($key==$k){
if(!empty($v['fatherAcl'])){
return $v['fatherAcl'];
}
}
}
}
return false;
}
//鉴权
function checkAcl(){
$a = $this->spArgs('a','index');
$c = $this->spArgs('c','index');
$fkey = 'c_'.$c.'_a_'.$a;
$key = $this->getFaherKey($fkey);//echo $key;exit;
$key = $key==''?md5($fkey.$this->syskey):md5($key.$this->syskey);
if(!in_array('c_'.$c.'_a_'.$a,$this->Aclarr) or in_array($key,$this->UserAcl)){
return true;
}else{
//$this->display("sys/no_acl.php");
echo '';exit;
exit;
}
}
/**
* index
* 获取所有路由
* 规则如下:
* 1、controller中,必须添加注释,否则权限功能会缺失(包括控制器类的注释和对应方法注释,构造函数不需注释)!,规则如示例中所示。
* 2、@author:作者姓名。
* 3、@docheck:是否鉴权
* 4、@fatherAction:鉴权时当此参数不为空时,则继承此参数值对应的栏目权限鉴权(只支持两级,即不可继承已继承权限的栏目!)。例:c_index_a_index
* 5、@AclSubjectName:该路由所指向的应用中文名称
示例:
/**
* user
*
* @author ShuraChow
* @docheck true
* @fatherAction
* @AclSubjectName 用户管理
*/
class user extends baseController{
/**
* user-index
*
* @author ShuraChow
* @docheck true
* @fatherAction c_user_a_useredit
* @AclSubjectName 用户列表
*/
function index(){
……
}
}
/**
* @author ShuraChow
*/
function getRouter($sys='_MC_'){
$cname = array();
//if(empty($_SESSION['_SYSACL'])){
if(!$this->spModel->getCaches('_SYS'.$sys.'ACL')){
//递归载入所有控制类
$cp = getAllFile($GLOBALS['SYS_DEFINE']['APP_PATH'].'/'.'controller',true);
if(is_array($cp['php'])){
foreach ($cp['php'] as $p){
require_once $p;
}
}
$allclass = get_declared_classes();
foreach($cp['php'] as $k=>$v){
$_classn = basename($v,".php");
//echo $_classn.'
';
$_class = new ReflectionClass($_classn);
$Doc = $_class->getDocComment();
preg_match_all('/@docheck\s+(\S*)/i',$Doc,$Acldo);
preg_match_all('/@fatherAction\s+(\S*)/i',$Doc,$fatherAcl);
if($Acldo[1][0]=='true'){
preg_match_all('/@AclSubjectName\s+(\S*)/i',$Doc,$AclSubjectName);
$AclTree['c_'.$_classn]['subject'] = $AclSubjectName[1][0];
$AclTree['c_'.$_classn]['fatherAcl'] = $fatherAcl[1][0];
$AclTree['c_'.$_classn]['key'] = md5('c_'.$_classn.$sys);
$Acl[md5('c_'.$_classn.$sys)] = $AclSubjectName[1][0];
$AclArr[] = 'c_'.$_classn;
//print_r($_class->getMethods());
foreach($_class->getMethods() as $rk => $rv){
//if($rv->class == $_classn and !preg_match('/_/',$rv->name)){
if($rv->class == $_classn){
$_method = new ReflectionMethod($_classn, $rv->name);
$Doc = $_method->getDocComment();
preg_match_all('/@docheck\s+(\S*)/i',$Doc,$Acldo);
preg_match_all('/@fatherAction\s+(\S*)/i',$Doc,$fatherAcls);
if($Acldo[1][0]=='true'){
preg_match_all('/@AclSubjectName\s+(\S*)/i',$Doc,$AclSubjectName);
$AclTree['c_'.$_classn]['items']['c_'.$_classn.'_a_'.$rv->name]['subject'] = $AclSubjectName[1][0];
$AclTree['c_'.$_classn]['items']['c_'.$_classn.'_a_'.$rv->name]['fatherAcl'] = $fatherAcls[1][0];
$AclTree['c_'.$_classn]['items']['c_'.$_classn.'_a_'.$rv->name]['key'] = md5('c_'.$_classn.'_a_'.$rv->name.$sys);
$Acl[md5('c_'.$_classn.'_a_'.$rv->name.$sys)] = $AclSubjectName[1][0];
$AclArr[] = 'c_'.$_classn.'_a_'.$rv->name;
}
}
}
}
}
//始终数据缓存,仅当删除缓存文件时才更新
$this->spModel->always = true;
$this->spModel->setCaches('_SYS'.$sys.'ACL_TREE',$AclTree);
$this->spModel->setCaches('_SYS'.$sys.'ACL_ARR',$AclArr);
$this->spModel->setCaches('_SYS'.$sys.'ACL',$Acl);
//$_SESSION['_SYS'.$sys.'ACL'] = $AclTree;
}
//print_r($_SESSION['_SYS'.$sys.'ACL']);
//print_r($this->spModel->getCaches('_SYS'.$sys.'ACL_TREE'));
//return $this->spModel->getCaches('_SYS'.$sys.'ACL_TREE');
}
function giveAllAcl(){
return array_keys($this->Router);
}
}
2012-11-30 11:01:37