设计基础:用户、角色、权限三大核心表,加上用户角色、角色权限两个映射表(用于给用户表联系上权限表)。这样就可以通过登录的用户来获取权限列表,或判断是否拥有某个权限。
大致用到5张表:用户表(UserInfo)、角色表(RoleInfo)、菜单表(MenuInfo)、用户角色表(UserRole)、角色菜单表(RoleMenu)。
各表的大体表结构如下:
1、用户表(UserInfo):Id、UserName、UserPwd
2、角色表(RoleInfo):Id、RoleName
3、菜单表(MenuInfo):Id、MenuName
4、用户角色表(UserRole):Id、UserId、RoleId
5、角色菜单表(RoleMenu):Id、RoleId、MenuId
最关键的地方是,某个用户登录时,如何查找该用户的菜单权限?其实一条语句即可搞定:
假如用户的用户名为Arthur,则他的菜单权限查询如下:
Select m.Id,m.MenuName from MenuInfo m ,UserInfo u, UserRole ur, RoleMenu rm Where m.Id = rm.MenuId and ur.RoleId = rm.RoleId and ur.UserId = u.Id and u.UserName = 'Arthur'
1、用户信息表:
1
2
3
4
5
6
7
8
9
10
11
12
create
table
employee
(
userid
varchar
(50)
not
null
,
--用户ID
username
varchar
(100),
--用户名
userpassword
varchar
(100),
--密码
..
..
..
..
)
alter
table
employee
--主键
add
constraint
pk_employee_userid
primary
key
(userid)
2、角色表:
1
2
3
4
5
6
7
create
table
role
(
roleid
varchar
(50)
not
null
,
--角色Id
rolename
varchar
(100),
--角色名称
)
alter
table
tole
--主键
add
constraint
pk_role_roleid
primary
key
(roleid)
3、权限菜单表
1
2
3
4
5
6
7
8
9
10
11
create
table
popedom
(
popedomid
int
identity(1,1)
not
null
,
--权限Id
popedomname
varchar
(100),
--权限名称
popedomfatherid
int
,
--权限父ID
popedomurl
varchar
(100)
--树的连接路径
..
..
)
er
table
popedom
--主键
add
constraint
PK_popedom
primary
key
(popedomid)
添加数据如 insert into popedom values('我的办公桌',0,'') insert into popedom values('电子邮箱',1,'../mail/EmaiolManage.aspx') (添加数据的原则是一级接点的popedomfatherid 为0,如果是(我的办公桌)下面的接点,它们的popedomfatherid为(我的办公桌)的主键)
4、用户与角色关系表
1
2
3
4
5
6
7
8
create
table
user_role
(
connectionid
int
identity(1,1)
not
null
,
--关系ID
userid
varchar
(50)
not
null
,
--管理员表ID
roleid
varchar
(50)
not
null
--角色Id
)
alter
table
user_role
--主键
add
constraint
PK_admin_role
primary
key
(connectionid)
5、角色与权限关系表
1
2
3
4
5
6
7
8
9
create
table
role_popedom
--角色与权限表
(
connectionid
int
identity(1,1),
--关系Id
roleid
varchar
(50)
not
null
,
--角色ID
popedomid
int
not
null
,
--权限Id
popedom
int
--权限 (1为可用,2为不可用)
)
alter
table
role_popedom
--主键
add
constraint
PK_role_popedom
primary
key
(connectionid)
--主键