Home > php教程 > PHP源码 > Authority.class.php 修改待验证的权限$name如果权限列表里面不存在则默认有该权限

Authority.class.php 修改待验证的权限$name如果权限列表里面不存在则默认有该权限

PHP中文网
Release: 2016-05-26 08:21:11
Original
1339 people have browsed it

php代码

//获得权限$name 可以是字符串或数组或逗号分割, uid为 认证的用户id, $or 是否为or关系,为true是, name为数组,只要数组中有一个条件通过则通过,如果为false需要全部条件通过。
    //最后修改功能:待验证的权限$name如果权限列表里面不存在则默认有该权限
    public function getAuth($name, $uid, $relation='or') {
        if (!$this->_config['AUTH_ON'])
            return true;
        $authList = $this->getAuthList($uid);
        if (is_string($name)) {
            if (strpos($name, ',') !== false) {
                $name = explode(',', $name);
            } else {
                $name = array($name);
            }
        }
        //修改部分开始
        foreach($name as $key=>$val){
            if(!$this->isExistsRule($val)){
                unset($name[$key]);
            }
        }
        if(count($name)==0){
            return true;
        }
        //修改部分结束

        $list = array(); //有权限的name
        foreach ($authList as $val) {
            if (in_array($val, $name))
                $list[] = $val;
        }
        if ($relation=='or' and !empty($list)) {
            return true;
        }
        $diff = array_diff($name, $list);
        if ($relation=='and' and empty($diff)) {
            return true;
        }
        return false;
    }
    /**
     * @desc 判断数据库是否存在权限
     * @param string $name RuleName
     */
    public function isExistsRule($name){
        static $rule = array();
        if(!empty($rule[$name])){
            return $rule[$name];
        }
        $rule[$name] = M()->table($this->_config['AUTH_RULE'])->where(array('name'=>$name))->count();
        return $rule[$name];
    }
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template