Authority. class. PHP modify the permission to be verified $name if it is in the permission list

From , 4 Years ago, written in PHP, viewed 51 times.
URL https://pastebin.vip/view/8deb8d1d
  1.     //获得权限$name 可以是字符串或数组或逗号分割, uid为 认证的用户id, $or 是否为or关系,为true是, name为数组,只要数组中有一个条件通过则通过,如果为false需要全部条件通过。
  2.     //最后修改功能:待验证的权限$name如果权限列表里面不存在则默认有该权限
  3.     public function getAuth($name, $uid, $relation='or') {
  4.         if (!$this->_config['AUTH_ON'])
  5.             return true;
  6.         $authList = $this->getAuthList($uid);
  7.         if (is_string($name)) {
  8.             if (strpos($name, ',') !== false) {
  9.                 $name = explode(',', $name);
  10.             } else {
  11.                 $name = array($name);
  12.             }
  13.         }
  14.         //修改部分开始
  15.         foreach($name as $key=>$val){
  16.             if(!$this->isExistsRule($val)){
  17.                 unset($name[$key]);
  18.             }
  19.         }
  20.         if(count($name)==0){
  21.             return true;
  22.         }
  23.         //修改部分结束
  24.  
  25.         $list = array(); //有权限的name
  26.         foreach ($authList as $val) {
  27.             if (in_array($val, $name))
  28.                 $list[] = $val;
  29.         }
  30.         if ($relation=='or' and !empty($list)) {
  31.             return true;
  32.         }
  33.         $diff = array_diff($name, $list);
  34.         if ($relation=='and' and empty($diff)) {
  35.             return true;
  36.         }
  37.         return false;
  38.     }
  39.     /**
  40.      * @desc 判断数据库是否存在权限
  41.      * @param string $name RuleName
  42.      */
  43.     public function isExistsRule($name){
  44.         static $rule = array();
  45.         if(!empty($rule[$name])){
  46.             return $rule[$name];
  47.         }
  48.         $rule[$name] = M()->table($this->_config['AUTH_RULE'])->where(array('name'=>$name))->count();
  49.         return $rule[$name];
  50.     }

Reply to "Authority. class. PHP modify the permission to be verified $name if it is in the permission list"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website