在EnterPrise SAB应用模块中,角色模块(Roles)处理部分是个比较重要的部分,它起着承上启下的作用。角色对于每一项操作而言,我们都规定了其可以操作的角色,当要执行这项操作之前,我们就拿此操作的Rule与IPrincipal进行比照,看看此IPrincipal是否满足这个Rule,如果满足说明有操作的权限,反之则没有。这样就达到了验证与授权的目的。
        本文主要对角色的基本操作进行一些说明。
        在处理角色之前,要先建立两个数据表:Roles、UserRoles用于承载角色数据和用户与角色关系数据

EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)CREATE TABLE [dbo].[Roles] (
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)    [RoleID] [
int] IDENTITY (11NOT NULL ,
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)    [RoleName] [nvarchar] (
256) COLLATE Chinese_PRC_CI_AS NULL 
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
ON [PRIMARY]
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)GO
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)CREATE TABLE [dbo].[UserRoles] (
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)    [U_ID] [
int] NULL ,
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)    [RoleID] [
int] NULL 
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
ON [PRIMARY]
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)GO

        建立表后,要建立与其相关的存储过程(如下):
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)CREATE PROCEDURE dbo.GetAllRoles
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
AS
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)    
SET NOCOUNT ON
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)    
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)    
SELECT RoleName
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)    FROM Roles
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)GO
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)CREATE PROCEDURE dbo.GetRoleIdByName
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)    @name    nvarchar(
256),
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)    @roleID 
int OUT
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
AS
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)    
SELECT @roleID = RoleID 
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)    FROM Roles 
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)    WHERE RoleName 
= @name
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
RETURN
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)GO
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)CREATE PROCEDURE dbo.GetRolesByName
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)    @name nvarchar(
256)
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
AS
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)    
DECLARE @userId int
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)    EXEC GetUserIdByName @name, @userId OUT
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)    
SELECT 
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)        Roles.RoleID, 
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)        Roles.RoleName
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)    FROM Userok
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)        
JOIN UserRoles
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)        
ON Userok.U_ID = UserRoles.U_ID
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)        
JOIN Roles
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)        
ON UserRoles.RoleID = Roles.RoleID
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)    WHERE Userok.U_ID 
= @userId
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
RETURN
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)GO
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)CREATE PROCEDURE dbo.GetUserInRoleByName
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)    @roleName nvarchar(
256)
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
AS
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)    
DECLARE @roleID int
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)    EXEC GetRoleIdByName @roleName, @roleID OUT
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)    
SELECT UserOK.U_ID, Userok.UserName
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)    FROM Roles 
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)        
JOIN UserRoles
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)            
ON Roles.RoleID = UserRoles.RoleID
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)        
JOIN Userok
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)            
ON UserRoles.U_ID = Userok.U_ID
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)    WHERE Roles.RoleID 
= @roleID
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
RETURN
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)GO
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)CREATE PROCEDURE dbo.InsertRole
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)    @name    nvarchar(
256)
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
AS
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)    INSERT INTO Roles
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)        (RoleName)
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)    VALUES
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)        (@name)
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)
RETURN
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)GO
EnterPrise应用(4) Security Application Block应用程序块 角色处理(VB.NET)

        表和存储过程建立完成后,即可进入代码阶段,先引用并添加System.Data、EnterpriseLibrary.Configuration、EnterpriseLibrary.Data、EnterpriseLibrary.Security.Database几个类,然后编写代码,下代码主要是对角色表进行添加、删除,为UserRoles(用户与角色的关系表)进行添加、删除等操作:
End Function

相关文章:

  • 2021-10-16
  • 2022-02-05
  • 2021-09-29
  • 2021-12-04
  • 2021-07-09
  • 2021-05-22
猜你喜欢
  • 2021-09-18
  • 2022-02-25
  • 2021-12-08
  • 2021-05-16
  • 2021-05-27
  • 2022-12-23
相关资源
相似解决方案