As a B/S-based management system, what users can perform and what operations is indeed a big problem. I am lucky to be led by the team lead, if you are involved in RBAC (Role-BasedAccessControl), you must Mark it. (the following content requires thinkphp framework knowledge)
As a B/S-based management system, what users can perform and what operations is indeed a big problem. I am lucky to be led by the team lead, if you are involved in Role-Based Access Control, you must Mark it. (the following content requires thinkphp framework knowledge)
No. 1 Database Design (only extract the relevant part)
User table: records user information, including user name, password, and description (omitted)
Role table: records system roles, such as administrators, leaders, and employees.
Node Table: A node is a part of a URL. each page is spliced by nodes. Here, the pid is the last node of the node. Using the pid, you can splice the URLs of each page with one node. The thinkphp framework consists of the module name and method name, for example, http: // localhost/app/index. php/User/login. User is the module name, stored as a node, login is the method name, and stored as a node. The preceding http: // localhost/app/index. php is the thinkphp entry address and does not need to be recorded. (Thinkphp content can refer to the http://www.thinkphp.cn/document/155.html)
There are two associated tables in the middle to record the association.
No. 2 use "inherited extension model" and "_ initialize ()" to detect permissions of roles and nodes
Inherit extension model: see http://doc.thinkphp.cn/manual/model_extend.html
_ Initialize (): The system Action class provides an initialization method _ initialize interface, which can be used to expand the needs. the _ initialize method is executed first before all operation methods are called.
We extend an AccessManagerAction model to all modules that require permission verification. In this model, we compile a _ initialize method, in which:
1. obtain the URL to be accessed, even if the User + login http: // localhost/app/index. php/User/login is obtained in thinkphp
2. obtain user roles
3. check the URL and role, and return accessible or inaccessible information.
The functions in AccessManagerAction are as follows:
- Public function _ initialize ()
- {
- $ Node = MODULE_NAME.ACTION_NAME; // Obtain the name of the module to be accessed and the name of the operation method.
- $ Role = session ('roleid'); // Obtain the role of the user.
- If ($ this-> roleAccessFlow ($ node, $ role) // detection function
- True; // echo "-- verification passed ";
- Else
- $ This-> error ('No authorization ');
- }
- Public function roleAccessFlow ($ node = 'null', $ role = 'null ')
- {
- $ Result = false;
- $ Action = str_replace ("Action", "" ,__ CLASS __);
- $ Nodestatus =-1;
- // $ Nodestatus is associated with the ifdatapermit field of the node table.
- // There are two cases: $ nodestatus =-1. the node is not in the node table. $ Nodestatus = 0, in the node table
- // Check whether the administrator is running. If yes, return true. if not, next step.
- If ($ this-> checkSessionAdmin ())
- {
- // Echo "the administrator does not need to verify ";
- $ Result = true;
- }
- // Check whether the node is in the node table. if it is not in the description, no verification is required. return true. If yes, proceed to the next step.
- Else
- {
- // Echo "non-administrator user. ";
- $ Nodestatus = $ this-> NodeInList ($ node); // check whether the node is in the node Table function
- If ($ nodestatus =-1)
- {
- // Echo "this node is not in the list and does not require verification. ";
- $ Result = true;
- }
- Else if ($ nodestatus = 0)
- {
- // Echo "this node needs verification ";
- $ Result = $ this-> checkRoleNodeAccess ($ node, $ role );
- // CheckRoleNodeAccess mapping user's role and node information
- }
- }
- Return $ result;
- }
Note:A view is required to connect the module name and the method name to form a complete URL, which is then checked in the permission table.