ThinkPHP的RBAC(基于角色权限控制)深入解析
一、什么是RBAC
基于角色的访问控制(Role-Based Access Control)作为传统访问控制(自主访问,强制访问)的有前景的代替受到广泛的关注。
在RBAC中,权限与角色相关联,用户通过成为适当角色的成员而得到这些角色的权限。这就极大地简化了权限的管理。
在一个组织中,角色是为了完成各种工作而创造,用户则依据它的责任和资格来被指派相应的角色,用户可以很容易地从一个角色被指派到另一个角色。角色可依新的需求和系统的合并而赋予新的权限,而权限也可根据需要而从某角色中回收。角色与角色的关系可以建立起来以囊括更广泛的客观情况。
二、ThinkPHP中的RBAC
先看下官方给的实例所用到的数据表,通过5张表实现权限控制,定义如下:
RBAC 要用到5个数据表
think_user (用户表)
think_role (用户分组表)
think_node (操作节点)
think_role_user (用户和用户分组的对应)
think_access (各个操作和用户组的对应)
用户表
角色表,有哪些角色,该角色与对应的userid用户相关联
根据用户表的id给出对应的角色id相关联,也就是给用户分配角色,比如userid为3的角色为2,根据role角色表,7代表员工的角色
access表,权限表,比如角色id为2,也就是员工的权限,可以的对应的结点
结点表,代表有哪些应用-模块-模块方法,并且定义了之间的一种关系,比如noteid为30的是Public模块,noteid为31,32,33,34的方法add,insert,edit,update都属于Public。noteid为85的test方法,属于noteid为84的Game模块下的方法。
三、config配置文件详解
我们看看thinkphp官方示例中的config文件:
复制代码 代码如下:
array(
'APP_AUTOLOAD_PATH'=>'@.TagLib',
'SESSION_AUTO_START'=>true,
'USER_AUTH_ON' =>true,
'USER_AUTH_TYPE' =>1, // 默认认证类型 1 登录认证 2 实时认证
'USER_AUTH_KEY' =>'authId', // 用户认证SESSION标记
'ADMIN_AUTH_KEY' =>'administrator',
'USER_AUTH_MODEL' =>'User', // 默认验证数据表模型
'AUTH_PWD_ENCODER' =>'md5', // 用户认证密码加密方式
'USER_AUTH_GATEWAY' =>'/Public/login',// 默认认证网关
'NOT_AUTH_MODULE' =>'Public', // 默认无需认证模块
'REQUIRE_AUTH_MODULE' =>'', // 默认需要认证模块
'NOT_AUTH_ACTION' =>'', // 默认无需认证操作
'REQUIRE_AUTH_ACTION' =>'', // 默认需要认证操作
'GUEST_AUTH_ON' =>false, // 是否开启游客授权访问
'GUEST_AUTH_ID' =>0, // 游客的用户ID
'DB_LIKE_FIELDS' =>'title|remark',
'RBAC_ROLE_TABLE' =>'think_role',
'RBAC_USER_TABLE' =>'think_role_user',
'RBAC_ACCESS_TABLE' =>'think_access',
'RBAC_NODE_TABLE' =>'think_node',
'SHOW_PAGE_TRACE'=>1//显示调试信息
);
大家看注释就应该懂大半了,其中Public模块是无需认证的,道理很简单,没登录之前大家都是游客身份,如果登录页面也要权限,那从哪里登录呢?是吧,呵呵。默认网关地址就是认证失败,没有权限跳转到此处,重新登陆。ADMIN_AUTH_KEY表示超级管理员权限,如果你在user表建立一个名为admin的用户,那么这个用户就是超级管理员,不用给它分配权限,什么权限都有,为什么要设置一个这样的管理员,因为当你把权限分配错了容易引起系统权限混乱,搞得大家都访问不了,这时候超级管理员就来了。
四、RBAC类的几个重要的方法
authenticate($map,$model=”)方法 传入查询用户的条件和用户表的MODEL 返回数组包含用户的信息
saveAccessList($authId=null)方法 传入用户的ID 此方法不返回值,只是设置 $_SESSION['_ACCESS_LIST']的值,其中包含了所有该用户对应的用户组的有权限操作的所有节点 $_SESSION['_ACCESS_LIST']['项目名']['模块名']['操作名'],以后判断权限就是判断当前项目,模块和操作是否在 $_SESSION['_ACCESS_LIST']中能找到。
checkAccess() 方法 检测当前模块和操作是否需要验证 返回bool类型
checkLogin()方法 检测登录
AccessDecision($appName=APP_NAME) 方法 就是检测当前项目模块操作 是否在$_SESSION['_ACCESS_LIST']数组中,也就是说 在 $_SESSION['_ACCESS_LIST'] 数组中$_SESSION['_ACCESS_LIST']['当前操作']['当前模块']['当前操作']是否存在。如果存在表示有权限 否则返回flase。
getAccessList($authId) 方法 通过查询数据库 返回权限列表 $_SESSION['_ACCESS_LIST']的值了。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











It allows users to perform more in-depth operations and customization of the system. Root permission is an administrator permission in the Android system. Obtaining root privileges usually requires a series of tedious steps, which may not be very friendly to ordinary users, however. By enabling root permissions with one click, this article will introduce a simple and effective method to help users easily obtain system permissions. Understand the importance and risks of root permissions and have greater freedom. Root permissions allow users to fully control the mobile phone system. Strengthen security controls, customize themes, and users can delete pre-installed applications. For example, accidentally deleting system files causing system crashes, excessive use of root privileges, and inadvertent installation of malware are also risky, however. Before using root privileges

Blue Wings Chaos Effect features a diverse cast of characters, each with a unique identity and backstory. For this reason, the editor has specially compiled an introduction to the characters of BlazBlue Chaos Effect for all players. How many characters are there in BlazBlue Chaos Effect? Answer: There are 7 characters. 1. [God of Death] Ragnar Chad Bradedge (nicknamed RG, Nissan), his brother and sister were raised by church nuns. One day, one of the six heroes of the villain broke in, killed the nuns, and burned them down. Church, took his sister away, leaving behind his younger brother "The Weapon of Things" Ice Sword Snow Girl. 2. Noel Vermillion The adopted daughter of the Vermillion family looks almost the same as Ragnar's sister. After graduation, he joined the governing body as secretary to Ragnar's younger brother. 3. λ-11 is collectively known as Lambda and Eleventh Sister. After the original developer gave up, Kokonoe rescued and

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.
