Arrow icon
Back to Insights

Retrieving Role Information in your Orchard View Templates

April 14, 2013
Arra Derderian

Update to this Blog Entry!

Although the below blog entry details how to get at some detailed information from a Layout template, the real way to control navigation item permissions is to actually attach the content permissions part to a Custom Menu content type. This will let you set permissions on your custom links. See this blog entry to see how to do this.

If you need to only render information for specific users based on pre-defined Orchard CMS roles you can use this this snippet :

   var firstName = "";    bool isClient = false;    bool isResearch = false;        if (WorkContext.CurrentUser != null) {        firstName = ((TextField)((ContentItem)WorkContext.CurrentUser.ContentItem).Parts.SelectMany(p => p.Fields).First(f => f.Name == "FirstName")).Value;        var roles = ((ContentItem)WorkContext.CurrentUser.ContentItem).As<UserRolesPart>().Roles;        isClient = roles.Any(x => x == "Client");        isResearch = roles.Any(x => x == "Researcher");    }

This example shows extracting a First Name property for the user from the User Part as well, but we first had to install the Profile Module so we could add this field. We are showing the User's first name in the Layout.cshtml file and then secondly, we are switching which link to an account area is displayed based on the User's role.

@if (isResearch) {<li><a href="/users/research">Browse Market Research</a></li> }@if (isClient) {<li><a href="users/accounts">Browse My Accounts</a></li>}

This code will only show the appropriate link to each user type. If you are using the caching module make sure you test this as well. I have not tested it and have the feeling if you are using the Orchard Caching module you may get a cached version of the page based on the last user who logged in. I will update this when I find out!

About the Author:

Arra Derderian serves as the President and as a Lead Technical Architect for Cloud Construct. As a founder at Cloud Construct, Arra is involved in all levels of the business from new project engagements, project planning, and development.

He also serves as the founder of the Boston Orchard CMS User Group and is a member of the Windows Azure Insiders group.

Arra graduated from Northeastern University School with a Bachelor of Science degree in Computer Science.

Author Photo
Arra Derderian
Founder & Chairman
Arrow icon
Back to Insights

Let's talk about your project

Drop us a note
Arrow
Or call us at:  
1.617.903.7604

Let's talk about
your project

Drop us a note
Arrow
Or call us at:
1.617.903.7604

Let's talk about your project

Drop us a note
Arrow
Or call us at:  
1.617.903.7604