123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- <?php
- namespace com;
- class Auth{
-
- protected $_config = array(
- 'auth_on' => true,
- 'auth_type' => 1,
- 'auth_group' => 'think_auth_group',
- 'auth_group_access' => 'think_auth_group_access',
- 'auth_rule' => 'think_auth_rule',
- 'auth_user' => 'think_admin'
- );
- public function __construct() {
- if (config('auth_config')) {
-
- $this->_config = array_merge($this->_config, config('auth_config'));
- }
- }
-
- public function check($name, $uid, $type=1, $mode='url', $relation='or') {
- if (!$this->_config['auth_on'])
- return true;
-
- $authList = $this->getAuthList($uid,$type);
-
- if (is_string($name)) {
- $name = strtolower($name);
- if (strpos($name, ',') !== false) {
- $name = explode(',', $name);
- } else {
- $name = array($name);
- }
- }
- $list = array();
- if ($mode=='url') {
- $REQUEST = unserialize( strtolower(serialize($_REQUEST)) );
- }
- foreach ( $authList as $auth ) {
- $query = preg_replace('/^.+\?/U','',$auth);
- if ($mode=='url' && $query!=$auth ) {
- parse_str($query,$param);
- $intersect = array_intersect_assoc($REQUEST,$param);
- $auth = preg_replace('/\?.*$/U','',$auth);
- if ( in_array($auth,$name) && $intersect==$param ) {
- $list[] = $auth ;
- }
- }else if (in_array($auth , $name)){
- $list[] = $auth ;
- }
- }
-
- if ($relation == 'or' and !empty($list)) {
- return true;
- }
- $diff = array_diff($name, $list);
- if ($relation == 'and' and empty($diff)) {
- return true;
- }
- return false;
- }
-
- public function getGroups($uid) {
- static $groups = array();
- if (isset($groups[$uid]))
- return $groups[$uid];
- $user_groups = \think\Db::table($this->_config['auth_group_access'])
- ->alias('a')
- ->join($this->_config['auth_group']." g", "g.id=a.group_id")
- ->where("a.uid='$uid' and g.status='1'")
- ->field('uid,group_id,title,rules')->select();
- $groups[$uid] = $user_groups ? $user_groups : array();
- return $groups[$uid];
- }
-
- protected function getAuthList($uid,$type) {
- static $_authList = array();
- $t = implode(',',(array)$type);
- if (isset($_authList[$uid.$t])) {
- return $_authList[$uid.$t];
- }
- if( $this->_config['auth_type']==2 && \think\Session::get('_auth_list_'.$uid.$t)){
- return \think\Session::get('_auth_list_'.$uid.$t);
- }
-
- $groups = $this->getGroups($uid);
- $ids = array();
- foreach ($groups as $g) {
- $ids = array_merge($ids, explode(',', trim($g['rules'], ',')));
- }
- $ids = array_unique($ids);
- if (empty($ids)) {
- $_authList[$uid.$t] = array();
- return array();
- }
- $map=array(
- 'id'=>array('in',$ids),
- 'type'=>$type,
- 'status'=>1,
- );
-
- $rules = \think\Db::table($this->_config['auth_rule'])->where($map)->field('condition,name')->select();
-
- $authList = array();
- foreach ($rules as $rule) {
- if (!empty($rule['condition'])) {
- $user = $this->getUserInfo($uid);
- $command = preg_replace('/\{(\w*?)\}/', '$user[\'\\1\']', $rule['condition']);
-
- @(eval('$condition=(' . $command . ');'));
- if ($condition) {
- $authList[] = strtolower($rule['name']);
- }
- } else {
-
- $authList[] = strtolower($rule['name']);
- }
- }
- $_authList[$uid.$t] = $authList;
- if($this->_config['auth_type']==2){
-
- \think\Session::set('_auth_list_'.$uid.$t, $authList);
- }
- return array_unique($authList);
- }
-
- protected function getUserInfo($uid) {
- static $userinfo=array();
- if(!isset($userinfo[$uid])){
- $userinfo[$uid]=\think\Db::table($this->_config['admin'])->where('id',$uid)->find();
- }
- return $userinfo[$uid];
- }
- }
|