Home > php教程 > php手册 > body text

Authority.class.php 修改待验证的权限$name如果权限列表里面不

WBOY
Release: 2016-06-06 19:35:14
Original
853 people have browsed it

ThinkPHP框架中自带权限认证类Authority.class.php。要把所有操作都加入到权限表这工作量太那啥了. 能实现仅把需要验证的操作加入权限表,如果待验证的权限在权限列表中不存在(总列表)则默认有该权限(已登录)。 虽然代码写的简单,不过还是挺实用。 ThinkPHP //

ThinkPHP框架中自带权限认证类 Authority.class.php。要把所有操作都加入到权限表这工作量太那啥了.
能实现仅把需要验证的操作加入权限表,如果待验证的权限在权限列表中不存在(总列表)则默认有该权限(已登录)。
虽然代码写的简单,不过还是挺实用。 ThinkPHP
    //获得权限$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
Related labels:
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!