ThinkPHP 3.0 + Rbac 應用示例

      此爲Rbac在ThinkPHP中應用的實例,用到了ThinkPHP的Rbac擴展,樣式比較難看,你們將就的看。此例子旨在學習交流使用,勿用於實際項目中。 php

      Rbac簡單說來就是基於「角色」的權限控制,全部用戶都屬於某一用戶組,以後給用戶組受權,則組內成員都有相應權限。而ThinkPHP的Rbac的核心,即爲在每一個操做前都進行權限驗證操做,用_initialize方法實現。而權限位則在登陸時寫到session中,若是對權限的實時性要求很是高,也能夠設置每一次驗證都到數據庫裏查。 node

      程序源碼: ajax

      http://vdisk.weibo.com/s/fOMnN sql

     相關配置項: 數據庫

'USER_AUTH_ON' => true,//開啓驗證
  'USER_AUTH_TYPE' => 1,//驗證類型
  'USER_AUTH_KEY' => 'uid',
  // REQUIRE_AUTH_MODULE  須要認證模塊,不設置即爲除了NOT_AUTH_MODULE中的模塊外所有驗證
  'NOT_AUTH_MODULE' => 'Public',
  'USER_AUTH_GATEWAY' => '/Public/login', //認證網關
  // RBAC_DB_DSN  數據庫鏈接DSN,默認使用配置文件
  'RBAC_ROLE_TABLE' => 'think_role', //角色表名稱
  'RBAC_USER_TABLE' => 'think_role_user', //用戶表名稱
  'RBAC_ACCESS_TABLE' => 'think_access', //權限表名稱
  'RBAC_NODE_TABLE' => 'think_node', //節點表名稱
  'USER_AUTH_MODEL' => 'User',
  'AUTH_PWD_ENCODER' => 'md5',
  'GUEST_AUTH_ON' => false,
  'ADMIN_AUTH_KEY' => 'administrator',//管理員標識

     認證方法: session

public function _initialize(){
     // 用戶權限檢查
        if (C('USER_AUTH_ON') && !in_array(MODULE_NAME, explode(',', C('NOT_AUTH_MODULE')))) {
            import('ORG.Util.RBAC');
            if (!RBAC::AccessDecision()) {
                //檢查認證識別號
                if (!$_SESSION [C('USER_AUTH_KEY')]) {
                    //跳轉到認證網關
                    redirect(PHP_FILE . C('USER_AUTH_GATEWAY'));
                }
                // 沒有權限 拋出錯誤
                if (C('RBAC_ERROR_PAGE')) {
                    // 定義權限錯誤頁面
                    redirect(C('RBAC_ERROR_PAGE'));
                } else {
                    if (C('GUEST_AUTH_ON')) {
                        $this->assign('jumpUrl', PHP_FILE . C('USER_AUTH_GATEWAY'));
                    }
                    // 提示錯誤信息
                    $this->error(L('_VALID_ACCESS_'));
                }
            }
        }
  }

     程序截圖以下: 學習

SQL: ui

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `think_access`
-- ----------------------------
DROP TABLE IF EXISTS `think_access`;
CREATE TABLE `think_access` (
  `role_id` smallint(6) unsigned NOT NULL,
  `node_id` smallint(6) unsigned NOT NULL,
  `pid` smallint(6) NOT NULL DEFAULT '0',
  `level` tinyint(1) NOT NULL,
  `module` varchar(50) DEFAULT NULL,
  KEY `groupId` (`role_id`),
  KEY `nodeId` (`node_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of think_access
-- ----------------------------
INSERT INTO `think_access` VALUES ('62', '112', '103', '3', null);
INSERT INTO `think_access` VALUES ('62', '109', '103', '3', null);
INSERT INTO `think_access` VALUES ('62', '108', '103', '3', null);
INSERT INTO `think_access` VALUES ('62', '107', '103', '3', null);
INSERT INTO `think_access` VALUES ('62', '106', '103', '3', null);
INSERT INTO `think_access` VALUES ('62', '105', '103', '3', null);
INSERT INTO `think_access` VALUES ('62', '104', '103', '3', null);
INSERT INTO `think_access` VALUES ('62', '103', '102', '2', null);
INSERT INTO `think_access` VALUES ('62', '102', '0', '1', null);

-- ----------------------------
-- Table structure for `think_node`
-- ----------------------------
DROP TABLE IF EXISTS `think_node`;
CREATE TABLE `think_node` (
  `id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  `title` varchar(50) DEFAULT NULL,
  `status` tinyint(1) DEFAULT '0',
  `remark` varchar(255) DEFAULT NULL,
  `sort` smallint(6) unsigned DEFAULT NULL,
  `pid` smallint(6) unsigned NOT NULL,
  `level` tinyint(1) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `level` (`level`),
  KEY `pid` (`pid`),
  KEY `status` (`status`),
  KEY `name` (`name`)
) ENGINE=MyISAM AUTO_INCREMENT=113 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of think_node
-- ----------------------------
INSERT INTO `think_node` VALUES ('102', 'Index', '首頁', '1', '', '0', '0', '1');
INSERT INTO `think_node` VALUES ('103', 'Index', '首頁模塊', '1', '', '0', '102', '2');
INSERT INTO `think_node` VALUES ('104', 'index', '首頁', '1', '', '0', '103', '3');
INSERT INTO `think_node` VALUES ('105', 'left', '左側模板', '1', '', '0', '103', '3');
INSERT INTO `think_node` VALUES ('106', 'main', '右側模板', '1', '', '0', '103', '3');
INSERT INTO `think_node` VALUES ('107', 'header', '頭部模板', '1', '', '0', '103', '3');
INSERT INTO `think_node` VALUES ('108', 'add', '用戶管理', '1', '', '0', '103', '3');
INSERT INTO `think_node` VALUES ('109', 'addUser', '新增用戶', '1', '', '0', '103', '3');
INSERT INTO `think_node` VALUES ('112', 'ajaxuser', '獲取用戶組', '1', '', '0', '103', '3');

-- ----------------------------
-- Table structure for `think_role`
-- ----------------------------
DROP TABLE IF EXISTS `think_role`;
CREATE TABLE `think_role` (
  `id` smallint(6) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  `pid` smallint(6) DEFAULT NULL,
  `status` tinyint(1) unsigned DEFAULT NULL,
  `remark` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `pid` (`pid`),
  KEY `status` (`status`)
) ENGINE=MyISAM AUTO_INCREMENT=63 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of think_role
-- ----------------------------
INSERT INTO `think_role` VALUES ('62', '用戶管理組', '0', '1', '這是用戶管理組,只能新增用戶');

-- ----------------------------
-- Table structure for `think_role_user`
-- ----------------------------
DROP TABLE IF EXISTS `think_role_user`;
CREATE TABLE `think_role_user` (
  `role_id` mediumint(9) unsigned DEFAULT NULL,
  `user_id` char(32) DEFAULT NULL,
  KEY `group_id` (`role_id`),
  KEY `user_id` (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of think_role_user
-- ----------------------------
INSERT INTO `think_role_user` VALUES ('62', '112');

-- ----------------------------
-- Table structure for `think_user`
-- ----------------------------
DROP TABLE IF EXISTS `think_user`;
CREATE TABLE `think_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `password` char(32) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=122 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of think_user
-- ----------------------------
INSERT INTO `think_user` VALUES ('2', 'admin', '21232f297a57a5a743894a0e4a801fc3');
INSERT INTO `think_user` VALUES ('112', 'test', '098f6bcd4621d373cade4e832627b4f6');

歡迎交流討論 this

相關文章
相關標籤/搜索