权限与角色数据库结构(转载)

来源:互联网 发布:什么是网络代理服务器 编辑:程序博客网 时间:2024/06/06 17:43

权限类型

表名:power

说明:存放权限类型

表结构:

字段名

数据类型

设定

说明

power_id

string

主键

权限编号

number

int

 

使用2的平方法数

remark

string

 

备注

:

sort

number

remark

read

1

读取查看

write

2

写入,保存

delete

4

删除

create

8

建立

edit

16

编辑

run

32

运行、执行

stop

64

终止

pause

128

中止

admin

256

管理

all

512

全部

none

1024

award

2048

授予

expand_1

 

对应到expand_1表的权限,如时间域

expand_2

 

对应到expand_2表的权限,如IP域

expand_3

 

对应到expand_3表的权限,如临时授权域

 

资源列表

表名:resource

说明:存放用户可访问的所有资源

表结构:

字段名

数据类型

设定

说明

resource_id

string

主键

资源编号

resource_sort

string

 

资源种类

remark

string

 

备注

:

resource_id

resource_ sort

remark

A_A

UI

A窗体,A控件

A_B

UI

A窗体,B控件

A_C

UI

A窗体,C控件

 

用户列表

表名:users

说明:存放用户信息

表结构:

字段名

数据类型

设定

说明

user_id

string

主键

用户编号

password

string

 

口令

remark

string

 

备注

:

user_id

password

remark

X

123

 

Y

123

 

Z

123

 

 

角色

表名:role

说明:存放角色信息

表结构:

字段名

数据类型

设定

说明

role_id

string

主键

角色编号

 

 

 

 

remark

string

 

备注

 

:

role_id

 

remark

role_1

 

 

role_2

 

 

role_3

 

 

 

角色对应的权限

表名:role_power

说明:存放角色信息

表结构:

字段名

数据类型

设定

说明

role_id

string

主键

 

power_id

string

 

remark

string

 

备注

:

role_id

power_id

remark

role_1

run

 

role_1

edit

 

role_2

run

 

role_3

run

 

role_1

read

 

role_3

edit

 

 

角色对应的用户

表名: role_user

说明:

表结构:

字段名

数据类型

设定

说明

role_id

string

主键

 

user_id

string

 

remark

string

 

备注

:

role_id

resource _id

remark

role_1

x

 

role_1

y

 

role_2

x

 

role_3

x

 

role_1

z

 

role_3

y

 

 

角色对应的资源

表名:role_resource

说明:

表结构:

 

字段名

数据类型

设定

说明

role_id

string

主键

 

resource_id

string

 

remark

string

 

备注

:

role_id

resource _id

remark

role_1

A_A

 

role_1

A_B

 

role_2

A_A

 

role_3

A_C

 

role_1

A_C

 

role_3

A_B

 

 

关系图




 

视图

用户可操作的资源视图

user_resource

SELECT TOP 100 PERCENT dbo.users.user_id, dbo.users.password, dbo.role_user.role_id,

      dbo.role_resource.resource_id, dbo.role_power.power_id

FROM dbo.users INNER JOIN

      dbo.role_user ON dbo.users.user_id = dbo.role_user.user_id INNER JOIN

      dbo.role_resource ON dbo.role_user.role_id = dbo.role_resource.role_id INNER JOIN

      dbo.role_power ON dbo.role_resource.role_id = dbo.role_power.role_id

ORDER BY dbo.users.user_id, dbo.role_resource.resource_id

 

存储过程

得到指定用户对资操作列表

get_userResource

--得到指定用户对资操作列表

 

create procedure get_userResource

@usname nvarchar(50),--用户名

@psw nvarchar(50),--口令

@scr nvarchar(50)='' --资源名,可以不传该参数,如果不传,将返回所有的资源

as

 

if @scr=''

   begin

      select * from user_resource where user_id=@usname and password=@psw

   end

else

    begin

      select * from user_resource where user_id=@usname and password=@psw and resource_id=@scr

 

    end

go

-- x用户的全部资源权限列表

get_userResource 'x','123'

 

go

 

-- x用户的对'A_A'资源权限列表

get_userResource 'x','123','A_A'

 

得到指定用户对指定资是否有指定的操作列权限

--得到指定用户对指定资是否有指定的操作列权限

--返回一个只有一行一列的表,true为存在,false为不存在

create procedure get_userSrcIsPw

                     @usname nvarchar(50),--用户名

                     @psw nvarchar(50),--口令

                     @scr nvarchar(50), --资源名

                     @pw nvarchar(50) --权限名

as

 

declare @temp nvarchar(5);

 

select @temp=power_id from user_resource

                      where     user_id=@usname

                            and password=@psw 

                            and resource_id=@scr

                            and power_id=@pw

 

if @temp= @pw

   begin

      select 'true' as '权限'

   end

else

    begin

      select 'false' as '权限'

 

    end

go

get_userSrcIsPw 'x','123','A_A','edit'

 我的例子没有提
 

read

1

write

2

delete

4

create

8

edit

16

run

32

stop

64

pause

128

admin

256

all

512

none

1024

award

2048


这种设记的意思,可以自已想一下(^_^)

 
原创粉丝点击