Displaying a button based on the user role.

Topics: Customizing Orchard, Writing modules
Apr 30, 2012 at 11:00 PM

Hi,

I created a custom Content Type 'News'. I created a page that displays a list of News type Content Item.All this is done using Orchard's configuration and doesn't involve any piece of code

Then, I also developed a module to display and edit News type content item.

Now, I have to put a button e.g. 'Create News' on the UI which would be displayed to the user only if he is in a role defined by admin.

How can this be achieved?

Coordinator
Apr 30, 2012 at 11:19 PM

Instead of relying on roles, you should rely on permissions. Look at any Permissions.cs file and follow the usage of the permissions defined therein.

Apr 30, 2012 at 11:30 PM

Agreed. But, still I would like to know how to access the permissions to be able to decide, when to display or hide a button. The example I saw in Orchard performs a check in the controller and  displays a message that "You a not authorized to perform this action".

What I need is something before this step where I can decide to hide or display the button.

Thanks!

Coordinator
Apr 30, 2012 at 11:49 PM

Before the controller? Why? How would it even know anything about your button?

May 1, 2012 at 12:02 AM

What I meant is the my custom module controllers is just for displaying the content type in Create\Edit mode. This is a separate page than the one I want to display the button on.

The button need to be hooked onto the page which I configured via the Orchard, and this is where I have to make a check for a user permission. If he has the permissions, he sees the button, clicks on it and sees the Content page rendered by my module.  I hope it makes sense.

Coordinator
May 1, 2012 at 12:13 AM

That button is part of a template, and that template corresponds to a part. The right place to put that code is the driver for that part.

May 1, 2012 at 4:01 PM

Ok, thanks. I think that would be the best approach. Another question, I have seen tutorials where using Shape Tracer, a new custom view can be generated and then any custom code can be placed into it. I was initially thinking of following that approach but wasn't sure if that's what is correct or not? Could you please advise what are the key differences between these two methods and when should one be used over the other. I am kind of confused.. Thanks !!

Coordinator
May 1, 2012 at 4:27 PM

As opposed to what? Writing it manually? Then the difference between approaches is that in one case you write more code yourself.

May 1, 2012 at 4:45 PM

Using Shape tracer to generate a custom view and checking permissions there V/s writing a part for the button. Which is the case that involves more code ?

Coordinator
May 1, 2012 at 4:50 PM

oh, I did not understand that you wanted to check for permissions from the view. No, that would be dirty. Do it from a driver.