A Practical Guide to SharePoint 2013

A Practical Guide to SharePoint 2013
A Practical Guide to SharePoint 2013 - Book by Saifullah Shafiq

Saturday, June 3, 2006

Checking User Permissions in SharePoint

Following is the code to check the rights of the users. Following code will show only those sites for which user has read privileges.

    Function CheckRights(ByVal sitePath As String, Byval strUser as string) As Boolean
    //sitePath contains the site that you retrieved in the code shown above
    // strUser contains the user for whom you want to check the rights and retrieve sites
        Try
            Dim blnStatus As Boolean = False
            If Not sitePath Is Nothing Or Not sitePath = "" Then
                Dim rtn As String() = parseURL(sitePath)
                    Dim siteCollection As SPSite
                    siteCollection = New SPSite(sitePath)
                    Dim site As SPWeb = siteCollection.OpenWeb(rtn(0))
                    Dim allUsers As SPUserCollection = site.Users
                    Dim user As SPUser
                    For Each user In allUsers
                        If user.LoginName.ToUpper = strUser Then
                            Dim allGroups As SPRoleCollection = user.Roles
                            Dim group As SPRole
                            For Each group In allGroups
                                Dim right As Integer
                                right = group.PermissionMask And SPRights.ViewListItems
                                If right = SPRights.ViewListItems Then
                                    blnStatus = True
                                    Return blnStatus
                                    Exit Function
                                End If
                            Next
                        End If
                    Next

                Return blnStatus
            End If
        Catch ex As Exception
            //Exception handling code here!
        End Try
    End Function

The function shown above (CheckRights) uses another function called as parseURL(). Following is the code for the parseURL() function:

Public Function parseURL(ByVal _fullPath As String) As String()
_fullPath = System.Web.HttpUtility.UrlDecode(_fullPath)
Dim site As SPSite = New SPSite(_fullPath)
Dim web As SPWeb = site.RootWeb
Dim webPath As String = _fullPath.Replace(web.Url & "/", "")
Dim rtnObject As String() = {"", "", ""}
Dim w As SPWeb
Dim folderPath As String = ""
Dim docLibPath As String = ""
Dim i, j As Integer
While CType(w, Object) Is Nothing
Try
w = site.OpenWeb(webPath)
Dim s As String = w.ID.ToString
'//above will raise the exception too
Catch ex As Exception
w = Nothing
i = webPath.LastIndexOf("/")
If i = -1 Then
Exit While '//means the end of subsite lookup
End If
j = webPath.Length
webPath = webPath.Remove(i, j - i)
End Try
End While
'//if there was some sub-site object then
If Not CType(w, Object) Is Nothing Then
folderPath = _fullPath.Substring(_fullPath.IndexOf(webPath) + webPath.Length + 1)
If folderPath.IndexOf("/") > 0 Then
docLibPath = folderPath.Substring(0, folderPath.IndexOf("/"))
folderPath = folderPath.Remove(0, folderPath.IndexOf("/") + 1)
End If
If docLibPath = webPath Then webPath = "" '_fullPath
rtnObject(0) = webPath
rtnObject(1) = docLibPath
rtnObject(2) = folderPath
Else '//there was no sub-site obect was found
folderPath = _fullPath.Substring(_fullPath.IndexOf(webPath))
If folderPath.IndexOf("/") > 0 Then
docLibPath = folderPath.Substring(0, folderPath.IndexOf("/"))
folderPath = folderPath.Remove(0, folderPath.IndexOf("/") + 1)
End If
If docLibPath = webPath Then webPath = "" '_fullPath
rtnObject(0) = webPath
rtnObject(1) = docLibPath
rtnObject(2) = folderPath
End If
Return rtnObject
End Function

Note: I know this will seem a bit complicated to you if you are a beginner but believe me it is not that complicated. It's pretty straight forward.
1. You retrieve sites from the portal.
2. You plug in CheckRights() function that checks whether the site has a user you passed in as a parameter and whether that user has reader rights.
3. parseURL() function is used by the CheckRights() function.
Just copy and paste the above functions, you may need to do minor tweaking and it should work fine.

-SSA

No comments:

Post a Comment