基于默认 ASP.NET RoleProvider 的分层 SQL 角色

发布于 2024-10-16 21:57:56 字数 2567 浏览 7 评论 0原文

我正在尝试对默认 ASP.NET RoleProvider 进行以下调整,以便它支持分层角色定义。但是我无法创建以下函数,它会保持执行函数...

参考:http://mark.tremaine.net/howto/hierarchical-sql-role-provider/

这个函数有什么问题吗?

    -- ================================================
    -- Template generated from Template Explorer using:
    -- Create Multi-Statement Function (New Menu).SQL
    --
    -- Use the Specify Values for Template Parameters 
    -- command (Ctrl-Shift-M) to fill in the parameter 
    -- values below.
    --
    -- This block of comments will not be included in
    -- the definition of the function.
    -- ================================================
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    -- =============================================
    -- Author:      <Author,,Name>
    -- Create date: <Create Date,,>
    -- Description: <Description,,>
    -- =============================================


     CREATE FUNCTION [dbo].[aspnet_Roles_Ancestor_TVF] (
     @RoleId uniqueidentifier
     )
     RETURNS
     @aspnet_Roles TABLE (
     ApplicationId uniqueidentifier
     , RoleId uniqueidentifier
     , RoleName nvarchar(256)
     , LoweredRoleName nvarchar(256)
     , Description nvarchar(256)
     , ParentRoleId uniqueidentifier
     )
     AS
     BEGIN
     ; WITH aspnet_Roles_CTE (
     ApplicationId
     , RoleId
     , RoleName
     , LoweredRoleName
     , Description
     , ParentRoleId
     , HierarchyLevel
     ) AS (
     SELECT
     ApplicationId
     , RoleId
     , RoleName
     , LoweredRoleName
     , Description
     , ParentRoleId
     , 1 AS HierarchyLevel
     FROM aspnet_Roles
     WHERE RoleId = @RoleId

     UNION ALL

     SELECT
     aspnet_Roles.ApplicationId
     , aspnet_Roles.RoleId
     , aspnet_Roles.RoleName
     , aspnet_Roles.LoweredRoleName
     , aspnet_Roles.Description
     , aspnet_Roles.ParentRoleId
     , aspnet_Roles_CTE.HierarchyLevel + 1 AS HierarchyLevel
     FROM aspnet_Roles
     INNER JOIN aspnet_Roles_CTE
     ON aspnet_Roles.RoleId = aspnet_Roles_CTE.ParentRoleId
     )

     INSERT INTO @aspnet_Roles (
     ApplicationId
     , RoleId
     , RoleName
     , LoweredRoleName
     , Description
     , ParentRoleId
     )
     SELECT
     ApplicationId
     , RoleId
     , RoleName
     , LoweredRoleName
     , Description
     , ParentRoleId
     FROM aspnet_Roles_CTE
     ORDER BY HierarchyLevel

     RETURN
     END
    GO

I'm trying to implement the following adjustments to the default ASP.NET RoleProvider so that it supports hierarchical role definitions. However i cannot create the following function, it keeps Executing the function...

Ref: http://mark.tremaine.net/howto/hierarchical-sql-role-provider/

What is wrong with this function?

    -- ================================================
    -- Template generated from Template Explorer using:
    -- Create Multi-Statement Function (New Menu).SQL
    --
    -- Use the Specify Values for Template Parameters 
    -- command (Ctrl-Shift-M) to fill in the parameter 
    -- values below.
    --
    -- This block of comments will not be included in
    -- the definition of the function.
    -- ================================================
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    -- =============================================
    -- Author:      <Author,,Name>
    -- Create date: <Create Date,,>
    -- Description: <Description,,>
    -- =============================================


     CREATE FUNCTION [dbo].[aspnet_Roles_Ancestor_TVF] (
     @RoleId uniqueidentifier
     )
     RETURNS
     @aspnet_Roles TABLE (
     ApplicationId uniqueidentifier
     , RoleId uniqueidentifier
     , RoleName nvarchar(256)
     , LoweredRoleName nvarchar(256)
     , Description nvarchar(256)
     , ParentRoleId uniqueidentifier
     )
     AS
     BEGIN
     ; WITH aspnet_Roles_CTE (
     ApplicationId
     , RoleId
     , RoleName
     , LoweredRoleName
     , Description
     , ParentRoleId
     , HierarchyLevel
     ) AS (
     SELECT
     ApplicationId
     , RoleId
     , RoleName
     , LoweredRoleName
     , Description
     , ParentRoleId
     , 1 AS HierarchyLevel
     FROM aspnet_Roles
     WHERE RoleId = @RoleId

     UNION ALL

     SELECT
     aspnet_Roles.ApplicationId
     , aspnet_Roles.RoleId
     , aspnet_Roles.RoleName
     , aspnet_Roles.LoweredRoleName
     , aspnet_Roles.Description
     , aspnet_Roles.ParentRoleId
     , aspnet_Roles_CTE.HierarchyLevel + 1 AS HierarchyLevel
     FROM aspnet_Roles
     INNER JOIN aspnet_Roles_CTE
     ON aspnet_Roles.RoleId = aspnet_Roles_CTE.ParentRoleId
     )

     INSERT INTO @aspnet_Roles (
     ApplicationId
     , RoleId
     , RoleName
     , LoweredRoleName
     , Description
     , ParentRoleId
     )
     SELECT
     ApplicationId
     , RoleId
     , RoleName
     , LoweredRoleName
     , Description
     , ParentRoleId
     FROM aspnet_Roles_CTE
     ORDER BY HierarchyLevel

     RETURN
     END
    GO

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

