How do I get Orchard User Role from ContentManager?

Topics: General
Sep 14, 2012 at 3:00 PM

I am new to LINQ and have read through several posts here and trying to figure out how to extract the role of the current user through the ContentManager.  I want to do this so that I can pass it to the view and set some things in the UI based on the role the user has.  i.e. I need to limit access for an Instructor role (which is a role specific to this implementation). 

So far I have managed to get to the IContentManager and IOrchardServices, and am able to get a list of users with this code (which I pulled from this post).  How can I extract the user role from users below?  Or is there a different/better way of handling this?

 

            var userQuery = _orchardServices.ContentManager.Query().ForType("User");
            var users = userQuery.List();

 

Perhaps I can pull it directly from the IOrchardServices instance? I am currently getting the Id like this:

 

_orchardServices.WorkContext.CurrentUser.Id

Sep 14, 2012 at 7:23 PM

Hey. This will get a list of a users roles because remember, a user can have multiple roles. The variable roles is a list of strings.

var user = orchardServices.WorkContext.CurrentUser;
var iUser = user.As<IUserRoles>();
var roles = iUser.Roles;
Sep 16, 2012 at 1:55 AM

Perfect - thanks!!

Feb 24, 2014 at 11:51 AM
Hi tone33,
I am not getting As<IUserRoles>, As and IUserRoles in my visual studio. What reference should I use here?
Feb 24, 2014 at 11:52 AM
Hazza wrote:
Hey. This will get a list of a users roles because remember, a user can have multiple roles. The variable roles is a list of strings. var user = orchardServices.WorkContext.CurrentUser; var iUser = user.As<IUserRoles>(); var roles = iUser.Roles;
I am not getting As<IUserRoles>, As and IUserRoles in my visual studio. What reference should I use here?
Feb 24, 2014 at 1:21 PM
Edited Feb 24, 2014 at 1:22 PM
using Orchard.ContentManagement;
using Orchard.Roles.Models;

You will probably need to add a reference to Orchard.Roles project in your references too. A tool like ReSharper makes resolving references in Orchard very easy ;)