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.