added 实现权限认证

This commit is contained in:
zhaoxiang 2016-11-15 12:31:42 +08:00
parent dea3f66196
commit a2bb140d01
2 changed files with 44 additions and 39 deletions

View File

@ -53,12 +53,12 @@ class Base extends Controller {
if( !in_array($this->url, $this->superUrl) ){ if( !in_array($this->url, $this->superUrl) ){
$menuInfo = Menu::where(['url' => $this->url])->find(); $menuInfo = Menu::where(['url' => $this->url])->find();
if( is_null($menuInfo) ){ if( is_null($menuInfo) ){
$this->error( '目录:'.$this->url.'不存在!' ); $this->error( '目录:'.$this->url.'不存在!', '' );
}else{ }else{
$this->menuInfo = $menuInfo->toArray(); $this->menuInfo = $menuInfo->toArray();
} }
$this->checkLogin(); $this->checkLogin();
// $this->checkRule(); $this->checkRule();
} }
} }
@ -90,4 +90,11 @@ class Base extends Controller {
$this->redirect('User/login'); $this->redirect('User/login');
} }
} }
private function checkRule(){
$check = (new \Permission())->check($this->url, $this->uid);
if( !$check ){
$this->error('权限认证失败!', '');
}
}
} }

View File

@ -34,23 +34,18 @@ class Permission {
protected $_config = [ protected $_config = [
'AUTH_ON' => true, //认证开关 'AUTH_ON' => true, //认证开关
'AUTH_TYPE' => 0, //认证方式0为时时认证1为登录认证[Cache缓存]2为登录认证[SESSION缓存]。 'AUTH_TYPE' => 0, //认证方式0为时时认证1为登录认证[Cache缓存]2为登录认证[SESSION缓存]。
'AUTH_GROUP' => 'AuthGroup', //用户组数据表名 'AUTH_GROUP' => 'auth_group', //用户组数据表名
'AUTH_GROUP_ACCESS' => 'AuthGroupAccess', //用户组明细表 'AUTH_GROUP_ACCESS' => 'auth_group_access', //用户组明细表
'AUTH_RULE' => 'AuthRule', //权限规则表 'AUTH_RULE' => 'auth_rule', //权限规则表
'AUTH_USER' => 'User' //用户信息表 'AUTH_USER' => 'user' //用户信息表
]; ];
public function __construct() { public function __construct() {
$options = [ foreach ( $this->_config as $key => $value ){
'AUTH_ON' => config('AUTH_ON'), $confValue = config($key);
'AUTH_TYPE' => config('AUTH_TYPE'), if( !is_null($confValue) ){
'AUTH_GROUP' => config('AUTH_GROUP'), $this->_config[$key] = $confValue;
'AUTH_GROUP_ACCESS' => config('AUTH_GROUP_ACCESS'), }
'AUTH_RULE' => config('AUTH_RULE'),
'AUTH_USER' => config('AUTH_USER')
];
if ( $options ) {
$this->_config = array_merge($this->_config, $options);
} }
} }
@ -91,7 +86,7 @@ class Permission {
$action = 0; $action = 0;
break; break;
} }
$authList[$name] = isset($authList[$name])?$authList[$name]:0;
return $authList[$name] & $action; return $authList[$name] & $action;
} }
@ -107,16 +102,22 @@ class Permission {
return $groups[$uid]; return $groups[$uid];
} }
$userGroups = \think\Db::table($this->_config['AUTH_GROUP_ACCESS'])->where(['uid' => $uid])->select(); $userGroups = \think\Db::table($this->_config['AUTH_GROUP_ACCESS'])->where(['uid' => $uid])->select();
foreach( $userGroups as &$value ){ if( !empty($userGroups) ){
$groupInfo = \think\Db::table($this->_config['AUTH_GROUP'])->where(['id' => $value['groupId']])->find()->toArray(); $groups[$uid] = [];
foreach( $userGroups as $value ){
$groupInfo = \think\Db::table($this->_config['AUTH_GROUP'])->where(['id' => $value['group_id']])->find();
if( !is_null($groupInfo) ){
if( $groupInfo['status'] != 1 ){ if( $groupInfo['status'] != 1 ){
unset($value); continue;
}else{ }else{
$value = $groupInfo; $groups[$uid][] = $value['group_id'];
}
} }
} }
$groups[$uid]=$userGroups?$userGroups:[];
return $groups[$uid]; return $groups[$uid];
}else{
return [];
}
} }
/** /**
@ -125,7 +126,6 @@ class Permission {
* @return array * @return array
*/ */
public function getAuthList( $uid ) { public function getAuthList( $uid ) {
static $_authList = []; static $_authList = [];
if (isset($_authList[$uid])) { if (isset($_authList[$uid])) {
return $_authList[$uid]; return $_authList[$uid];
@ -140,25 +140,23 @@ class Permission {
} }
} }
$groups = $this->getGroups($uid); $groups = $this->getGroups($uid);
$ids = []; if ( empty($groups) ) {
foreach ($groups as $g) {
$ids = array_merge($ids, explode(',', trim($g['rules'], ',')));
}
$ids = array_unique($ids);
if ( empty($ids) ) {
$_authList[$uid] = []; $_authList[$uid] = [];
return []; return [];
} }
$authList = []; $authList = [];
foreach ($ids as $IValue){ foreach ($groups as $g) {
$tmp = explode(':',$IValue); $groupRule = \think\Db::table($this->_config['AUTH_RULE'])->where(['group_id' => $g])->select();
if( isset($authList[$tmp[0]]) ){ if( !empty($groupRule) ){
$authList[$tmp[0]] = $authList[1] | $authList[1]; foreach ( $groupRule as $groupValue ){
if( isset($authList[$groupValue['url']]) ){
$authList[$groupValue['url']] = $authList[$groupValue['url']] | $groupValue['auth'];
}else{ }else{
$authList[$tmp[0]] = $authList[1]; $authList[$groupValue['url']] = $groupValue['auth'];
}
}
} }
} }