ThinkPHP3.2.3完整版中对Auth.class.php的使用

时间:2024-04-21 14:06:34

一,先创建数据表

1、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 '',
`type` tinyint(1) NOT NULL DEFAULT '',
`status` tinyint(1) NOT NULL DEFAULT '',
`condition` char(100) NOT NULL DEFAULT '', # 规则附件条件,满足附加条件的规则,才认为是有效的规则
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

2、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 '',
`rules` char(80) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

3、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;

4.既然是对后台管理员权限认证,所以还需要创建后台管理员表think_admin

DROP TABLE IF EXISTS `think_admin`;
CREATE TABLE `think_admin` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '管理员ID',
`username` varchar(255) DEFAULT NULL COMMENT '管理员账号',
`password` varchar(32) DEFAULT NULL COMMENT '管理员密码',
`ip` varchar(255) DEFAULT NULL COMMENT '最后登录IP地址',
`login_time` int(11) DEFAULT NULL COMMENT '最后登录时间',
`login_count` mediumint(8) NOT NULL COMMENT '登录次数',
`status` tinyint(1) NOT NULL DEFAULT '' COMMENT '账户状态,禁用为0 启用为1',
`create_time` int(11) DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

5.创建一张网站会员用户表think_user,权限认证(后台管理员对用户表的增删改查的权限)

DROP TABLE IF EXISTS `think_user`;
CREATE TABLE `think_user` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '管理员ID',
`username` varchar(255) DEFAULT NULL COMMENT '管理员账号',
`password` varchar(32) DEFAULT NULL COMMENT '管理员密码',
`ip` varchar(255) DEFAULT NULL COMMENT '最后登录IP地址',
`login_time` int(11) DEFAULT NULL COMMENT '最后登录时间',
`login_count` mediumint(8) NOT NULL COMMENT '登录次数',
`status` tinyint(1) NOT NULL DEFAULT '' COMMENT '账户状态,禁用为0 启用为1',
`create_time` int(11) DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
#便于测试,插入几条数据

insert into think_user (`username`,`password`) values('zhangsan','');
insert into think_user (`username`,`password`) values('lisi','');
insert into think_user (`username`,`password`) values('wangwu','');

二,在使用Auth类前需要配置config.PHP

'AUTH_CONFIG'=>array(
'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_admin'//用户信息表
)

写个公共控制器:

<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Auth; //公共的权限认证控制器
class AuthController extends Controller {
protected function _initialize(){
//session不存在时,不允许直接访问
if(!session('aid')){
$this->error('还没有登录,正在跳转到登录页',U('Public/login'));
} //session存在时,不需要验证的权限
$not_check = array('Index/clear/cache',
'Index/edit/pwd','Index/logout','Admin/admin_list',
'Admin/admin/list','Admin/admin/edit','Admin/admin/add'); //当前操作的请求 模块名/方法名
if(in_array(MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME, $not_check)){
return true;
} //下面代码动态判断权限
$auth = new Auth();
if(!$auth->check(MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME,session('aid')) && session('aid') != 1){
$this->error('没有权限');
}
}
}