Advertisement

03.13.2008 at 07:14PM PDT, ID: 23240681
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.8

Get bit value from joined tables using recursion

Asked by dij8 in SQL Query Syntax, SQL Server 2005

Tags:

I am storing document details in a table.  A document can belong to multiple categories(1).  Categories can have parent categories which can have parent categories and so on.  A category can have permissions assigned to a user(2) and or a group(3). A group is a table which contains a list of users(4).  A category inherits the permissions of its parent.  The document inherits the permissions of it category.

(1) table DocumentLibraryDocumentDetails relates to table DocumentLibraryCategory via table DocumentLibraryDocumentCategory:
DocumentLibraryDocumentDetails DD
JOIN DocumentLibraryDocumentCategory DC ON DC.DocumentLibraryDocumentDetailsId = DD.DocumentLibraryDocumentDetailsId
JOIN DocumentLibraryCategory DLC ON DLC.DocumentLibraryCategoryId = DC.DocumentLibraryCategoryId

(2) table DocumentLibraryCategoryUser relates to table DocumentLibraryDocumentDetails where DocumentLibraryCategoryUser.DocumentLibraryDocumentDetailsId = DocumentLibraryDocumentDetails.DocumentLibraryDocumentDetailsId

(3) table DocumentLibraryDocumentGroup relates to table DocumentLibraryDocumentDetails where DocumentLibraryDocumentGroup.DocumentDetailsId = DocumentLibraryDocumentDetails.DocumentLibraryDocumentDetailsId

(4) table User relates to table RoleGroup via table RoleGroupMember
User TU
JOIN RoleGroupMember RGM ON RGM.UserId = TU.UserId
JOIN RoleGroup RG ON RG.RoleGroupId = RGM.RoleGroupId


I need to check if a user has permission to view a document.  I thought I had it with the following but as it turns out (logically so now I look closer at the SQL) I am only checking the permission of the category it belongs to and its immediate parent.  I need some way of recursing the category parents and checking permissions.  And of course, once a parent category is found with permissions then it shouldn't recurse any more it should just verify whether the user is allowed (a categories assigned permissions override any parent category permissions).

Note: with this SP I am passing a comma delimited list of categories.  It also has a public/admin check which can be ignored.  Just for fun a document can have its own permissions assigned which override the category permissions.  These three things all work but it was easier to include these parts of the SP than remove them.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
ALTER PROCEDURE [dbo].[GetDocumentLibraryDocumentDetailsByCriteria]
  @CategoryIds nvarchar(50),
  @ForPublic bit,
  @UserId int
AS
BEGIN
  CREATE TABLE #ValueList (ListValue int NOT NULL)
  IF @CategoryIds IS NOT NULL AND @CategoryIds <> ''
    BEGIN
      declare @curPos int
      declare @prevCurPos int
      set @curPos = -1
      set @prevCurPos = 1
      While @curPos <>0
        begin
          set @curPos = charindex(',', @CategoryIds, @curPos + 1)
          if @curPos <> 0
            begin
              insert into #ValueList values( CONVERT(int, substring(@CategoryIds, @prevCurPos, @curPos-@prevCurPos)) )
            end
          else
            begin
              insert into #ValueList values( CONVERT(int, substring(@CategoryIds, @prevCurPos, len(@CategoryIds)-@prevCurPos +1)) )
            end
          Set @prevCurPos = @curPos+1
        end
    END
  SELECT * FROM DocumentLibraryDocumentDetails
    WHERE DocumentLibraryDocumentDetailsId IN (SELECT DISTINCT DD.DocumentLibraryDocumentDetailsId FROM DocumentLibraryDocumentDetails DD
      LEFT JOIN DocumentLibraryDocumentCategory DC ON DC.DocumentLibraryDocumentDetailsId = DD.DocumentLibraryDocumentDetailsId
      LEFT JOIN DocumentLibraryCategory DLC ON DLC.DocumentLibraryCategoryId = DC.DocumentLibraryCategoryId
      LEFT JOIN DocumentLibraryDocumentGroup DLDG ON DLDG.DocumentDetailsId = DD.DocumentLibraryDocumentDetailsId
      WHERE 
        (@CategoryIds = '' OR @CategoryIds IS NULL
          OR ((DC.[DocumentLibraryCategoryId]) IN (SELECT ListValue FROM #ValueList))
        )
        AND (
            (@ForPublic = 1
              AND ((DD.[InheritPermissions] = 0 AND
                  (
                    (SELECT COUNT(DU.UserId) FROM DocumentLibraryDocumentUser DU WHERE DU.DocumentLibraryDocumentDetailsId = DD.DocumentLibraryDocumentDetailsId) = 0
                    AND (SELECT COUNT(DG.GroupId) FROM DocumentLibraryDocumentGroup DG WHERE DG.DocumentDetailsId = DD.DocumentLibraryDocumentDetailsId) = 0
                  )
                  OR @UserId IN (SELECT DU.UserId FROM DocumentLibraryDocumentUser DU WHERE DU.DocumentLibraryDocumentDetailsId = DD.DocumentLibraryDocumentDetailsId)
                  OR @UserId IN (SELECT RGM.UserId FROM [RoleGroup] RG INNER JOIN [RoleGroupMember] RGM ON RGM.RoleGroupId = RG.RoleGroupId WHERE RG.RoleGroupId IN (SELECT DG.GroupId FROM DocumentLibraryDocumentGroup DG WHERE DG.DocumentDetailsId = DD.DocumentLibraryDocumentDetailsId))
                )
                OR (DD.[InheritPermissions] = 1 AND
                  (
                    (SELECT COUNT(DLCU.UserId) FROM DocumentLibraryCategoryUser DLCU WHERE DLCU.DocumentLibraryCategoryId = DLC.DocumentLibraryCategoryId OR DLCU.DocumentLibraryCategoryId = DLCP.DocumentLibraryCategoryId) = 0
                     AND (SELECT COUNT(DLCG.RoleGroupId) FROM DocumentLibraryCategoryRoleGroup DLCG WHERE DLCG.DocumentLibraryCategoryId = DLC.DocumentLibraryCategoryId OR DLCG.DocumentLibraryCategoryId = DLCP.DocumentLibraryCategoryId) = 0
                  )
                  OR @UserId IN (SELECT DLCU.UserId FROM DocumentLibraryCategoryUser DLCU WHERE DLCU.DocumentLibraryCategoryId = DLC.DocumentLibraryCategoryId OR DLCU.DocumentLibraryCategoryId = DLCP.DocumentLibraryCategoryId)
                  OR @UserId IN (SELECT RGM.UserId FROM [RoleGroup] RG INNER JOIN [RoleGroupMember] RGM ON RGM.RoleGroupId = RG.RoleGroupId WHERE RG.RoleGroupId IN (SELECT DLCG.RoleGroupId FROM DocumentLibraryCategoryRoleGroup DLCG WHERE DLCG.DocumentLibraryCategoryId = DLC.DocumentLibraryCategoryId OR DLCG.DocumentLibraryCategoryId = DLCP.DocumentLibraryCategoryId))
                )
              )
            )
            OR (@ForPublic <> 1)
          )
        )
END
[+][-]03.13.2008 at 07:18PM PDT, ID: 21122584

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 14-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.13.2008 at 07:28PM PDT, ID: 21122629

View this solution now by starting your 14-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: SQL Query Syntax, SQL Server 2005
Tags: SQL 2005
Sign Up Now!
Solution Provided By: chapmandew
Participating Experts: 1
Solution Grade: A
 
 
 
Loading Advertisement...
20081112-EE-VQP-43 / EE_QW_2_20070628