Permission management is generally divided into "User Management", "role management", and "permission management ". The permission management mainly controls how to add, modify, and delete a module. For example, a user can add Article ", But does not have the permission to modify (Review) the article. The following describes "permission management ".
First, check the format of the data stored in the database:CopyCodeCode: News: 3, Gallery: 1, users: 15
You may often see the format 3, 1, and 15 stored in the database, without the previous news, gallery, and users.ProgramIt is easy to view when writing. News stands for the News Module, gallery stands for the image library module, and users stands for the user management module, whereas 3,1, 15:Copy codeThe Code is as follows: 1 = 1 = 20
3 = 1 + 2 = 20 + 21
15 = 1 + 2 + 4 + 8 = 20 + 21 + 22 + 23
All these values can be obtained using bitwise OR:Copy codeThe Code is as follows: 1 = convert. toint32 (1)
3 = convert. toint32 (1 | 2)
15 = convert. toint32 (1 | 2 | 4 | 8)
And 1, 2, 4, and 8 correspond to the permission enumeration:Copy codeThe Code is as follows: // <summary>
/// 1 add, 2 modify, 4 Delete, 8 manage list
/// </Summary>
Public Enum perm
{
Add = 1,
Update = 2,
Delete = 4,
Readlist = 8
}
If you already have a permission value of 7, how do we know what permissions this user has? Our analysis:Copy codeThe Code is as follows: 7 = 1 + 2 + 4 = 20 + 21 + 22 = 2 3-1
In fact, this is a mathematical formula. I'm glad that what I learned in the Mathematics Department has a role in programming:Copy codeThe Code is as follows: sigma (2n, n is a natural number) = 2n + 1-1, namely 1 + 2 + 4 + 8 +... + 64 = 128-1 = 127
Undoubtedly, this is complicated.
Unfinished, To be continued...