时间:2021-07-01 10:21:17 帮助过:35人阅读
class User
{
bool CanDelete;
bool CanRead;
bool CanWrite;
bool CanModify;
bool CanCreate;
}
这里设计5个属性来管控用户的权限。我发现这样虽然很直观,但是不宜扩张。我们将权限独立出来,在看下面代码:
代码如下:
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;
}
我们先试用一下,你就能感觉到神奇之处:
代码如下:
//创建一个用户
User admin = new User();
admin.Permissions = PermissionTypes.Read
| PermissionTypes.Write
| PermissionTypes.Delete;
//验证权限
bool canRead = ((PermissionTypes.Read & admin.Permissions) == PermissionTypes.Read);
bool canWrite = ((PermissionTypes.Write & admin.Permissions) == PermissionTypes.Write);
bool canCreate = ((PermissionTypes.Create & admin.Permissions) == PermissionTypes.Create);
//查看结果
Console.WriteLine(canRead); //true
Console.WriteLine(canWrite); //true
Console.WriteLine(canCreate); //false
利用了'|'和'&'两个操作。但是这样看起来很是很别捏,初始化权限和验证权限用了一长串'|'和'&'运算的代码。很不直观。我在System.Enum中扩展一些方法供你调用,代码如下。
代码如下:
//是否存在权限
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;
}
}
//判断权限
public static bool Is<T>(this System.Enum type, T value)
{
try
{
return (int)(object)type == (int)(object)value;
}
catch
{
return false;
}
}
//添加权限
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(
"不能添加类型 '{0}'",
typeof(T).Name
), ex);
}
}
//移除权限
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(
"不能移除类型 '{0}'",
typeof(T).Name
), ex);
}
}
使用一下:
代码如下:
//创建一个用户
User admin = new User();
PermissionTypes permissions = new PermissionTypes();
admin.Permissions = permissions;
//添加权限
admin.Permissions = admin.Permissions.Add(PermissionTypes.Create);
admin.Permissions = admin.Permissions.Add(PermissionTypes.Read);
admin.Permissions = admin.Permissions.Add(PermissionTypes.Write);
//判断权限
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:
大部分权限管理都是数据库的操作,好依照上面的思路,我在sqlserver里面模拟一下以上的操作,在sql中与或运算是很高效的。先设计两张表User和Permission。
1、获取有Read权限的所有用户:
代码如下:select * from [User] where PermissionTypes&1 =1
Result:
2、获取有Delete权限的所有用户:
代码如下:select * from [User] where PermissionTypes&8 =8
Result:
3、判断麒麟是否有有Delete权限
代码如下:
if exists (select * from [User] where Name='qilin' and PermissionTypes&8 =8)
print 'true'
else
print 'flase'
Result: flase
作者:朱祁林