Example of a PHP bitwise operator permission operation

Source: Internet
Author: User
On the binary number of the bit operation, the common is "or, and, not" the three simple operations, of course, PHP manual also has "XOR, left shift, right shift" the three operations.

How to define Permissions
The permissions are defined by a value of 2 N, and so on. Why do you define this? This definition guarantees that there is only one 1 in each permission value (binary), and that it corresponds to exactly one permission. Like what:

Define (' ADD ', 1); Increased permissions define (' UPD ', 2); Modify permissions define (' SEL ', 4); Find permissions define (' DEL ', 8); Delete permissions

Permission actions
Permission operations actually involve the concept of "role". Permission operations are done by giving a role a certain permission, prohibiting certain permissions, and detecting whether a role has some kind of permission. Relative to these three operations. It can be conveniently implemented with the operation of binary number.

Give some kind of permission to use the bitwise OR operator $a_access = ADD | UPD | SEL | DEL; A has the ability to ADD and delete changes $b_access UPD | SEL; B has the change check permission $c_access = ADD | UPD; C has the Change permission//prohibit some kind of permission with "bit and" and "bitwise NON" operator $d_access = $c _access & ~upd; D has only the privilege//check if there is a permission to use the bitwise AND operator Var_dump ($b _access & ADD); 1 for B has empowerment var_dump ($b _access & DEL); 0 on behalf of B does not have permission to delete

Implement simple permission classes and role classes
Using the above permissions operation method, you can simply encapsulate a permission class and a role class.

<?php/** * Simple Permission class */class Peak_auth {/** * Permission class counter * function is to generate permission value * * @var int */protected Stati    c $authCount = 0;    /** * Permission Name * * @var String */protected $authName;    /** * Permissions Details * * @var String */protected $authMessage;    /** * Permission value * * @var int 2 n/protected $authValue;     /** * constructor * Initialize permission name, permission details, and permission value * * @param string $authName permission name * @param string $authMessage permission details        */Public Function construct ($authName, $authMessage = ') {$this->authname = $authName;        $this->authmessage = $authMessage;        $this->authvalue = 1 << self:: $authCount;    Self:: $authCount + +; }/** * This class does not allow object copy operations */Private Function Clone () {}/** * Set permissions Details * * @param string $auth    Message */Public Function setauthmessage ($authMessage) {$this->authmessage = $authMessage; }/** * Get permission name * * @return String */Public Function Getauthname () {return $this->authname;     /** * Get Permission value * * @return int */Public Function Getauthvalue () {return $this->authvalue; /** * Get permission Details * * @return String */Public Function getauthmessage () {return $this-&G    T;authmessage; }}/** * Simple Character class * * @author 27_man */class peak_role {/** * role name * * @var String */protected $roleNam    E    /** * Role-owned permission value * * @var int */protected $authValue;    /** * Parent Role Object * * @var peak_role */protected $parentRole; /** * Constructor * * @param string $roleName role name * @param peak_role $parentRole Parent Role Object */Public function        Construct ($roleName, peak_role $parentRole = null) {$this->rolename = $roleName;        $this->authvalue = 0;            if ($parentRole) {$this->parentrole = $parentRole; $this->authvalue = $parentRoLe->getauthvalue ();            }}/** * Gets the permissions of the parent role */protected function Fetchparenauthvalue () {if ($this->parentrole) {        $this->authvalue |= $this->parentrole->getauthvalue (); }}/** * Give some kind of permission * * @param peak_auth $auth * @return peak_role for chained operation */Public Function-All        ow (Peak_auth $auth) {$this->fetchparenauthvalue ();        $this->authvalue |= $auth->getauthvalue ();    return $this; }/** * Block certain permissions * * @param peak_auth $auth * @return peak_role for chained operation */Public function deny (Pea        K_auth $auth) {$this->fetchparenauthvalue ();        $this->authvalue &= ~ $auth->getauthvalue ();    return $this; }/** * Detects if there is a permission * * @param peak_auth $auth * @return Boolean */Public function Checkauth (Pea    K_auth $auth) {return $this->authvalue & $auth->getauthvalue (); }/** * Get permission values for roles * *@return int */Public Function Getauthvalue () {return $this->authvalue; }}?>
Related Article

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.