User Power Detailed _php

Source: Internet
Author: User
Keywords Detailed power user Enable owned first
Have patience to read it (or the algorithm is important)
User Initial weight: Enable = 0;
If a user has the first power Select, then the user right value: Enable = enable + 1;
If a user has a second power insert, then the user right value: Enable = enable + 2;
If a user has a third power update, then the user right value: Enable = enable + 4;
If a user has the fourth power Delete, then the user right value: Enable = enable + 8;
Why the 1, + 2, +4, +8 instead of + 1, + 2, + 3, +4?
If it is + 1, + 2, + 3, + 4:
If a user has the first power Select, then the user right value: Enable = enable + 1;
If a user has a second power insert, then the user right value: Enable = enable + 2;
If a user has a third power update, then the user right value: Enable = enable + 3;
If a user has the fourth power Delete, then the user right value: Enable = enable + 4;
So when the user weights: Enable = 3 o'clock, you can't tell if the user is having
The first power select and the second power insert two rights, or only the third power update,
This can be avoided by the previous weight-value algorithm.
However, when using the previous weighted algorithm, how to enable users to quickly know what rights the user has
What about the force? If the user right value enable = 5, the user has the first power select and the third power update,
Note that you do not have the second power insert.
Below I will list of user rights:

Power to have the power of the user's right value enable
SelectItem: (1, 3, 5, 7, 9, 11, 13, 15)
InsertItem: (2, 3, 6, 7, 10, 11, 14, 15)
UpdateItem: (4, 5, 6, 7, 12, 13, 14, 15)
DeleteItem: (8, 9, 10, 11, 12, 13, 14, 15)
It is not obvious that the user with the right value can have "more" power,
If: User right value enable = 3, the user has the first power (Select) and the second power (Insert), a total of two rights,
user right value enable = 4, the user has the third power (Update), only one right.

Take a closer look at the "User Rights List":
SelectItem: (1, 3, 5, 7, 9, 11, 13, 15): 1 = 0 + 2, 0 power of 3 = 2, 1 power to 2, 0 = 5 power of 2, 2 power of 2;
InsertItem: (2, 3, 6, 7, 10, 11, 14, 15): 2 = 0 + 2, 1 power of 3 = 2, 0 power to 2, 1 = 6 power of 2, 2 power of 2;
UpdateItem: (4, 5, 6, 7, 12, 13, 14, 15): 4 = 0 + 2, 2 power of 5 = 2, 0 power to 2, 2 = 6 power of 2, 2 power of 2;
DeleteItem: (8, 9, 10, 11, 12, 13, 14, 15): 8 = 0, 2, 3 power, 9 = 2 Power + 0 2 Power 3 = 10 2 Power + 1 power;

The rules are summarized as follows:
Have the first power select User's weight: Enable =? + 2 of the power of 0, (where it must also be split into 2 of the addition of N power)
Has the second power insert user's weight: Enable =? + 2 of the power of 1, (where it must also be split into 2 of the addition of N power)
With the third power update the user's weight: Enable =? + 2 of the Power of 2, (where it must also be split into 2 of the addition of N power)
Have the fourth power delete user's weight: Enable =? + 2 of the Power of 3, (where it must also be split into 2 of the addition of N power)

It's clear to me:
As long as the user's weight can be split to 2 n power addition,
If there is n=0, then there is the first power Select,
If there is n=1, then there is a second power insert,
If there is n=2, then there is a third power update,
If there is a n=3, there is a fourth power delete,

So how to enable the user to quickly split the value of 2 n power to add the case?
Oh! As long as the enable is converted to binary, from right to left to the base, the bit destructors to have the corresponding right,
Example:
(11) 10 = (1011) 2, that is, the 第一、二、四 power, and "User Rights list" in accordance with;
(12) 10 = (1100) 2, that is, the third to fourth power, and the "User Rights list" in accordance with;
(15) 10 = (1111) 2, that is, the 第一、二、三、四 power, and "User Rights list" in accordance with;
When the level of entitlement is extremely complex, the algorithm can quickly know the user's power:
For example:
A total of 8 levels, user weights enable=67, (67) 10 = (1000011) 2, that is, the possession of 第一、六、七 power,
The user weight value enable=67, (159) 10 = (10011111) 2, that is, has the 第一、四、五、六、七、八 power.
To this end, I hope you have not wasted your time, to inspire you.
  • 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.