您的位置:首页 > 编程语言 > PHP开发

[置顶] ThinkPHP5的权限控制Auth

2017-03-12 12:26 453 查看
ThinkPHP5使用Auth类首先我们需要建立3个数据表

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 '',
`type` tinyint(1) NOT NULL DEFAULT '1',
`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;
创建好后三个表后,我们需要使用一个验证方法就是以下的
在使用前我们需要引入

use think\Request;

$controller=Request::instance()->controller();
$module=Request::instance()->module();
$action=Request::instance()->action();
$url=$module.'/'.$controller.'/'.$action;

check($url, $uid);
其中$uid为登录用户的id

然后我们需要在配置文件上配置

$_config = array(
'auth_on'           => true,                      // 认证开关
'auth_type'         => 1,                         // 认证方式,1为实时认证;2为登录认证。
'auth_group'        => 'auth_group',        // 用户组数据表名
'auth_group_access' => 'auth_group_access', // 用户-用户组关系表
'auth_rule'         => 'auth_rule',         // 权限规则表
'auth_user'         => 'user'             // 用户信息表
);

差不多了现在就可以给数据库中插入数据了。Auth基本的就差不多了后面的都是增删改查的操作了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: