Most systems have permission systems. In general, it can control the person's access to a page, or to some fields, controls, or invisible. Whether the data in the GridView can be deleted, can be added, added, and so on. Most people are independent of their permissions as a subsystem. But here I do not want to design a rights management system, online design is too much, you can say that each developer has its own development rights management system ideas and ideas.
In this article, I first use simple C # code to simulate a user's permissions, and then use SQL to simulate it. This is a very simple, intuitive, and efficient way to determine the user's permissions.
C#:
OK, first, from the simplest start, define a user class, as follows.
Copy Code code as follows:
Class User
{
BOOL CanDelete;
BOOL CanRead;
BOOL CanWrite;
BOOL Canmodify;
BOOL Cancreate;
}
Here are 5 properties designed to control user permissions. I find it very intuitive, but not suitable for expansion. We separate the permissions and look at the following code:
Copy Code code 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 first and you'll feel the Magic:
Copy Code code as follows:
Create a user
User admin = new user ();
Admin. Permissions = Permissiontypes.read
| Permissiontypes.write
| Permissiontypes.delete;
Verify Permissions
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 Results
Console.WriteLine (CanRead); True
Console.WriteLine (CanWrite); True
Console.WriteLine (cancreate); False
Using the ' | ' and ' & ' two operations. But it looks very, very easy. Initialize permissions and verify permissions with a long list of ' | ' And the ' & ' operation of the code. It's not intuitive. I extend some methods for you to call in System.Enum, the code is as follows.
Copy Code code as follows:
Is there a permission
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;
}
}
Judging permissions
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 (
"Cannot add type ' {0} '",
typeof (T). Name
), ex);
}
}
Remove permissions
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 (
"The type ' {0} ' cannot be removed",
typeof (T). Name
), ex);
}
}
Use:
Copy Code code 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);
Judging permissions
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 of the rights management is the operation of the database, in accordance with the above ideas, I in the SQL Server inside the simulation of the above operations, and or the operation is very efficient. First design two table user and permission.
1. Get all users with Read permission:
Copy Code code as follows:
SELECT * FROM [User] where permissiontypes&1 =1
Result:
2. Get all users with delete permission:
Copy Code code as follows:
SELECT * FROM [User] where permissiontypes&8 =8
Result:
3, to determine whether the Kirin has a delete permission
Copy Code code as follows:
if exists (select * from [User] where name= ' Qilin ' and permissiontypes&8 =8)
print ' true '
Else
print ' Flase '
Result:flase
Author: Zhu Qilin