复制代码 代码如下:
class User
{
bool CanDelete;
bool CanRead;
bool CanWrite;
bool CanModify;
bool CanCreate;
}
复制代码 代码如下:
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
复制代码 代码如下:
//是否存在权限
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();
3、判断麒麟是否有有Delete权限
复制代码 代码如下:
if exists (select * from [User] where and PermissionTypes&8 =8)
print 'true'
else
print 'flase'
新闻热点
疑难解答
图片精选