Four kinds of usage analysis of RBAC in thinkphp _php skills

Source: Internet
Author: User

The example of this article describes four usages of RBAC class in thinkphp. Share to everyone for your reference. The specific methods are as follows:

The first category: In the landing operation of the login controller

1.rbac::authenticate ();

The data that is used to find the user name submitted by the form in the user table is essentially a user table lookup statement:

Copy Code code as follows:
Return M (modle)->where (array)->find ();
This operation has two parameters

The A.array () array has the same function as the table lookup array:

Copy Code code as follows:
Array (' field ' => ' value ', ' Field ' =>array (' condition ', ' value '));

B.model is the table name, the default is the configuration parameter

Copy Code code as follows:
C (' User_auth_model ');
The return value is a query result that is manifested in a one-dimensional array.
Note: It is a single record search method for the user table, we can use it, directly with the search statement.

2.rbac::saveaccesslist ();

The user can manipulate the application name (group name), controller name, operation name in a three-dimensional array of the situation to write session.

The parameter is the user ID, generally we after the user login authentication passes, will write the user ID to the session

Copy Code code as follows:
C (' User_auth_key ');

The default of this method is to get $_session (C (' User_auth_key ')) this parameter;

Class Two: Put in the public controller (all the controller classes that participate in the permission validation all have to follow attains this class)

3.rbac::accessdecision ();

Used to determine whether the current user has permissions on the current control, the parameter defaults to the application name App_name, and if it is a grouped pattern, it has to be passed in to the group name Group_name

There's a way to call this.

Copy Code code as follows:
Rbac::checkaccess ();
Used to verify whether the current controller or operation participates in this judgment.

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

Need to verify controller: Require_auth_module requires verification action: Require_auth_action

No authentication controller required: Not_auth_module does not require validation: not_auth_action

If full write is required: write operations in Require_auth_action must also write their own controllers in Require_auth_module.

If you do not need to write all: The controller is written in Not_auth_module, all the methods in its controller will not need to be validated.

If you write the operation in Not_auth_action alone, you need to pay attention to the problem of duplicate operation name.

4.rbac::checklogin ();

Used to determine whether the user logged in.
Note: After landing the first page shows that this operation must participate in validation, so each role has to join this operation permissions.

Also can not use this method, directly determine whether the $_session[c (' User_auth_key ')] exists, does not exist to jump to the landing interface so that you can login after the first show this operation does not participate in validation.

I hope this article will be helpful to everyone's thinkphp framework program design.

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: info-contact@alibabacloud.com 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.