基於使用者角色的許可權設計

2021-04-27 11:49:11 字數 4519 閱讀 5142

整個例子隨便試驗下。

--使用者與角色是多對多

--角色與許可權是多對多

aps_user---使用者表

aps_role---角色表

aps_user_group--使用者角色表

aps_module---模組表(或成選單表)

aps_power---許可權表

1、先建立錶用以下指令碼

if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[fk_aps_power_aps_module]') and objectproperty(id, n'isforeignkey') = 1)

alter table [dbo].[aps_power] drop constraint fk_aps_power_aps_module

goif exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[fk_aps_power_aps_role]') and objectproperty(id, n'isforeignkey') = 1)

alter table [dbo].[aps_power] drop constraint fk_aps_power_aps_role

goif exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[fk_aps_user_group_aps_role]') and objectproperty(id, n'isforeignkey') = 1)

alter table [dbo].[aps_user_group] drop constraint fk_aps_user_group_aps_role

goif exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[fk_aps_user_group_aps_user]') and objectproperty(id, n'isforeignkey') = 1)

alter table [dbo].[aps_user_group] drop constraint fk_aps_user_group_aps_user

goif exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[aps_module]') and objectproperty(id, n'isusertable') = 1)

drop table [dbo].[aps_module]

goif exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[aps_power]') and objectproperty(id, n'isusertable') = 1)

drop table [dbo].[aps_power]

goif exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[aps_role]') and objectproperty(id, n'isusertable') = 1)

drop table [dbo].[aps_role]

goif exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[aps_user]') and objectproperty(id, n'isusertable') = 1)

drop table [dbo].[aps_user]

goif exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[aps_user_group]') and objectproperty(id, n'isusertable') = 1)

drop table [dbo].[aps_user_group]

gocreate table [dbo].[aps_module] (

[mid] [int] not null ,

[mname] [nvarchar] (50) collate sql_latin1_general_cp1_ci_as not null ,

[murl] [nvarchar] (100) collate sql_latin1_general_cp1_ci_as null

) on [primary]

gocreate table [dbo].[aps_power] (

[pid] [int] not null ,

[rid] [int] not null ,

[mid] [int] not null

) on [primary]

gocreate table [dbo].[aps_role] (

[rid] [int] not null ,

[rname] [nvarchar] (50) collate sql_latin1_general_cp1_ci_as not null

) on [primary]

gocreate table [dbo].[aps_user] (

[uid] [int] not null ,

[uname] [nvarchar] (50) collate sql_latin1_general_cp1_ci_as not null

) on [primary]

gocreate table [dbo].[aps_user_group] (

[uid] [int] not null ,

[rid] [int] not null ,

[modify_date] [datetime] null

) on [primary]

goalter table [dbo].[aps_module] add

constraint [pk_aps_module] primary key  clustered

([mid]

)  on [primary]

goalter table [dbo].[aps_role] add

constraint [pk_aps_role] primary key  clustered

([rid]

)  on [primary]

goalter table [dbo].[aps_user] add

constraint [pk_aps_user] primary key  clustered

([uid]

)  on [primary]

goalter table [dbo].[aps_user_group] add

constraint [df_aps_user_group_modify_date] default (getdate()) for [modify_date]

goalter table [dbo].[aps_power] add

constraint [fk_aps_power_aps_module] foreign key

([mid]

) references [dbo].[aps_module] (

[mid]

),constraint [fk_aps_power_aps_role] foreign key

([rid]

) references [dbo].[aps_role] (

[rid])go

alter table [dbo].[aps_user_group] add

constraint [fk_aps_user_group_aps_role] foreign key

([rid]

) references [dbo].[aps_role] (

[rid]

),constraint [fk_aps_user_group_aps_user] foreign key

([uid]

) references [dbo].[aps_user] (

[uid])go

2、---獲取使用者角色

select b.rname from aps_user a ,aps_role b,aps_user_group c

where c.uid= a.uid and c.rid=b.rid and a.uname='guangshu'

--獲取使用者所有角色許可權(或稱模組或稱選單)

select distinct (g.murl) ,g.mname  from aps_module  g

where g.mid in(

select  distinct f.mid from aps_power f where f.rid in(

select b.rid from aps_user a ,aps_role b,aps_user_group c

where c.uid= a.uid and c.rid=b.rid and a.uname='guangshu'))

基於角色的許可權設計

基於角色的許可權設計 一 在任何系統中,許可權設計是最基礎的東西,本文給出乙個基於角色的許可權設計的循序漸進的設計方案。在許可權系統中,功能 許可權 是最小的單位,比如起草新聞 編輯新聞 審核新聞 刪除新聞等,而角色是一類功能的集合,比如新聞編輯這個角色,他可能有起草新聞 編輯新聞等功能集合,而責任...

基於角色的許可權設計

在任何系統中,許可權設計是最基礎的東西,本文給出乙個基於角色的許可權設計的循序漸進的設計方案。在許可權系統中,功能 許可權 是最小的單位,比如起草新聞 編輯新聞 審核新聞 刪除新聞等,而角色是一類功能的集合,比如新聞編輯這個角色,他可能有起草新聞 編輯新聞等功能集合,而責任編輯他可能就有更多的許可權...

基於角色的許可權設計

基於角色的許可權設計 一 在任何系統中,許可權設計是最基礎的東西,本文給出乙個基於角色的許可權設計的循序漸進的設計方案。在許可權系統中,功能 許可權 是最小的單位,比如起草新聞 編輯新聞 審核新聞 刪除新聞等,而角色是一類功能的集合,比如新聞編輯這個角色,他可能有起草新聞 編輯新聞等功能集合,而責任...