RBAC(Role-Based Access Controll)基于角色的访问控制

在 ThinkPHP3.2.3 中 RBAC 类位于 /ThinkPHP/Library/Org/Util/Rbac.class.php

 

在后台管理模块中,每个用户都属于相应的角色组,例如用户 admin 属于超级管理员角色组,用户 dee 属于普通管理员角色组,用户 jane 属于销售角色组,用户 nicole 属于财务角色组,每个角色组拥有的权限都不同。用户和角色组属于多对多的关系,即一个用户可能属于多个角色组,一个角色组有多个用户。

所有模块(例如 Home、Admin)、控制器(Controller)、方法(Action)都是节点,角色组是否能够访问这些节点的信息即是该角色组的权限信息。角色组和节点也是多对多的关系,即一个角色组可以访问多个节点,多个角色组都有可以访问同一个节点。

即 Rbac 功能需要 5 张数据表:用户表、角色表、用户-角色中间表、节点表、角色-节点中间表(权限表)。在 Rbac.class.php 中系统已经给出了其中的 4 张表:角色表(role)、用户-角色中间表(role_user)、节点表(node)、权限表(access):

/*
-- --------------------------------------------------------
CREATE TABLE IF NOT EXISTS `think_access` (
  `role_id` smallint(6) unsigned NOT NULL,
  `node_id` smallint(6) unsigned NOT NULL,
  `level` tinyint(1) NOT NULL,
  `module` varchar(50) DEFAULT NULL,
  KEY `groupId` (`role_id`),
  KEY `nodeId` (`node_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `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  DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `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  DEFAULT CHARSET=utf8 ;

CREATE TABLE IF NOT EXISTS `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;
*/
4张表信息

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-03-07
  • 2021-09-03
  • 2022-12-23
  • 2021-12-18
  • 2021-12-18
猜你喜欢
  • 2021-09-19
  • 2021-05-23
  • 2022-12-23
  • 2021-05-18
  • 2021-06-25
  • 2021-06-02
  • 2022-12-23
相关资源
相似解决方案