thinkphp-权限控制
1.在 Admin 模块下创建一个 IndexController.class.php(默认就有了)
<?php
namespace Home2\Controller;
use Common\Controller\AuthController;
class IndexController extends AuthController {
public function index(){
echo "后台首页(home2)";
}
}
?>
2.在baidu根目录下的 Common 公共模块下创建 Controller 文件夹,并在里面创建一个 AuthController.class.php 类,这个类用于权限控制。
<?php
namespace Common\Controller;
use Think\Controller;
use Think\Auth;
class AuthController extends Controller {
protected function _initialize() {
$sess_auth=session('auth');
if(!$sess_auth){
$this->error('非法访问:跳登录页',U('Login/index'));
}
//超级会员,不用验证权限
if($sess_auth['uid']==1){
return true;
}
//权限判断
$auth = new Auth();
//'Home2/Index/index' //放权的id
if(!$auth->check(MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME,$sess_auth['uid'])) {
$this->error('没有权限',U('Login/index'));
}
}
}
?>
3.后台Home2里创建一个 LoginController.class.php,模版为 index.html。
(模块) LoginController.class.php (模板)index.html
<?php <body>
namespace Home2\Controller; <form method="post" action="{:U('Login/index')}">
use Think\Controller; <p>用户名:<input type="text" name="user"></p>
class LoginController extends Controller{ <p><input type="submit" name="登录"></p>
public function index(){ </form>
if(IS_POST){ </body>
$login=array();
switch(I('user',null,false)){
case 'admin':
$login['uid']=1;
$login['user']='admin';
break;
case 'test':
$login['uid']=2;
$login['user']='test';
break;
case 'guest':
$login['uid']=3;
$login['user']='guest';
break;
default:
$this->error('不存在此用户');
}
if(count($login)){
session('auth',$login);
$this->success('登录成功',U('Index/index'));
}
}
else{
$this->display();
}
}
}
?>
4.数据库中的表
可以在ThinkPHP/Library/Think/下的
Auth.class.php中查看权限用法与表创建
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;
直接将以上复制至数据库中即可创建三个表
think_auth_group 表名
id title status rules
用户id 用户名 状态(没什么) 权限id
1 默认管理组 1 1,2,3,4,5
think_auth_group_access 表名 //把权限1分配给用户3
uid group_id
用户id 权限id
3 1
think_auth_rule id name title type status condition
表名 权限id 权限地址 权限名 1 1 1