<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                一,先創建數據表 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 '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; ``` 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 '1', `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 '1' 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 '1' 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','123456'); insert into think_user (`username`,`password`) values('lisi','123456'); insert into think_user (`username`,`password`) values('wangwu','123456'); 二,在使用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'//用戶信息表 ) ``` 補充:完整的sql ``` # ************************************************************ # Sequel Pro SQL dump # Version 4499 # # http://www.sequelpro.com/ # https://github.com/sequelpro/sequelpro # # Host: localhost (MySQL 5.5.42) # Database: thinkphp # Generation Time: 2015-12-15 03:03:54 +0000 # ************************************************************ /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; # Dump of table 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 '1' COMMENT '賬戶狀態,禁用為0 啟用為1', `create_time` int(11) DEFAULT NULL COMMENT '創建時間', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; LOCK TABLES `think_admin` WRITE; /*!40000 ALTER TABLE `think_admin` DISABLE KEYS */; INSERT INTO `think_admin` (`id`, `username`, `password`, `ip`, `login_time`, `login_count`, `status`, `create_time`) VALUES (1,'admin2','123456',NULL,NULL,0,1,NULL), (2,'admin1','123456',NULL,NULL,0,1,NULL), (3,'admin','123456',NULL,NULL,0,1,NULL); /*!40000 ALTER TABLE `think_admin` ENABLE KEYS */; UNLOCK TABLES; # Dump of table think_auth_group # ------------------------------------------------------------ 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; LOCK TABLES `think_auth_group` WRITE; /*!40000 ALTER TABLE `think_auth_group` DISABLE KEYS */; INSERT INTO `think_auth_group` (`id`, `title`, `status`, `rules`) VALUES (1,'超級管理員',1,'1,2,3,4,5'), (2,'普通管理員',1,'4,5'); /*!40000 ALTER TABLE `think_auth_group` ENABLE KEYS */; UNLOCK TABLES; # Dump of table think_auth_group_access # ------------------------------------------------------------ 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; LOCK TABLES `think_auth_group_access` WRITE; /*!40000 ALTER TABLE `think_auth_group_access` DISABLE KEYS */; INSERT INTO `think_auth_group_access` (`uid`, `group_id`) VALUES (1,2), (2,2), (3,1); /*!40000 ALTER TABLE `think_auth_group_access` ENABLE KEYS */; UNLOCK TABLES; # Dump of table think_auth_rule # ------------------------------------------------------------ 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; LOCK TABLES `think_auth_rule` WRITE; /*!40000 ALTER TABLE `think_auth_rule` DISABLE KEYS */; INSERT INTO `think_auth_rule` (`id`, `name`, `title`, `type`, `status`, `condition`) VALUES (1,'Admin/admin/role','角色管理',1,1,''), (2,'Admin/admin/index','管理員列表',1,1,''), (3,'Admin/Member/edit','會員信息修改',1,1,''), (4,'Admin/Member/index','會員列表',1,1,''), (5,'Admin/Member/show','單個會員信息查看',1,1,''); /*!40000 ALTER TABLE `think_auth_rule` ENABLE KEYS */; UNLOCK TABLES; # Dump of table 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 '1' COMMENT '賬戶狀態,禁用為0 啟用為1', `create_time` int(11) DEFAULT NULL COMMENT '創建時間', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; LOCK TABLES `think_user` WRITE; /*!40000 ALTER TABLE `think_user` DISABLE KEYS */; INSERT INTO `think_user` (`id`, `username`, `password`, `ip`, `login_time`, `login_count`, `status`, `create_time`) VALUES (1,'wangwu','123456',NULL,NULL,0,1,NULL), (2,'lisi','123456',NULL,NULL,0,1,NULL), (3,'wangwu','123456',NULL,NULL,0,1,NULL), (4,'zhangsan','123456',NULL,NULL,0,1,NULL); /*!40000 ALTER TABLE `think_user` ENABLE KEYS */; UNLOCK TABLES; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; ``` 使用: 在某個控制的方法里: ``` //會員信息編輯頁面展示 public function edit(){ // session('uid','3'); //設置session; //下面代碼動態判斷權限 $auth = new Auth(); //var_dump($auth->getGroups(1));//獲得用戶所屬的所有用戶組 if(!$auth->check(MODULE_NAME.'/'.CONTROLLER_NAME.'/'.ACTION_NAME,session('uid'))){ echo '沒有權限'; }else{ echo '有權限'; //todo... } $this->display('add'); } ``` 也可以寫個公共控制器: ``` <?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('沒有權限'); } } } ``` ———————————————— 版權聲明:本文為CSDN博主「學習筆記666」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。 原文鏈接:https://blog.csdn.net/github_26672553/article/details/50263051
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看