Tuesday, May 17, 2011

Add User To SSRS Reports In A Folder

RS.exe is located at \Program Files\Microsoft SQL Server\100\Tools\Binn. You can run the utility from any folder on your file system.

'=====================================================================
'  File:     AddUserToReportsInAFolder.rss
'
'  Summary:  Demonstrates a script that can be used with RS.exe to
'                set security on an item in Reporting Services.
'
'---------------------------------------------------------------------
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
' KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
' IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
' PARTICULAR PURPOSE.
'=====================================================================*/
'
' Variables that are passed on the command line with the -v switch:
' userName - the name of the user for which to add a policy
' roleName - the name of the role to apply for the user (i.e. Browser, Content Manager)
' itemPath - the path of the item for which you want to add the policy (i.e. /SampleReports)
' keepCurrentPolicy - whether to keep the current policy and add the new one
'
' Sample command line:
' rs -i AddItemSecurity.rss -s http://localhost/reportserver -v userName="MyTestUser"
'    -v roleName="Browser" -v itemPath="/SampleReports" -v keepCurrentPolicy="True"

Public Sub Main()
   Dim isRoot As Boolean = False
   Dim inheritParent As Boolean
  
   Dim policies() As Policy
   Dim newPolicies() As Policy
   Dim policy As New Policy()
   Dim roles(0) As Role
   roles(0) = New Role()
   roles(0).Name = roleName
   policy.Roles = roles
   policy.GroupUserName = userName
  
   While Not isRoot
      ' Once the root of the catalog is reached,
      ' stop applying policies
      If itemPath = "/" Then
         isRoot = True
      End If
     
      policies = rs.GetPolicies(itemPath, inheritParent)
        
      ' If the user selects not to keep inherited or current policy,
      ' empty the policy
      If Not keepCurrentPolicy = "True" Then
         policies = Nothing
      End If
      newPolicies = AddNewPolicy(policy, policies)
      rs.SetPolicies(itemPath, newPolicies)
      itemPath = GetParentPath(itemPath)
   End While
   Console.WriteLine("Policy successfully set.")
End Sub 'Main
  

' Method to parse the path of an item and retrieve
' the parent path of an item
Private Function GetParentPath(currentPath As String) As String
   Dim delimiter As String = "/"
   Dim rx As New System.Text.RegularExpressions.Regex(delimiter)
   Dim childPath As String() = rx.Split(currentPath)
  
   Dim parentLength As Integer = childPath.Length - 1
   Dim parentPath(parentLength) As String
  
   Dim i As Integer
   For i = 0 To parentLength - 1
      parentPath(i) = childPath(i)
   Next i
   If parentPath.Length = 1 Then
      Return "/"
   Else
      Return String.Join("/", parentPath)
   End If
End Function 'GetParentPath
  
' Takes the policy to add and applies it to the current set
' of policies if applicable
Private Function AddNewPolicy(policyToAdd As Policy, policies() As Policy) As Policy()
   If Not (policies Is Nothing) Then
      Dim policy As Policy
      For Each policy In  policies
         If policy.GroupUserName = policyToAdd.GroupUserName Then
            Throw New Exception("The supplied User policy already exists for the item.")
         End If
      Next policy
      Dim list As New System.Collections.ArrayList(policies)
      list.Add(policyToAdd)
      Return CType(list.ToArray(GetType(Policy)), Policy())
   Else
      policies = New Policy(0) {}
      policies(0) = policyToAdd
      Return policies
   End If
End Function 'AddNewPolicy

No comments:

Post a Comment