这是转载的 http://www.cnblogs.com/zhuqil/archive/2010/04/02/Permission.html
大部分系统都有权限系统。一般来说,它能管控人员对某个可否页面的访问;对某些字段的可见或者不可见。对gridview中的数据是否可删除、可添加、可新增、可见。大部分人都把权限作为一个子系统独立出来。但是这里我不是想设计一个权限管理系统,网上的设计方案太多了,可以说每个开发人员都有自己的开发权限管理系统的想法和思路。
在这篇文章中,我先用简单的C#代码模仿一个用户的权限,再使用sql去模拟。这是一种很简单,很直观,很高效的方式去判定用户的权限。好吧,先从最简单开始,定义一个用户(User)类,如下。
class User
2 {
3 bool CanDelete;
4 bool CanRead;
5 bool CanWrite;
6 bool CanModify;
7 bool CanCreate;
8 }
这里设计5个属性来管控用户的权限。我发现这样虽然很直观,但是不宜扩张。我们将权限独立出来,在看下面代码:
1
enum
PermissionTypes :
int
2
{
3
None
=
0
,
4
Read
=
1
,
5
Write
=
2
,
6
Modify
=
4
,
7
Delete
=
8
,
8
Create
=
16
,
9
All
=
Read
|
Write
|
Modify
|
Delete
|
Create
10
}
11
class
User
12
{
13
public
PermissionTypes Permissions
=
PermissionTypes.None;
14
}
我们先试用一下,你就能感觉到神奇之处:
1
//
创建一个用户
2
User admin
=
new
User();
3
admin.Permissions
=
PermissionTypes.Read
4
|
PermissionTypes.Write
5
|
PermissionTypes.Delete;
6
7
//
验证权限
8
bool
canRead
=
((PermissionTypes.Read
&
admin.Permissions)
==
PermissionTypes.Read);
9
bool
canWrite
=
((PermissionTypes.Write
&
admin.Permissions)
==
PermissionTypes.Write);
10
bool
canCreate
=
((PermissionTypes.Create
&
admin.Permissions)
==
PermissionTypes.Create);
11
12
//
查看结果
13
Console.WriteLine(canRead);
//
true
14
Console.WriteLine(canWrite);
//
true
15
Console.WriteLine(canCreate);
//
false
16
利用了'|'和'&'两个操作。但是这样看起来很是很别捏,初始化权限和验证权限用了一长串'|'和'&'运算的代码。很不直观。我在System.Enum中扩展一些方法供你调用,代码如下。
1
public
static
class
EnumerationExtensions
2
{
3
4
//
checks if the value contains the provided type
5
public
static
bool
Has
<
T
>
(
this
System.Enum type, T value)
6
{
7
try
8
{
9
return
(((
int
)(
object
)type
&
(
int
)(
object
)value)
==
(
int
)(
object
)value);
10
}
11
catch
12
{
13
return
false
;
14
}
15
}
16
17
//
checks if the value is only the provided type
18
public
static
bool
Is
<
T
>
(
this
System.Enum type, T value)
19
{
20
try
21
{
22
return
(
int
)(
object
)type
==
(
int
)(
object
)value;
23
}
24
catch
25
{
26
return
false
;
27
}
28
}
29
30
//
appends a value
31
public
static
T Add
<
T
>
(
this
System.Enum type, T value)
32
{
33
try
34
{
35
return
(T)(
object
)(((
int
)(
object
)type
|
(
int
)(
object
)value));
36
}
37
catch
(Exception ex)
38
{
39
throw
new
ArgumentException(
40
string
.Format(
41
"
Could not append value from enumerated type '{0}'.
"
,
42
typeof
(T).Name
43
), ex);
44
}
45
}
46
47
//
completely removes the value
48
public
static
T Remove
<
T
>
(
this
System.Enum type, T value)
49
{
50
try
51
{
52
return
(T)(
object
)(((
int
)(
object
)type
&
~
(
int
)(
object
)value));
53
}
54
catch
(Exception ex)
55
{
56
throw
new
ArgumentException(
57
string
.Format(
58
"
Could not remove value from enumerated type '{0}'.
"
,
59
typeof
(T).Name
60
), ex);
61
}
62
}
63
}
使用一下:
1
//
创建一个用户
2
User admin
=
new
User();
3
PermissionTypes permissions
=
new
PermissionTypes();
4
admin.Permissions
=
permissions;
5
//
添加权限
6
admin.Permissions
=
admin.Permissions.Add(PermissionTypes.Create);
7
admin.Permissions
=
admin.Permissions.Add(PermissionTypes.Read);
8
admin.Permissions
=
admin.Permissions.Add(PermissionTypes.Write);
9
//
判断权限
10
bool
canRead
=
admin.Permissions.Has(PermissionTypes.Read);
//
true
11
bool
canWrite
=
admin.Permissions.Has(PermissionTypes.Write);
//
true
12
bool
canDelete
=
admin.Permissions.Has(PermissionTypes.Delete);
//
false
13
bool
canCreate
=
admin.Permissions.Has(PermissionTypes.Create);
//
true
14
15
Console.WriteLine(canRead);
//
true
16
Console.WriteLine(canWrite);
//
true
17
Console.WriteLine(canDelete);
//
false
18
Console.WriteLine(canCreate);
//
true
19
Console.Read();
SQL:
大部分权限管理都是数据库的操作,好依照上面的思路,我在sqlserver里面模拟一下以上的操作,在sql中与或运算是很高效的。先设计两张表User和Permission。
1、获取有Read权限的所有用户:
1
select
*
from
[
User
]
where
PermissionTypes
&
1
=
1
Result:
2、获取有Delete权限的所有用户:
1
select
*
from
[
User
]
where
PermissionTypes
&
8
=
8
result:
3、判断麒麟是否有有Delete权限
1
if
exists
(
select
*
from
[
User
]
where
Name
=
'
qilin
'
and
PermissionTypes
&
8
=
8
)
2
print
'
true
'
3
else
4
print
'
flase
'
result:
flase