EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
Posted on 2006-11-02 09:46 THEONE2008 阅读(1398) 评论(0) 编辑 收藏 举报 在EnterPrise SAB应用模块中,角色模块(Roles)处理部分是个比较重要的部分,它起着承上启下的作用。角色对于每一项操作而言,我们都规定了其可以操作的角色,当要执行这项操作之前,我们就拿此操作的Rule与IPrincipal进行比照,看看此IPrincipal是否满足这个Rule,如果满足说明有操作的权限,反之则没有。这样就达到了验证与授权的目的。
本文主要对角色的基本操作进行一些说明。
在处理角色之前,要先建立两个数据表:Roles、UserRoles用于承载角色数据和用户与角色关系数据
CREATE TABLE [dbo].[Roles] (
[RoleID] [int] IDENTITY (1, 1) NOT NULL ,
[RoleName] [nvarchar] (256) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[UserRoles] (
[U_ID] [int] NULL ,
[RoleID] [int] NULL
) ON [PRIMARY]
GO
[RoleID] [int] IDENTITY (1, 1) NOT NULL ,
[RoleName] [nvarchar] (256) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[UserRoles] (
[U_ID] [int] NULL ,
[RoleID] [int] NULL
) ON [PRIMARY]
GO
建立表后,要建立与其相关的存储过程(如下):
CREATE PROCEDURE dbo.GetAllRoles
AS
SET NOCOUNT ON
SELECT RoleName
FROM Roles
GO
CREATE PROCEDURE dbo.GetRoleIdByName
@name nvarchar(256),
@roleID int OUT
AS
SELECT @roleID = RoleID
FROM Roles
WHERE RoleName = @name
RETURN
GO
CREATE PROCEDURE dbo.GetRolesByName
@name nvarchar(256)
AS
DECLARE @userId int
EXEC GetUserIdByName @name, @userId OUT
SELECT
Roles.RoleID,
Roles.RoleName
FROM Userok
JOIN UserRoles
ON Userok.U_ID = UserRoles.U_ID
JOIN Roles
ON UserRoles.RoleID = Roles.RoleID
WHERE Userok.U_ID = @userId
RETURN
GO
CREATE PROCEDURE dbo.GetUserInRoleByName
@roleName nvarchar(256)
AS
DECLARE @roleID int
EXEC GetRoleIdByName @roleName, @roleID OUT
SELECT UserOK.U_ID, Userok.UserName
FROM Roles
JOIN UserRoles
ON Roles.RoleID = UserRoles.RoleID
JOIN Userok
ON UserRoles.U_ID = Userok.U_ID
WHERE Roles.RoleID = @roleID
RETURN
GO
CREATE PROCEDURE dbo.InsertRole
@name nvarchar(256)
AS
INSERT INTO Roles
(RoleName)
VALUES
(@name)
RETURN
GO
AS
SET NOCOUNT ON
SELECT RoleName
FROM Roles
GO
CREATE PROCEDURE dbo.GetRoleIdByName
@name nvarchar(256),
@roleID int OUT
AS
SELECT @roleID = RoleID
FROM Roles
WHERE RoleName = @name
RETURN
GO
CREATE PROCEDURE dbo.GetRolesByName
@name nvarchar(256)
AS
DECLARE @userId int
EXEC GetUserIdByName @name, @userId OUT
SELECT
Roles.RoleID,
Roles.RoleName
FROM Userok
JOIN UserRoles
ON Userok.U_ID = UserRoles.U_ID
JOIN Roles
ON UserRoles.RoleID = Roles.RoleID
WHERE Userok.U_ID = @userId
RETURN
GO
CREATE PROCEDURE dbo.GetUserInRoleByName
@roleName nvarchar(256)
AS
DECLARE @roleID int
EXEC GetRoleIdByName @roleName, @roleID OUT
SELECT UserOK.U_ID, Userok.UserName
FROM Roles
JOIN UserRoles
ON Roles.RoleID = UserRoles.RoleID
JOIN Userok
ON UserRoles.U_ID = Userok.U_ID
WHERE Roles.RoleID = @roleID
RETURN
GO
CREATE PROCEDURE dbo.InsertRole
@name nvarchar(256)
AS
INSERT INTO Roles
(RoleName)
VALUES
(@name)
RETURN
GO
表和存储过程建立完成后,即可进入代码阶段,先引用并添加System.Data、EnterpriseLibrary.Configuration、EnterpriseLibrary.Data、EnterpriseLibrary.Security.Database几个类,然后编写代码,下代码主要是对角色表进行添加、删除,为UserRoles(用户与角色的关系表)进行添加、删除等操作:
Public Function InsertRole(ByVal RoleName As String) As Boolean
'添加角色
'定义配置文件中数据库名
Dim DbInstanceName As String = "temp"
manager = New UserRoleManager(DbInstanceName, ConfigurationManager.GetCurrentContext())
'验证角色名是否已经存在 使用GetRoleIdByName存储过程
If manager.GetRoleIdFromRoleName(RoleName) <= 0 Then
'建立新的角色 使用Insertrole存储过程
manager.CreateRole(RoleName)
InsertRole = True
Else
InsertRole = False
End If
End Function
Public Sub AddUserRole(ByVal userName As String, ByVal role As String)
' Checks if role already exists. If not, role is created.
Dim existingRoles As String() = Me.GetAllRoles()
Dim roleExists As Boolean = False
Dim associationExists As Boolean = False
Dim i As Integer = 0
For i = 0 To existingRoles.Length - 1
roleExists = (existingRoles(i).CompareTo(role) = 0)
If (roleExists) Then Exit For
Next i
If (Not roleExists) Then
Me.manager.CreateRole(role)
Else
' Checks if there is already an association between userName and existing role.
Dim dsRoles As DataSet = Me.manager.GetUserRoles(userName)
For Each row As DataRow In dsRoles.Tables(0).Rows
associationExists = (Convert.ToString(row("RoleName")).CompareTo(role) = 0)
If (associationExists) Then Exit For
Next
End If
If (Not associationExists) Then
Me.manager.CreateUserRole(userName, role)
End If
End Sub
Public Sub DeleteRoleFromUser(ByVal userName As String, ByVal role As String)
Me.manager.DeleteUserRole(userName, role)
End Sub
Public Function GetAllRoles() As String()
'返回所有角色名称
Dim dsRoles As DataSet = Me.manager.GetAllRoles()
Dim res(dsRoles.Tables(0).Rows.Count - 1) As String
Dim i As Integer = 0
For Each row As DataRow In dsRoles.Tables(0).Rows
res(i) = CType(row("RoleName"), String)
i += 1
Next
Return res
End Function
Public Function GetRolesForUser(ByVal userName As String) As String()
'使用GetRolesByName存储过程
'建立用户 角色以及用户角色关系表之间的链接,并检索出符合条件的角色数据
' UserOK Roles UserRoles
Dim dsRoles As DataSet = Me.manager.GetUserRoles(userName)
Dim res(dsRoles.Tables(0).Rows.Count - 1) As String
Dim i As Integer = 0
For Each row As DataRow In dsRoles.Tables(0).Rows
res(i) = CType(row("RoleName"), String)
i += 1
Next
Return res
End Function
'添加角色
'定义配置文件中数据库名
Dim DbInstanceName As String = "temp"
manager = New UserRoleManager(DbInstanceName, ConfigurationManager.GetCurrentContext())
'验证角色名是否已经存在 使用GetRoleIdByName存储过程
If manager.GetRoleIdFromRoleName(RoleName) <= 0 Then
'建立新的角色 使用Insertrole存储过程
manager.CreateRole(RoleName)
InsertRole = True
Else
InsertRole = False
End If
End Function
Public Sub AddUserRole(ByVal userName As String, ByVal role As String)
' Checks if role already exists. If not, role is created.
Dim existingRoles As String() = Me.GetAllRoles()
Dim roleExists As Boolean = False
Dim associationExists As Boolean = False
Dim i As Integer = 0
For i = 0 To existingRoles.Length - 1
roleExists = (existingRoles(i).CompareTo(role) = 0)
If (roleExists) Then Exit For
Next i
If (Not roleExists) Then
Me.manager.CreateRole(role)
Else
' Checks if there is already an association between userName and existing role.
Dim dsRoles As DataSet = Me.manager.GetUserRoles(userName)
For Each row As DataRow In dsRoles.Tables(0).Rows
associationExists = (Convert.ToString(row("RoleName")).CompareTo(role) = 0)
If (associationExists) Then Exit For
Next
End If
If (Not associationExists) Then
Me.manager.CreateUserRole(userName, role)
End If
End Sub
Public Sub DeleteRoleFromUser(ByVal userName As String, ByVal role As String)
Me.manager.DeleteUserRole(userName, role)
End Sub
Public Function GetAllRoles() As String()
'返回所有角色名称
Dim dsRoles As DataSet = Me.manager.GetAllRoles()
Dim res(dsRoles.Tables(0).Rows.Count - 1) As String
Dim i As Integer = 0
For Each row As DataRow In dsRoles.Tables(0).Rows
res(i) = CType(row("RoleName"), String)
i += 1
Next
Return res
End Function
Public Function GetRolesForUser(ByVal userName As String) As String()
'使用GetRolesByName存储过程
'建立用户 角色以及用户角色关系表之间的链接,并检索出符合条件的角色数据
' UserOK Roles UserRoles
Dim dsRoles As DataSet = Me.manager.GetUserRoles(userName)
Dim res(dsRoles.Tables(0).Rows.Count - 1) As String
Dim i As Integer = 0
For Each row As DataRow In dsRoles.Tables(0).Rows
res(i) = CType(row("RoleName"), String)
i += 1
Next
Return res
End Function