If you have patience, you can read (or the algorithm is important) the initial user weight: Enable0. if a user has the first Select power, the user weight is EnableEnable + 1; if a user has the second power Insert, the user's weight is: EnableEnable + 2; if a user has the third power Update, the user's weight is: EnableEnable + 4; patience (or algorithm importance)
Initial user weight: Enable = 0;
If a user has the first right to Select, the user's right value is: Enable = Enable + 1;
If a user has the second right to Insert, the user's right value is: Enable = Enable + 2;
If a user has the third right to Update, the user's right value is: Enable = Enable + 4;
If a user has the fourth right to Delete, the user's right value is: Enable = Enable + 8;
Why are the numbers of + 1, + 2, + 4, and + 8 in sequence, instead of + 1, + 2, + 3, and + 4?
Suppose it is + 1, + 2, + 3, + 4:
If a user has the first right to Select, the user's right value is: Enable = Enable + 1;
If a user has the second right to Insert, the user's right value is: Enable = Enable + 2;
If a user has the third right to Update, the user's right value is: Enable = Enable + 3;
If a user has the fourth right to Delete, the user's right value is: Enable = Enable + 4;
When the user weight is set to Enable to 3, you cannot determine whether the user has both permissions.
The first power Select and the second power Insert are the three power Update,
The former weight algorithm can avoid this situation.
However, when the previous weight algorithm is used, how can the user's weight Enable quickly know which permissions the user has?
What about force? For example, if the user weight is Enable = 5, the user has the first Power Select and the third power Update,
Note that you do not have the second power Insert.
I will list the user powers below:
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)
Obviously, users who do not have a large value of Enable have "more" power,
Assume that the user's right value is Enable = 3, and the user has the first power (Select) and the second power (Insert). There are two rights in total,
User authorization value: Enable = 4. the user has the third right (Update) and only one right.
Observe the "user power list" carefully ":
SelectItem :( 1, 3, 5, 7, 9, 11, 13, 15): 1 = 0 power of 0 + 2; 3 = 1 power of 2 + 0 power of 2; 5 = 2 Power + 0 power of 2;
InsertItem :( 2, 3, 6, 7, 10, 11, 14, 15): 1 power of 2 = 0 + 2; 3 = 0 power of 2 + 1 power of 2; 6 = 2 Power + 1 power of 2;
UpdateItem: (4, 5, 6, 7, 12, 13, 14, 15): 4 = 2 power of 0 + 2; 5 = 0 power of 2 + 2 power of 2; 6 = 2 power of 2 + 2 power of 2;
DeleteItem: (8, 9, 10, 11, 12, 13, 14, 15): 8 = 3 Power of 0 + 2; 9 = 0 power of 2 + 3 power of 2 = 1 power of 2 + 3 power of 2;
The rule is summarized as follows:
Select user with the first power: Enable =? + 0 power of 2; (where? It must also be split into two N-power addition cases)
Has the second power: Enable =? + 1 power of 2; (where? It must also be split into two N-power addition cases)
Has the third power to Update the user's weight: Enable =? + 2 power; (where? It must also be split into two N-power addition cases)
Has the fourth right: Enable =? + 3 power of 2; (where? It must also be split into two N-power addition cases)
It is clear that:
As long as the user's weight value Enable is split into the N-power addition of 2,
If N = 0, Select has the first power,
If N = 1, the system has the second power of Insert,
If N = 2, the system has the third power: Update,
If N = 3, the system has the fourth power: Delete,
So how can we quickly split the user weight value Enable into the N-power addition of 2?
Haha! As long as the Enable is converted to binary, the base is obtained from the right to the left, and the base bit has the corresponding right,
Example:
(11) 10 = (1011) 2, that is, having the first, second, and fourth powers, consistent with the "user Power List;
(12) 10 = (1100) 2, that is, having the third and fourth powers, consistent with the "user Power List;
(15) 10 = (1111) 2, that is, having the first, second, third, and fourth powers, consistent with the "user Power List;
When the level of rights is extremely complex, the algorithm can quickly know the user's power:
For example:
There are 8 levels in total, with the user's right value Enable = 67; (67) 10 = (1000011) 2, that is, they have the first, sixth, and seventh power,
User authorization value: Enable = 67; (159) 10 = (10011111) 2, that is, the user has the first, fourth, fifth, sixth, seventh, and eighth power.
By now, I hope you have not wasted your time and will be inspired.
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.