Get current Role of logged in user

kalpak shambharkar asked on June 29, 2017 16:49

I have a scenario where I want to access the role of current logged in user with help of macro, how can I access the role, is there any macro method.

Correct Answer

Trevor Fayas answered on June 29, 2017 19:13

This would be the macro to get a user's roles, it is a collection of RoleInfo objects though, Ilesh is correct if you want to do a check, but if you want to get all a user's roles, this is it.

{% SiteObjects.Roles.Where("RoleID in (Select RoleID from CMS_UserRole where UserID = '"+MembershipContext.AuthenticatedUser.UserID+"')") #%}

Another note, this is the Site roles, it won't include the Global roles, you would have to call the below for Global Roles

{% GlobalObjects.Roles.Where("RoleID in (Select RoleID from CMS_UserRole where UserID = '"+MembershipContext.AuthenticatedUser.UserID+"')") #%}

Another option is to make a custom macro method that can return the user's roles, may be cleaner!.

1 votesVote for this answer Unmark Correct answer

Recent Answers


Ilesh Mistry answered on June 29, 2017 17:31

You could use CurrentUser.IsInRole() and then do something with that role you're looking for?

0 votesVote for this answer Mark as a Correct answer

Development Support Avastonetech answered on June 29, 2017 19:13 (last edited on June 29, 2017 19:13)

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.