thinkphp的auth权限设计

thinkphp权限这个功能需要4张表 分别为think_auth_rule,规则表,think_auth_group 用户组表,think_auth_group_access 用户组明细表以及用户表think_manager。

数据表命令:
//数据库
/*


– think_auth_rule,规则表,
– id:主键,name:规则唯一标识, title:规则中文名称 status 状态:为1正常,为0禁用,condition:规则表达式,为空表示存在就验证,不为空表示按照条件验证


DROP TABLE IF EXISTS think_auth_rule;
CREATE TABLE think_auth_rule (
id mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
name char(80) NOT NULL DEFAULT ‘’,
title char(20) NOT NULL DEFAULT ‘’,
status tinyint(1) NOT NULL DEFAULT ‘1’,
condition char(100) NOT NULL DEFAULT ‘’,
PRIMARY KEY (id),
UNIQUE KEY name (name)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


– think_auth_group 用户组表,
– id:主键, title:用户组中文名称, rules:用户组拥有的规则id, 多个规则","隔开,status 状态:为1正常,为0禁用


DROP TABLE IF EXISTS think_auth_group;
CREATE TABLE think_auth_group (
id mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
title char(100) NOT NULL DEFAULT ‘’,
status tinyint(1) NOT NULL DEFAULT ‘1’,
rules char(80) NOT NULL DEFAULT ‘’,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


– think_auth_group_access 用户组明细表
– uid:用户id,group_id:用户组id


DROP TABLE IF EXISTS think_auth_group_access;
CREATE TABLE think_auth_group_access (
uid mediumint(8) unsigned NOT NULL,
group_id mediumint(8) unsigned NOT NULL,
UNIQUE KEY uid_group_id (uid,group_id),
KEY uid (uid),
KEY group_id (group_id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

配置thinkphp的配置文件
‘AUTH_ON’ => true, //认证开关
‘AUTH_TYPE’ => 1, // 认证方式,1为时时认证;2为登录认证。
‘AUTH_GROUP’ => ‘think_auth_group’, //用户组数据表名
‘AUTH_GROUP_ACCESS’ => ‘think_auth_group_access’, //用户组明细表
‘AUTH_RULE’ => ‘think_auth_rule’, //权限规则表
‘AUTH_USER’ => ‘think_manager’//用户信息表

接下来就编写对应模块的增删改查的方法
用户组
权限列表
管理员列表
用户组编辑权限列表

接下来在后台公共控制器里的_initialize()方法里面添加
import(‘ORG.Util.Auth’);
$auth = new Auth();
n a m e = s t r t o l o w e r ( M O D U L E N A M E . / . A C T I O N N A M E ) ; i f ( name = strtolower(MODULE_NAME.'/'.ACTION_NAME); if( name == ‘admin/login’ || $name == ‘admin/index/index’) {
$res = true;
}else{
$res = a u t h > c h e c k ( auth->check( name,session(‘manager_id’));
}
if(!$res){
$this->error(“没有权限”);
}
测试成功

这是整个auth类的内容


class Auth{

    //默认配置
    protected $_config = array(
        'AUTH_ON' => true, //认证开关
        'AUTH_TYPE' => 1, // 认证方式,1为时时认证;2为登录认证。
        'AUTH_GROUP' => 'apc_auth_group', //用户组数据表名
        'AUTH_GROUP_ACCESS' => 'apc_auth_group_access', //用户组明细表
        'AUTH_RULE' => 'apc_auth_rule', //权限规则表
        'AUTH_USER' => 'apc_manager'//用户信息表
    );

    public function __construct() {
        if (C('AUTH_CONFIG')) {
            //可设置配置项 AUTH_CONFIG, 此配置项为数组。
            $this->_config = array_merge($this->_config, C('AUTH_CONFIG'));
        }
    }

    //获得权限$name 可以是字符串或数组或逗号分割, uid为 认证的用户id, $or 是否为or关系,为true是, name为数组,只要数组中有一个条件通过则通过,如果为false需要全部条件通过。
    public function check($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);
            }
        }
        $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;
    }

    //获得用户组,外部也可以调用
    public function getGroups($uid) {
        static $groups = array();
        if (isset($groups[$uid]))
            return $groups[$uid];
        $user_groups = M()->table($this->_config['AUTH_GROUP_ACCESS'] . ' a')->where("a.uid='$uid' and g.status='1'")->join($this->_config['AUTH_GROUP']." g on a.group_id=g.id")->select();
        $groups[$uid]=$user_groups?$user_groups:array();
        return $groups[$uid];
    }

    //获得权限列表
    protected function getAuthList($uid=3) {
        static $_authList = array();
        if (isset($_authList[$uid])) {
            return $_authList[$uid];
        }
        if(isset($_SESSION['_AUTH_LIST_'.$uid])){
            return $_SESSION['_AUTH_LIST_'.$uid];
        }
        //读取用户所属用户组
        $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] = array();
            return array();
        }
        //读取用户组所有权限规则
        $map=array(
            'id'=>array('in',$ids),
            'status'=>1
        );
        $rules = M()->table($this->_config['AUTH_RULE'])->where($map)->select();
        //循环规则,判断结果。
        $authList = array();
        foreach ($rules as $r) {
            if (!empty($r['condition'])) {
                //条件验证
                $user = $this->getUserInfo($uid);
                $command = preg_replace('/\{(\w*?)\}/', '$user[\'\\1\']', $r['condition']);
                //dump($command);//debug
                @(eval('$condition=(' . $command . ');'));
                if ($condition) {
                    $authList[] = $r['name'];
                }
            } else {
                //存在就通过
                $authList[] = $r['name'];
            }
        }
        $_authList[$uid] = $authList;
        if($this->_config['AUTH_TYPE']==2){
            //session结果
            $_SESSION['_AUTH_LIST_'.$uid]=$authList;
        }
        return $authList;
    }
    //获得用户资料,根据自己的情况读取数据库
    protected function getUserInfo($uid) {
        static $userinfo=array();
        if(!isset($userinfo[$uid])){
             $userinfo[$uid]=M()->table($this->_config['AUTH_USER'])->find($uid);
        }
        return $userinfo[$uid];
    }

}


猜你喜欢

转载自blog.csdn.net/lxy_hegh/article/details/88643327