thinkphp-权限控制
1.在 Admin 模塊下創(chuàng)建一個 IndexController.class.php(默認就有了)
<?php
namespace Home2\Controller;
use Common\Controller\AuthController;
class IndexController extends AuthController {
public function index(){
echo "后臺首頁(home2)";
}
}
?>
?
2.在baidu根目錄下的 Common 公共模塊下創(chuàng)建 Controller 文件夾,并在里面創(chuàng)建一個 AuthController.class.php 類,這個類用于權(quán)限控制。
<?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'));
}
//超級會員,不用驗證權(quán)限
if($sess_auth['uid']==1){
return true;
}
//權(quán)限判斷
$auth = new Auth();
//'Home2/Index/index' //放權(quán)的id
if(!$auth->check(MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME,$sess_auth['uid'])) {
$this->error('沒有權(quán)限',U('Login/index'));
}
}
}
?>
?
3.后臺Home2里創(chuàng)建一個 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.數(shù)據(jù)庫中的表
可以在ThinkPHP/Library/Think/下的
Auth.class.php中查看權(quán)限用法與表創(chuàng)建
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 '', # 規(guī)則附件條件,滿足附加條件的規(guī)則,才認為是有效的規(guī)則
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- ----------------------------
-- think_auth_group 用戶組表,
-- id:主鍵, title:用戶組中文名稱, rules:用戶組擁有的規(guī)則id, 多個規(guī)則","隔開,status 狀態(tài):為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;
直接將以上復(fù)制至數(shù)據(jù)庫中即可創(chuàng)建三個表
think_auth_group 表名 ?
? ? ? ?id ? ? ? ? ? title ? ? ? ? status ? ? ? ? rules
用戶id ? ?用戶名 ? ? 狀態(tài)(沒什么) ?權(quán)限id
? ? ?1 ? ? ? 默認管理組 ? ? ? 1 ? ? ? ? ? ?1,2,3,4,5
?
think_auth_group_access ? ?表名 //把權(quán)限1分配給用戶3
? ? ???uid ? ? ? ?group_id
? ? ? ? 用戶id 權(quán)限id
?3 1
?
think_auth_rule ? ? id ? ? ? ?name ? ? ? ? title ? ? ? type ? ? ?status ? ? ?condition
表名 權(quán)限id ? 權(quán)限地址 ? ? 權(quán)限名 ? ? ? ?1 ? ? ? ? ? 1 ? ? ? ? ? ? ? ? 1
?
轉(zhuǎn)載于:https://www.cnblogs.com/yjh1604600160/p/think20.html
總結(jié)
以上是生活随笔為你收集整理的thinkphp-权限控制的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 闭包(匿名函数) php
- 下一篇: 查看php-fpm 占用内存情况