Analysis on the usage of RBAC in ThinkPHP

Source: Internet
Author: User
This article mainly introduces four RBAC usage methods in ThinkPHP, and analyzes common usage of logon controllers and public controllers in detail, which has some reference value, you can refer to the following example to describe the usage of RBAC in ThinkPHP. Share it with you for your reference. The specific method is as follows:

Type 1: put it in the login operation of the login controller

1. RBAC: authenticate ();

The data used to search for the username submitted by the form in the user table is essentially a user table query statement:

The code is as follows:

Return M (modle)-> where (array)-> find ();

This operation has two parameters

A. The syntax and function of array () is the same as that of table search array:

The code is as follows:

Array ('field' => 'value', 'field' => array ('condition', 'value '));

B. The model is the table name. The default value is the configuration parameter.

The code is as follows:

C ('User _ AUTH_MODEL ');

The returned value is a one-dimensional array.
Note: it is a single record query method for user tables. we can directly use a query statement without it.

2. RBAC: saveAccessList ();

Write the application name (group name), controller name, and operation name that you can manipulate into the session in a three-dimensional array.

The parameter is the user id. generally, after the user login verification is passed, the user ID will be written to

The code is as follows:

C ('User _ AUTH_KEY ');

In this method, $ _ SESSION (C ('User _ AUTH_KEY ') is used by default;

Class 2: Put them in the public controller (all the controller classes that participate in permission verification must follow this class)

3. RBAC: accessdemo ();

This parameter is used to determine whether the current user has permissions for the current operation. the default parameter is APP_NAME. if the parameter is set to group mode, the group name GROUP_NAME must be input.

A method is called.

The code is as follows:

RBAC: checkAccess ();

Used to verify whether the current controller or operation participates in this judgment.

Note: There are four preparation parameters. we need to write two parameters.

Verification controller: REQUIRE_AUTH_MODULE verification operation: REQUIRE_AUTH_ACTION

No verification controller required: NOT_AUTH_MODULE no verification operation required: NOT_AUTH_ACTION

If full write is required: the controller must be written in REQUIRE_AUTH_ACTION at the same time.

If you do not need to write all: if the controller is written in NOT_AUTH_MODULE, all methods in the controller do not need to be verified.

If you write the operation in NOT_AUTH_ACTION, you must pay attention to the problem of duplicate operation names.

4. RBAC: checkLogin ();

Used to determine whether a user logs in.
Note: after logging on to the homepage, this operation must be verified. Therefore, each role must be authorized to perform this operation.

You can also use this method to directly determine whether $ _ SESSION [C ('User _ AUTH_KEY ')] exists, if it does not exist, you can jump to the login interface so that this operation is not verified on the homepage after login.

I hope this article will help you with ThinkPHP framework programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.