Simple and efficient permission design example implemented by asp.net + sqlserver

Source: Internet
Author: User

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, I first use a simple C # code to simulate a user's permissions, and then use SQL to simulate. 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 codeThe 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 and look at the following code:
Copy codeThe Code is as follows:
Enum PermissionTypes: int
{
None = 0,
Read = 1,
Write = 2,
Modify = 4,
Delete = 8,
Create = 16,
All = Read | Write | Modify | Delete | Create
}
Class User
{
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 codeThe 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 );
}
}

Use:
Copy codeThe Code is as follows:
// Create a user
User admin = new User ();
PermissionTypes permissions = new PermissionTypes ();
Admin. Permissions = permissions;
// Add Permissions
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 codeThe 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

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.