Register  
Friday, March 12, 2010
Knowledge Base  


ActiReq Preview
  
Latest Blogs
  
Articles
05

Sometimes we need to know whether a user is a portal administrator or not. Let's see how simple it is.

A portal administrator can be either a "real" administrator, or the host user. The DotNetNuke.Entities.Users.UserInfo class contains the IsSuperUser property which states whether the user is the host administrator or not.

 If the user is not a host administrator, we need to check whether it is a portal administrator. To achieve this, we need to retrieve the user's roles and verify if one of them is "Administrators".

The first step is getting a UserInfo instance, containing the user's properties:

Dim objUsers As New UserController
Dim objUserInfo As UserInfo

objUserInfo = objUsers.GetUserByUsername(PortalSettings.PortalId, userName)

...

Next, we need to enumerate all roles associated to this user:

Dim objUsers As New UserController
Dim objUserInfo As UserInfo
Dim roleController As roleController = New roleController
Dim roles() As String
 

objUserInfo = objUsers.GetUserByUsername(PortalSettings.PortalId, userName)

roles = roleController.GetRolesByUser(objUserInfo.UserID, objUserInfo.PortalID)

...

The GetRolesByUser method returns an array of strings containing the name of the roles associated to the user..

The final step is to loop through this array, searching for an element equal to "Administrators":

Dim objUsers As New UserController
Dim objUserInfo As UserInfo
Dim roleController As roleController = New roleController
Dim roles(), role As String
Dim
isAdmin As Boolean
 

objUserInfo = objUsers.GetUserByUsername(PortalSettings.PortalId, userName)

roles = roleController.GetRolesByUser(objUserInfo.UserID, objUserInfo.PortalID)
For Each role In roles
  If
(role = "Administrators") Then
   
isAdmin = True
  End
If
Next


...

This is our final IsAdministrator method which returns true if the user is a portal or host administrator or not

Public Function IsAdministrator(ByVal username As String) As Boolean 
  Dim
objUsers As New UserController 
 
Dim objUserInfo As UserInfo = objUsers.GetUserByUsername(PortalSettings.PortalId, username) 
 
Dim isAdmin As Boolean 

 
isAdmin = objUserInfo.IsSuperUser 

  If
Not isAdmin Then 
    Dim
roleController As roleController = New roleController 
   
Dim roles(), role As String 

   
roles = roleController.GetRolesByUser(objUserInfo.UserID, objUserInfo.PortalID) 
   
For Each role In roles 
     
If (role = "Administrators") Then 
       
isAdmin = True 
      End
If 
    Next 
  End
If 

  Return
isAdmin 

End Function 

Post Rating

Comments

Neil
# Neil
Sunday, May 25, 2008 3:04 AM
Can't you just use the .IsInRole("Administrators") method of UserInfo?
almny
Thursday, October 29, 2009 7:58 AM
thianks man
it's working fine

Post Comment

Name (required)

Email (required)

Website

Enter the code shown above in the box below

    

 

stock icons

 

 

© 2006-2008 Developer's Corner - Powered by Elapsus   |  Privacy Statement  |  Terms Of Use