饭团 2024-10-23 21:57:56

我认为问题出在 CTE 的结构上。 Union 查询的前半部分应代表父项,第二部分应返回子项。也就是说,你是沿着层次结构走下去,而不是向上走。因此,我会将 Union 查询后半部分的 On 子句更改为:

aspnet_Roles_CTE.RoleId = aspnet_Roles.ParentRoleId

编辑

一些示例数据会有所帮助。这是我发起的一个小测试:

Declare @RoleId int;
Set @RoleId = 1;

With aspnet_Roles As
    (
    Select 1 As ApplicationId, 1 As RoleId, 'Parent Role A' As RoleName, Null As ParentRoleId
    Union All Select 1, 2, 'Parent Role B', Null
    Union All Select 1, 3, 'Parent Role C', Null
    Union All Select 1, 4, 'Child Role A-A', 1
    Union All Select 1, 5, 'Child Role A-B', 1
    Union All Select 1, 6, 'Child Role A-C', 1
    Union All Select 1, 7, 'Child Role A-A-A', 4
    Union All Select 1, 8, 'Child Role A-A-B', 4
    Union All Select 1, 9, 'Child Role A-A-C', 4
    )
    , aspnet_Roles_CTE ( ApplicationId, RoleId, RoleName, ParentRoleId, HierarchyLevel ) As
    (
    Select ApplicationId, RoleId, RoleName, ParentRoleId, 1 AS HierarchyLevel
    From aspnet_Roles
    Where RoleId = @RoleId
    Union All
    Select AR.ApplicationId, AR.RoleId, AR.RoleName, AR.ParentRoleId, HierarchyLevel + 1
    From aspnet_Roles As AR
        Join aspnet_Roles_CTE As CTE
            On CTE.ApplicationId = AR.ApplicationId
                And CTE.RoleId = AR.ParentRoleId
     )
Select ApplicationId, RoleId, RoleName, ParentRoleId, HierarchyLevel
From aspnet_Roles_CTE

结果:

ApplicationId | RoleId | RoleName         | ParentRoleId | HierarchyLevel
1             | 1      | Parent Role A    | NULL         | 1
1             | 4      | Child Role A-A   | 1            | 2
1             | 5      | Child Role A-B   | 1            | 2
1             | 6      | Child Role A-C   | 1            | 2
1             | 7      | Child Role A-A-A | 4            | 3
1             | 8      | Child Role A-A-B | 4            | 3
1             | 9      | Child Role A-A-C | 4            | 3

I believe the issue is in the structure of your CTE. The first half of the Union query should represent the parent and the second should return the children. I.e, you are walking down the hierarchy not up it. Thus, I would change the On clause in the second half of the Union query to:

aspnet_Roles_CTE.RoleId = aspnet_Roles.ParentRoleId.

Edit

Some sample data would help. Here's a small test I whipped up:

Declare @RoleId int;
Set @RoleId = 1;

With aspnet_Roles As
    (
    Select 1 As ApplicationId, 1 As RoleId, 'Parent Role A' As RoleName, Null As ParentRoleId
    Union All Select 1, 2, 'Parent Role B', Null
    Union All Select 1, 3, 'Parent Role C', Null
    Union All Select 1, 4, 'Child Role A-A', 1
    Union All Select 1, 5, 'Child Role A-B', 1
    Union All Select 1, 6, 'Child Role A-C', 1
    Union All Select 1, 7, 'Child Role A-A-A', 4
    Union All Select 1, 8, 'Child Role A-A-B', 4
    Union All Select 1, 9, 'Child Role A-A-C', 4
    )
    , aspnet_Roles_CTE ( ApplicationId, RoleId, RoleName, ParentRoleId, HierarchyLevel ) As
    (
    Select ApplicationId, RoleId, RoleName, ParentRoleId, 1 AS HierarchyLevel
    From aspnet_Roles
    Where RoleId = @RoleId
    Union All
    Select AR.ApplicationId, AR.RoleId, AR.RoleName, AR.ParentRoleId, HierarchyLevel + 1
    From aspnet_Roles As AR
        Join aspnet_Roles_CTE As CTE
            On CTE.ApplicationId = AR.ApplicationId
                And CTE.RoleId = AR.ParentRoleId
     )
Select ApplicationId, RoleId, RoleName, ParentRoleId, HierarchyLevel
From aspnet_Roles_CTE

Results:

ApplicationId | RoleId | RoleName         | ParentRoleId | HierarchyLevel
1             | 1      | Parent Role A    | NULL         | 1
1             | 4      | Child Role A-A   | 1            | 2
1             | 5      | Child Role A-B   | 1            | 2
1             | 6      | Child Role A-C   | 1            | 2
1             | 7      | Child Role A-A-A | 4            | 3
1             | 8      | Child Role A-A-B | 4            | 3
1             | 9      | Child Role A-A-C | 4            | 3
蓝天白云 2024-10-23 21:57:56

我知道 CTE 有递归耗尽限制,但也许你有一个孩子的父母的孩子,即循环关系?

I know that CTEs have a recursion exhaustion limit, but maybe you've got a child of a parent of a child, i.e., a cyclic relationship?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文