I moved from Jane
Original address of this article:The original address
Role permission judgment, this should be most applications have a function, then how to determine permission?
Here we say the train of thought, may be relatively low, you see the officer do not laugh.
For example, I now have A,B,C, and D. To determine whether an account has these functions. We have a list of all the permissions for this account. Where does the list come from? Of course is the background request back ah! The permission of function A is =>0, and so on. B=>1,C=>2,D=>3.
So if I have one account with all permissions, list would be [0,1,2,3], and if I don’t have B privileges it would be [0,2,3,4], so I think you get the idea.
So if you want to know if you have a permission, you’re going to say, well, why don’t you just go through the list? What if you have more access? And don’t you think it’s lower? = So at this point, we can use bit operations for this purpose:
Let’s start with the method we use to determine:
HasPermission (permission) {// Check whether permissions exist. Permission is a permission in the display of all permissions.return permission & GlobalValue.authValue
}
Copy the code
Define a declaration that contains all permissions
// Display all permissions listexport letPermission = {A: Math. The pow (2, 0), / / A function B: math.h pow (2, 1), / / B function C: math.h pow (2, 2), / D/C function: math.h pow (2, 3), / / D function... }Copy the code
When we and the background request a list containing a role, do the following for the list
if (authList && authList.length > 0) {
let authValue = 0
for (let i = 0; i < authList.length; i++) {
let index = authList[i]
let permission = Math.pow(2, index)
authValue += permission
}
returnGlobalvalue. authValue = authValue;}Copy the code
P.S. if you understand, you don’t need to look down. Don’t waste Daniel’s time ~~
In case you don’t understand my brother will be very confused what I am doing, what is this step operation? Don’t panic. Let’s read on
Let’s take an example to illustrate the problem. Assuming that my current account only has AC functions, how much authList do I get? Very simple: [0,2]
So what is the authValue that I’m going to return based on this for loop?
2 ^ 0 +2 ^ 2 =5, I won’t go into the math. So now we know that authValue = 5, so suppose I now want to determine whether the current account has A function permission:
let isHave = this. hasPermission(Permission.A)
Copy the code
So the next important thing, which requires A little bit of knowledge of discrete mathematics, is to compute the Permission.A&5 step and convert it to binary and then perform the & operation
1 0 0 0 0 0 1 & 0 0 1 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 0 0 0 0 1 result: 2 0 power = 1Copy the code
Zero means false, non-zero means true so the answer is, you have access to A
The same is true with Permission.B&5
Again, convert to binary and then ampersand
1 0 and 1 0 0 0 0 0 0 1 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 0 0 0 0 0 the result is: 0Copy the code
So the answer is no B permission
I believe that this should be clear. If you don’t understand the operation of ampersand, you can search the operation of ampersand in discrete mathematics. 1=1, 0 = any =0 (1=1, 0 =0) =) so to sum up: when making any permission judgment, you can use this kind of bit operation to make judgment.
The others have no EMMM