Most systems have permission systems. In general, it allows administrators to access a certain page or make some fields or controls visible or invisible. Whether data in the gridview can be deleted, added, or added. Most people use permissions as a sub-system. However, I don't want to design a permission management system here. There are too many online design solutions. It can be said that every developer has his own ideas and ideas for developing a permission management system.
In this article
Article In, I first use a simple C #
Code Simulate the permissions of a user and then use SQL to simulate the permissions. This is a simple, intuitive, and efficient way to determine user permissions.
C #:
Well, first define a user class in the simplest way, as shown below.
Copy code The Code is as follows: class user
{
Bool candelete;
Bool Canread;
Bool canwrite;
Bool canmodify;
Bool cancreate;
}
five attributes are designed to control user permissions. I found that although this is intuitive, it is not suitable for expansion. Let's separate the permissions. See the following code: copy Code the code is as follows: Enum permissiontypes: int
{< br> none = 0,
Read = 1,
write = 2,
modify = 4,
Delete = 8,
Create = 16,
All = read | write | modify | Delete | create
}< br> class user
{< br> Public permissiontypes permissions = permissiontypes. none;
}
Let's try it out first, and you will feel the magic:Copy codeThe Code is as follows: // create a user
User admin = new user ();
Admin. Permissions = permissiontypes. Read
| Permissiontypes. Write
| Permissiontypes. Delete;
// Verify the permission
Bool Canread = (permissiontypes. Read & Admin. permissions) = permissiontypes. Read );
Bool canwrite = (permissiontypes. Write & Admin. permissions) = permissiontypes. Write );
Bool cancreate = (permissiontypes. Create & Admin. permissions) = permissiontypes. Create );
// View the result
Console. writeline (Canread); // true
Console. writeline (canwrite); // true
Console. writeline (cancreate); // false
The '|' and '&' operations are used. However, this seems very difficult. The initialization and verification permissions use a long string of '|' and '&' code. Not intuitive. I will extend some methods in system. Enum for you to call. The Code is as follows. Copy code The Code is as follows: // whether the permission exists
Public static bool has <t> (this system. Enum type, T value)
{
Try
{
Return (INT) (object) type & (INT) (object) value) = (INT) (object) value );
}
Catch
{
Return false;
}
}
// Determine the permission
Public static bool is <t> (this system. Enum type, T value)
{
Try
{
Return (INT) (object) type = (INT) (object) value;
}
Catch
{
Return false;
}
}
// Add Permissions
Public static t add <t> (this system. Enum type, T value)
{
Try
{
Return (t) (object) (INT) (object) type | (INT) (object) value ));
}
Catch (exception ex)
{
Throw new argumentexception (
String. Format (
"Type '{0}' cannot be added }'",
Typeof (T). Name
), Ex );
}
}
// Remove the permission
Public static t remove <t> (this system. Enum type, T value)
{
Try
{
Return (t) (object) (INT) (object) type &~ (INT) (object) value ));
}
Catch (exception ex)
{
Throw new argumentexception (
String. Format (
"Type '{0}' cannot be removed }'",
Typeof (T). Name
), Ex );
}
}
usage: copy Code the code is as follows: // create a user
user admin = new user ();
permissiontypes permissions = new permissiontypes ();
admin. permissions = permissions;
// Add permission
admin. permissions = admin. permissions. add (permissiontypes. create);
admin. permissions = admin. permissions. add (permissiontypes. read);
admin. permissions = admin. permissions. add (permissiontypes. write);
// determine the permission
bool Canread = admin. permissions. has (permissiontypes. read); // true
bool canwrite = admin. permissions. has (permissiontypes. write); // true
bool candelete = admin. permissions. has (permissiontypes. delete); // false
bool cancreate = admin. permissions. has (permissiontypes. create); // true
console. writeline (Canread); // true
console. writeline (canwrite); // true
console. writeline (candelete); // false
console. writeline (cancreate); // true
console. read ();
SQL:
Most permission management operations are performed on databases. In accordance with the above ideas, I simulated the above operations in sqlserver, which is very efficient in SQL and or operations. Design two tables, user and permission.
1. Obtain all users with read permission:
Copy code The Code is as follows: Select * from [user] Where permissiontypes & 1 = 1
Result:
2. Obtain all users with the delete permission:Copy codeThe Code is as follows: Select * from [user] Where permissiontypes & 8 = 8
Result:
3. Check whether the unicorn has the delete permission.Copy codeThe Code is as follows: if exists (select * from [user] Where name = 'qilin' and permissiontypes & 8 = 8)
Print 'true'
Else
Print 'flase'
Result: flase
Author: Zhu Yulin