一,先创建数据表

1、think_auth_rule,规则表

id:主键,

name:规则唯一标识,

 title:规则中文名称 

status 状态:为1正常,为0禁用,

condition:规则表达式,为空表示存在就验证,不为空表示按照条件验证

[sql] view plain copy

  1. DROP TABLE IF EXISTS `think_auth_rule`;  

  2. CREATE TABLE `think_auth_rule` (    

  3.     `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,    

  4.     `name` char(80) NOT NULL DEFAULT '',    

  5.     `title` char(20) NOT NULL DEFAULT '',    

  6.     `type` tinyint(1) NOT NULL DEFAULT '1',      

  7.     `status` tinyint(1) NOT NULL DEFAULT '1',    

  8.     `condition` char(100) NOT NULL DEFAULT '',  # 规则附件条件,满足附加条件的规则,才认为是有效的规则  

  9.     PRIMARY KEY (`id`),    

  10.     UNIQUE KEY `name` (`name`)  

  11. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8;  

2、think_auth_group 用户组表
id:主键, 


title:用户组中文名称, 

rules:用户组拥有的规则id, 多个规则","隔开,

status 状态:为1正常,为0禁用

[sql] view plain copy

  1. DROP TABLE IF EXISTS `think_auth_group`;  

  2. CREATE TABLE `think_auth_group` (   

  3.     `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,   

  4.     `title` char(100) NOT NULL DEFAULT '',   

  5.     `status` tinyint(1) NOT NULL DEFAULT '1',   

  6.     `rules` char(80) NOT NULL DEFAULT '',   

  7.     PRIMARY KEY (`id`)  

  8. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8;  

3、think_auth_group_access 用户组明细表
uid:用户id,


group_id:用户组id

[sql] view plain copy

  1. DROP TABLE IF EXISTS `think_auth_group_access`;  

  2. CREATE TABLE `think_auth_group_access` (    

  3.     `uid` mediumint(8) unsigned NOT NULL,    

  4.     `group_id` mediumint(8) unsigned NOT NULL,   

  5.     UNIQUE KEY `uid_group_id` (`uid`,`group_id`),    

  6.     KEY `uid` (`uid`),   

  7.     KEY `group_id` (`group_id`)  

  8. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;  


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

[sql] view plain copy

  1. DROP TABLE IF EXISTS `think_admin`;  

  2. CREATE TABLE `think_admin` (  

  3.   `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '管理员ID',  

  4.   `username` varchar(255) DEFAULT NULL COMMENT '管理员账号',  

  5.   `password` varchar(32) DEFAULT NULL COMMENT '管理员密码',  

  6.   `ip` varchar(255) DEFAULT NULL COMMENT '最后登录IP地址',  

  7.   `login_time` int(11) DEFAULT NULL COMMENT '最后登录时间',  

  8.   `login_count` mediumint(8) NOT NULL COMMENT '登录次数',  

  9.   `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '账户状态,禁用为0   启用为1',  

  10.   `create_time` int(11) DEFAULT NULL COMMENT '创建时间',  

  11.   PRIMARY KEY (`id`)  

  12. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8;  

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

[sql] view plain copy

  1. DROP TABLE IF EXISTS `think_user`;  

  2. CREATE TABLE `think_user` (  

  3.   `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '管理员ID',  

  4.   `username` varchar(255) DEFAULT NULL COMMENT '管理员账号',  

  5.   `password` varchar(32) DEFAULT NULL COMMENT '管理员密码',  

  6.   `ip` varchar(255) DEFAULT NULL COMMENT '最后登录IP地址',  

  7.   `login_time` int(11) DEFAULT NULL COMMENT '最后登录时间',  

  8.   `login_count` mediumint(8) NOT NULL COMMENT '登录次数',  

  9.   `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '账户状态,禁用为0   启用为1',  

  10.   `create_time` int(11) DEFAULT NULL COMMENT '创建时间',  

  11.   PRIMARY KEY (`id`)  

  12. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8;  

[sql] view plain copy

  1. #便于测试,插入几条数据  

  2.   

  3. insert into think_user (`username`,`password`) values('zhangsan','123456');  

  4. insert into think_user (`username`,`password`) values('lisi','123456');  

  5. insert into think_user (`username`,`password`) values('wangwu','123456');  




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

[php] view plain copy

  1. 'AUTH_CONFIG'=>array(  

  2.         'AUTH_ON' => true, //认证开关  

  3.         'AUTH_TYPE' => 1, // 认证方式,1为时时认证;2为登录认证。  

  4.         'AUTH_GROUP' => 'think_auth_group', //用户组数据表名  

  5.         'AUTH_GROUP_ACCESS' => 'think_auth_group_access', //用户组明细表  

  6.         'AUTH_RULE' => 'think_auth_rule', //权限规则表  

  7.         'AUTH_USER' => 'think_admin'//用户信息表  

  8.     )  

补充:完整的sql


[sql] view plain copy

  1. # ************************************************************  

  2. # Sequel Pro SQL dump  

  3. # Version 4499  

  4. #  

  5. # http://www.sequelpro.com/  

  6. # https://github.com/sequelpro/sequelpro  

  7. #  

  8. # Host: localhost (MySQL 5.5.42)  

  9. # Database: thinkphp  

  10. # Generation Time: 2015-12-15 03:03:54 +0000  

  11. # ************************************************************  

  12.   

  13.   

  14. /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;  

  15. /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;  

  16. /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;  

  17. /*!40101 SET NAMES utf8 */;  

  18. /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;  

  19. /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;  

  20. /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;  

  21.   

  22.   

  23. # Dump of table think_admin  

  24. # ------------------------------------------------------------  

  25.   

  26. DROP TABLE IF EXISTS `think_admin`;  

  27.   

  28. CREATE TABLE `think_admin` (  

  29.   `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '管理员ID',  

  30.   `username` varchar(255) DEFAULT NULL COMMENT '管理员账号',  

  31.   `password` varchar(32) DEFAULT NULL COMMENT '管理员密码',  

  32.   `ip` varchar(255) DEFAULT NULL COMMENT '最后登录IP地址',  

  33.   `login_time` int(11) DEFAULT NULL COMMENT '最后登录时间',  

  34.   `login_count` mediumint(8) NOT NULL COMMENT '登录次数',  

  35.   `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '账户状态,禁用为0   启用为1',  

  36.   `create_time` int(11) DEFAULT NULL COMMENT '创建时间',  

  37.   PRIMARY KEY (`id`)  

  38. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;  

  39.   

  40. LOCK TABLES `think_admin` WRITE;  

  41. /*!40000 ALTER TABLE `think_admin` DISABLE KEYS */;  

  42.   

  43. INSERT INTO `think_admin` (`id`, `username`, `password`, `ip`, `login_time`, `login_count`, `status`, `create_time`)  

  44. VALUES  

  45.     (1,'admin2','123456',NULL,NULL,0,1,NULL),  

  46.     (2,'admin1','123456',NULL,NULL,0,1,NULL),  

  47.     (3,'admin','123456',NULL,NULL,0,1,NULL);  

  48.   

  49. /*!40000 ALTER TABLE `think_admin` ENABLE KEYS */;  

  50. UNLOCK TABLES;  

  51.   

  52.   

  53. # Dump of table think_auth_group  

  54. # ------------------------------------------------------------  

  55.   

  56. DROP TABLE IF EXISTS `think_auth_group`;  

  57.   

  58. CREATE TABLE `think_auth_group` (  

  59.   `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,  

  60.   `title` char(100) NOT NULL DEFAULT '',  

  61.   `status` tinyint(1) NOT NULL DEFAULT '1',  

  62.   `rules` char(80) NOT NULL DEFAULT '',  

  63.   PRIMARY KEY (`id`)  

  64. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;  

  65.   

  66. LOCK TABLES `think_auth_group` WRITE;  

  67. /*!40000 ALTER TABLE `think_auth_group` DISABLE KEYS */;  

  68.   

  69. INSERT INTO `think_auth_group` (`id`, `title`, `status`, `rules`)  

  70. VALUES  

  71.     (1,'超级管理员',1,'1,2,3,4,5'),  

  72.     (2,'普通管理员',1,'4,5');  

  73.   

  74. /*!40000 ALTER TABLE `think_auth_group` ENABLE KEYS */;  

  75. UNLOCK TABLES;  

  76.   

  77.   

  78. # Dump of table think_auth_group_access  

  79. # ------------------------------------------------------------  

  80.   

  81. DROP TABLE IF EXISTS `think_auth_group_access`;  

  82.   

  83. CREATE TABLE `think_auth_group_access` (  

  84.   `uid` mediumint(8) unsigned NOT NULL,  

  85.   `group_id` mediumint(8) unsigned NOT NULL,  

  86.   UNIQUE KEY `uid_group_id` (`uid`,`group_id`),  

  87.   KEY `uid` (`uid`),  

  88.   KEY `group_id` (`group_id`)  

  89. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;  

  90.   

  91. LOCK TABLES `think_auth_group_access` WRITE;  

  92. /*!40000 ALTER TABLE `think_auth_group_access` DISABLE KEYS */;  

  93.   

  94. INSERT INTO `think_auth_group_access` (`uid`, `group_id`)  

  95. VALUES  

  96.     (1,2),  

  97.     (2,2),  

  98.     (3,1);  

  99.   

  100. /*!40000 ALTER TABLE `think_auth_group_access` ENABLE KEYS */;  

  101. UNLOCK TABLES;  

  102.   

  103.   

  104. # Dump of table think_auth_rule  

  105. # ------------------------------------------------------------  

  106.   

  107. DROP TABLE IF EXISTS `think_auth_rule`;  

  108.   

  109. CREATE TABLE `think_auth_rule` (  

  110.   `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,  

  111.   `name` char(80) NOT NULL DEFAULT '',  

  112.   `title` char(20) NOT NULL DEFAULT '',  

  113.   `type` tinyint(1) NOT NULL DEFAULT '1',  

  114.   `status` tinyint(1) NOT NULL DEFAULT '1',  

  115.   `condition` char(100) NOT NULL DEFAULT '',  

  116.   PRIMARY KEY (`id`),  

  117.   UNIQUE KEY `name` (`name`)  

  118. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;  

  119.   

  120. LOCK TABLES `think_auth_rule` WRITE;  

  121. /*!40000 ALTER TABLE `think_auth_rule` DISABLE KEYS */;  

  122.   

  123. INSERT INTO `think_auth_rule` (`id`, `name`, `title`, `type`, `status`, `condition`)  

  124. VALUES  

  125.     (1,'Admin/admin/role','角色管理',1,1,''),  

  126.     (2,'Admin/admin/index','管理员列表',1,1,''),  

  127.     (3,'Admin/Member/edit','会员信息修改',1,1,''),  

  128.     (4,'Admin/Member/index','会员列表',1,1,''),  

  129.     (5,'Admin/Member/show','单个会员信息查看',1,1,'');  

  130.   

  131. /*!40000 ALTER TABLE `think_auth_rule` ENABLE KEYS */;  

  132. UNLOCK TABLES;  

  133.   

  134.   

  135. # Dump of table think_user  

  136. # ------------------------------------------------------------  

  137.   

  138. DROP TABLE IF EXISTS `think_user`;  

  139.   

  140. CREATE TABLE `think_user` (  

  141.   `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '会员ID',  

  142.   `username` varchar(255) DEFAULT NULL COMMENT '会员账号',  

  143.   `password` varchar(32) DEFAULT NULL COMMENT '会员密码',  

  144.   `ip` varchar(255) DEFAULT NULL COMMENT '最后登录IP地址',  

  145.   `login_time` int(11) DEFAULT NULL COMMENT '最后登录时间',  

  146.   `login_count` mediumint(8) NOT NULL COMMENT '登录次数',  

  147.   `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '账户状态,禁用为0   启用为1',  

  148.   `create_time` int(11) DEFAULT NULL COMMENT '创建时间',  

  149.   PRIMARY KEY (`id`)  

  150. ) ENGINE=MyISAM DEFAULT CHARSET=utf8;  

  151.   

  152. LOCK TABLES `think_user` WRITE;  

  153. /*!40000 ALTER TABLE `think_user` DISABLE KEYS */;  

  154.   

  155. INSERT INTO `think_user` (`id`, `username`, `password`, `ip`, `login_time`, `login_count`, `status`, `create_time`)  

  156. VALUES  

  157.     (1,'wangwu','123456',NULL,NULL,0,1,NULL),  

  158.     (2,'lisi','123456',NULL,NULL,0,1,NULL),  

  159.     (3,'wangwu','123456',NULL,NULL,0,1,NULL),  

  160.     (4,'zhangsan','123456',NULL,NULL,0,1,NULL);  

  161.   

  162. /*!40000 ALTER TABLE `think_user` ENABLE KEYS */;  

  163. UNLOCK TABLES;  

  164.   

  165.   

  166.   

  167. /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;  

  168. /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;  

  169. /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;  

  170. /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;  

  171. /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;  

  172. /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;  


使用:


在某个控制的方法里:


[php] view plain copy

  1. //会员信息编辑页面展示  

  2.     public function edit(){  

  3.         //  

  4.         session('uid','3');  //设置session;  

  5.   

  6.   

  7.         //下面代码动态判断权限  

  8.         $auth = new Auth();  

  9.   

  10.         //var_dump($auth->getGroups(1));//获得用户所属的所有用户组  

  11.   

  12.         if(!$auth->check(MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME,session('uid'))){  

  13.             echo '没有权限';  

  14.         }else{  

  15.             echo '有权限';  

  16.             //todo...  

  17.         }  

  18.   

  19.   

  20.         $this->display('add');  

  21.     }  


也可以写个公共控制器:



[php] view plain copy

  1. <?php  

  2. namespace Admin\Controller;  

  3. use Think\Controller;  

  4. use Think\Auth;  

  5.   

  6. //公共的权限认证控制器  

  7. class AuthController extends Controller {  

  8.     protected function _initialize(){  

  9.         //session不存在时,不允许直接访问  

  10.         if(!session('aid')){  

  11.             $this->error('还没有登录,正在跳转到登录页',U('Public/login'));  

  12.         }  

  13.   

  14.         //session存在时,不需要验证的权限  

  15.         $not_check = array('Index/clear/cache',  

  16.             'Index/edit/pwd','Index/logout','Admin/admin_list',  

  17.             'Admin/admin/list','Admin/admin/edit','Admin/admin/add');  

  18.           

  19.         //当前操作的请求                 模块名/方法名  

  20.         if(in_array(MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME, $not_check)){  

  21.             return true;  

  22.         }  

  23.           

  24.         //下面代码动态判断权限  

  25.         $auth = new Auth();  

  26.         if(!$auth->check(MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME,session('aid')) && session('aid') != 1){  

  27.             $this->error('没有权限');  

  28.         }  

  29.     }  

  30. }