此文章來自也算賭徒的我的關於'角色型存取控制'的許可權管理的資料庫的設計
請發郵件到freeget.one@gmail.com獲得翻強軟體。
*/
use [master]
go
-- 檢查資料庫 [RBAC]是否存在,如果存在則刪除(只測試用,不然會丟資料.)
-- Search from the sysdatabase to see that if the [RBAC] database exist.
-- If exists then drop it else create it.
if exists(select * from sysdatabases where name = 'RBAC')
drop database [RBAC]
go
-- 建立資料庫 [RBAC]
-- Create the database named by '[RBAC]'.
create database [RBAC]
go
-- 使用資料庫 [RBAC]
-- Use the database of '[RBAC]'.
use [RBAC]
go
-- 建立 "使用者" 資料表 [RBAC_User]
-- Create the datatable named by '[RBAC_User]' to save users.
create table [RBAC_User]
(
--使用者編號
[User_ID] int primary key not null,
--使用者名稱稱
[User_Name] varchar(20) not null,
--使用者密碼
[User_PassWord] varchar(20) not null,
--使用者狀態
[User_Lock] bit not null
)
go
-- 添加測試資料
-- Add data for test
insert into [RBAC_User] values(1,'FightingYang','PassWord',0);
go
insert into [RBAC_User] values(2,'Supper3000','Teacher',0);
go
insert into [RBAC_User] values(3,'JianzhongLi','Teacher',1);
go
select * from [RBAC_User]
go
-- 建立 "組" 資料表 [RBAC_Group]
-- Create the datatable named by '[RBAC_Group]' to save groups.
create table [RBAC_Group]
(
--組編號
[Group_ID] int primary key not null,
--組名稱
[Group_Name] varchar(20) not null
)
go
-- 添加測試資料
-- Add data for test
insert into [RBAC_Group] values(1,'編程愛好者');
go
insert into [RBAC_Group] values(2,'MSDN老師');
go
select * from [RBAC_Group]
go
-- 建立 "角色" 資料表 [RBAC_Role]
-- Create the datatable named by '[RBAC_Role]' to save roles.
create table [RBAC_Role]
(
--角色編號
[Role_ID] int primary key not null,
--角色名稱
[Role_Name] varchar(20) not null
)
go
-- 添加測試資料
-- Add data for test
insert into [RBAC_Role] values(1,'admin');
go
insert into [RBAC_Role] values(2,'user');
go
select * from [RBAC_Role]
go
-- 建立 "資源" 資料表 [RBAC_Resource]
-- Create the datatable named by '[RBAC_Resource]' to save Resources.
create table [RBAC_Resource]
(
--資源編號
[Resource_ID] int primary key not null,
--資源名稱
[Resource_Name] varchar(20) not null
)
go
-- 添加測試資料
-- Add data for test
insert into [RBAC_Resource] values(1,'音頻');
go
insert into [RBAC_Resource] values(2,'視頻');
go
select * from [RBAC_Resource]
go
-- 建立 "操作" 資料表 [RBAC_Operate]
-- Create the datatable named by '[RBAC_Operate]' to save Operates.
create table [RBAC_Operate]
(
--操作編號
[Operate_ID] int primary key not null,
--操作名稱
[Operate_Name] varchar(10) not null
)
go
-- 添加測試資料
-- Add data for test
insert into [RBAC_Operate] values(1,'添加');
go
insert into [RBAC_Operate] values(2,'讀取');
go
insert into [RBAC_Operate] values(3,'編寫');
go
insert into [RBAC_Operate] values(4,'刪除');
go
select * from [RBAC_Operate]
go
-- 建立 "許可權" 資料表 [RBAC_Privilege]
-- Create the datatable named by [RBAC_Privilege] to save privileges.
create table [RBAC_Privilege]
(
--許可權編號
[Privilege_ID] int primary key not null,
--資源編號
[Resource_ID] int foreign key references [RBAC_Resource]([Resource_ID]) not null,
--操作編號
[Operate_ID] int foreign key references [RBAC_Operate]([Operate_ID]) not null
)
go
-- 添加測試資料
-- Add data for test
-- 第一條許可權是對"音頻"的"添加"許可權
insert into [RBAC_Privilege] values(1,1,1);
go
-- 第二條許可權是對"音頻"的"讀取"許可權
insert into [RBAC_Privilege] values(2,1,2);
go
-- 第三條許可權是對"音頻"的"編寫"許可權
insert into [RBAC_Privilege] values(3,1,3);
go
-- 第四條許可權是對"音頻"的"刪除"許可權
insert into [RBAC_Privilege] values(4,1,4);
go
-- 第五條許可權是對"視頻"的"讀取"許可權
insert into [RBAC_Privilege] values(5,2,1);
go
-- 第六條許可權是對"視頻"的"讀取"許可權
insert into [RBAC_Privilege] values(6,2,2);
go
-- 第七條許可權是對"視頻"的"編寫"許可權
insert into [RBAC_Privilege] values(7,2,3);
go
-- 第八條許可權是對"視頻"的"刪除"許可權
insert into [RBAC_Privilege] values(8,2,4);
go
select * from [RBAC_Operate]
go
-- 建立 "授權" 資料表 [RBAC_Impower]
-- Create the datatable named by [RBAC_Impower] to save Impower.
create table [RBAC_Impower]
(
--授權編號
[Impower_ID] int primary key not null,
--角色編號
[Role_ID] int foreign key references [RBAC_Role]([Role_ID]) not null,
--許可權編號
[Privilege_ID] int foreign key references [RBAC_Privilege]([Privilege_ID]) not null
)
go
-- 添加測試資料
-- Add data for test
-- 第一條授權內容"admin"具有'對"音頻"的"添加"許可權'
insert into [RBAC_Impower] values(1,1);
go
-- 第二條授權內容"admin"具有'對"音頻"的"讀取"許可權'
insert into [RBAC_Impower] values(2,2);
go
-- 第三條授權內容"admin"具有'對"音頻"的"編寫"許可權'
insert into [RBAC_Impower] values(3,3);
go
-- 第四條授權內容"admin"具有'對"音頻"的"刪除"許可權'
insert into [RBAC_Impower] values(4,4);
go
-- 第五條授權內容"admin"具有'對"視頻"的"添加"許可權'
insert into [RBAC_Impower] values(5,5);
go
-- 第六條授權內容"admin"具有'對"視頻"的"讀取"許可權'
insert into [RBAC_Impower] values(6,6);
go
-- 第七條授權內容"admin"具有'對"視頻"的"編寫"許可權'
insert into [RBAC_Impower] values(7,7);
go
-- 第八條授權內容"admin"具有'對"視頻"的"刪除"許可權'
insert into [RBAC_Impower] values(8,8);
go
-- 第九條授權內容"user"具有'對"音頻"的"讀取"許可權'
insert into [RBAC_Impower] values(9,2);
go
-- 第十條授權內容"user"具有'對"視頻"的"讀取"許可權'
insert into [RBAC_Impower] values(10,5);
go
select * from [RBAC_Impower]
go
-- 添加測試資料
-- Add data for test
-- 組所具備的角色的資料第一條的內容是"MSDN老師"具有"admin"的角色
insert into [RBAC_GroupRole] values(1,2,1);
go
-- 組所具備的角色的資料第二條的內容是"編程愛好者"具有"user"的角色
insert into [RBAC_GroupRole] values(2,1,2);
go
select * from [RBAC_GroupRole]
go
-- 建立 "使用者組" 資料表 [RBAC_UserGroupRole]
-- Create the datatable named by '[RBAC_UserGroupRole]' to save userGroupRoles.
create table [RBAC_UserGroupRole]
(
--使用者組編號
[UserGroup_ID] int primary key not null,
--使用者編號
[User_ID] int foreign key references [RBAC_User]([User_ID]) not null,
--組編號
[Group_ID] int foreign key references [RBAC_Group]([Group_ID]) not null,
--角色編號
[Role_ID] int foreign key references [RBAC_Role]([Role_ID]) not null
)
go
-- 添加測試資料
-- Add data for test
-- 第一條使用者組資料是"FightingYang"屬於"編程愛好者"組,在組中的角色是"admin"
insert into [RBAC_UserGroup] values(1,1,1,1);
go
-- 第二條使用者組資料是"Supper3000"屬於"MSDN老師"組,在組中的角色是"admin"
insert into [RBAC_UserGroup] values(2,2,2,1);
go
-- 第三條使用者組資料是"JianzhongLi"屬於"MSDN老師"組,在組中的角色是"user"
insert into [RBAC_UserGroup] values(3,3,2,2);
go
select * from [RBAC_UserGroupRole]
go