Home Backend Development PHP Tutorial Analysis of practical PHP member permission control implementation principles_PHP tutorial

Analysis of practical PHP member permission control implementation principles_PHP tutorial

Jul 21, 2016 pm 03:29 PM
php generation member analyze principle accomplish practical control yes replace Permissions system design Universal

My general permission system design is to try not to involve code modification when changing permissions. It comes from the chinaunix forum. I turned it over today to take a look. I hope it will be helpful to everyone and will be a great improvement for bkJia friends.

Copy code The code is as follows:

/*
  *Control access table
 * Acl value function
* 1 Requires login
* 2 Self-modification
* 4 Requires group permission set
* 8 Requires identity access set
🎜> * 64 Accessible Sundays
 * 128 Accessible times
 * 256 Enter password to access
 * 512 Super management use
 */
 class aclACL extends acl {
Public $routername="acl";
public $aclid='2'; //Permission resource ID, if the logged-in person does not have this permission, then the other values ​​​​(below) are 0 and cannot be accessed
public $roledisable=array(9); //Disable identity
public $pwd=123456; //Password access ACL->noPwd();
public $date=array('begin'=>0 ,'end'=>0); //Allow dates between
public $hours=array('begin'=>0,'end'=>0); //Hour range within a day
Public $weeks=array('begin'=>0,'end'=>0); //Monday to Saturday within a week
public $aclgroup=array("create"=>"4, 45,8"); //create requires groups to create
public $aclrole=array("all"=>"6","create"=>"7,95,78"); // Create requires the role to be created. This group requires the role with ID 6 to access
public $acl=array("all"=>0,
"index"=>4, //Table 4 Indicates the combination of inspection groups
 "delete"=>1, //Delete can only be deleted after logging in, of course it can be set to 2 or 4
 "update"=>1, //Update submission can only be done by logging in It can be updated after doing it here to prevent illegal and post. Edit cannot access the display edit content page
 "createForm"=>1, //It is also impossible to submit a new database
 "edit"=>0, //The edit box is displayed only after logging in
 "show"=>0, //It can be displayed without logging in
 "create"=>1); //Innovation forms require login operations and can set a certain group to be able to Create
 }
 ?>
 

This is the file module to be authenticated is acl
Whenever a user accesses the acl module, if authentication is turned on, this class will be called
Then this class will perform authentication checks based on the value of all or index of $acl.
Just put this file in the router/acl directory. The framework will automatically authenticate. If the user does not have the corresponding forward authorization, he will not be able to access the corresponding restrictions.
For example, the negative permission of the crud create method is 17. According to the previous explanation, login and group authorization should be required, which are the three groups 4 45 8 of create in the $aclgroup array.
First, if the member is not logged in, he will be prompted to log in. If the member is not logged in, he will be prompted to log in. If you are not in these three groups and cannot access this method, you will be prompted that you do not have permission.
At present, the router can enable acl control according to the situation
The method is to add public function isAcl(){} in the xxxxRouter.class.php file
You can return the permission file name, such as returning curd, then it will be called automatically curdACL.class.php class and name
CurdRouter class setting verification


Copy code The code is as follows:
class curdRouter extends controller{
  //Return to RBAC control access list verification class. By default, it has the same name as the router, which is curd
  //You can not write this function, then the universal permission system will not be enabled.
public function isAcl(){}
public function index()
{
$booktype=M("booktype");
$this->pager=C("pager" ); //Get the category
$this->pager->setPager($booktype->count(),10,'page');//Get the total number of data, set each page to 10
$this->assign("list",$booktype->orderby("bookid desc")->limit($this->pager->offset(),10)->fetch() ->getRecord());
 }
 public function login(){ //Login page
 }
 public function logout(){ //Exit page
 MY()-> ;logout(); //Log out
 redirect(url_for("guestbook/index"),"Logout successful",3);
 }
 public function noAcl($mask) { //Process it If there is no permission, redirect to login
redirect(url_for("guestbook/login"),"Login required",3);
}
public function loginpost() { //Login submission place simply handles login authentication
 if($_POST['author']=='queryphp'&&md5($_POST['pwd'])==md5('123456'))
 {
 MY()->setLogin (); //Set login status
redirect(url_for("guestbook/adminlist"),"Login successful",3);
 }
redirect(url_for("guestbook/login"),"Login Failed",3);
 }


Copy code The code is as follows:

  /*
* Basic login information class
* Permission table can be cached Data is restored when logging in.
 */
 class mybase {
 public $options=array();
 public $uid;
 public $username;
 public $isadmin;
 public $role= array(); //The identity I use
public $group=array(); //The group I am in
public $grouprole=array(); //The identity of the group
public $mygroupMar=array (); //The group I own and manage
public $mygroupOwn=array(); //The group that belongs to me
public $acl=array(); // Active control table groupacl and myacl control permission collection content It is rbacid of rbac
public $groupacl=array(); //Control permissions owned by the group
public $myacl=array(); //Control permissions owned by my identity
public $loginfaild =0; //If the number of failed logins exceeds this number, how many minutes should the IP login be banned?

This is basic
You can put myUser.class.php in the project lib directory
Copy the code and use the MY() function to get myUser.
curd6.gif

 

rbac.png

 

rbac-2.gif

rbac-4.gif
rbac-5.gif
rbac-6.gif
rbac-7.gif
rbac-8.gif
rbac-9.gif
rbac-10.gif
rbac-11.gif
rbac-12.gif
rbac-13.gif
rbac-14.gif
rbac-15.gif
rbac-16.gif
rbac-17.gif
rbac-18.gif
rbac-19.gif
rbac-20.gif
rbac-21.gif
rbac-22.gif
rbac-23.gif
rbac-24.gif
rbac-25.gif
rbac-26.gif

You can view the framework file

There is a guestbookRouter.class.php in the project/router directory

In the background

 

Get the guestbookRouter.class.php class name and method.

Then add permissions to these methods

 

There is an application permission on the right and a cancellation permission. If you cancel the permission, it means there is no permission restriction

That is to delete the permission file

Apply permissions means adding permissions to this class will generate a permissions file.

Generate the guestbookACL.class.php file in project/router/acl/

When the program loads guestbookRouter.class.php, it will check whether there is a guestbookACL.class.php permission file

If yes, use permission verification, if not, then no. Adding and subtracting permissions in this way does not change the entry of the guestbookRouter.class.php file

So it will be very convenient to add permissions in the future.
http://queryphp.googlecode.com/files/queryphp_2011_01_27.zip

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323443.htmlTechArticleMy general permission system design is to try not to involve code modification when changing permissions. It comes from the chinaunix forum and is transferred here today. have a look. I hope it will be helpful to everyone. For friends with PHP100...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? Apr 07, 2025 am 12:02 AM

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

See all articles