ASP.NET MVC 5 Customise Bootstrap navbar based on User Role - asp.net-mvc

I'm using the ASP.NET MVC 5 built in authentication methods. I would like to show and hide links (in the menu navbar) based on the role the user is in.
Has anyone acheived this?
Where would be a starting point?

Just wrap your links in:
#if (User.IsInRole("SomeRole"))
{
...
}

You can use MvcSiteMap for this. It has a feature called SecurityTrimming which uses the [Authorize] attribute on your action methods to decide whether or not to display the menu item.
I know it's frowned upon to post a links in answers but I found this blog post very useful.
In addition to the role-based menu visibility, I added custom attributes to the MvcSiteMapNodes to determine visibility of links that were accessible to users but I didn't want shown in the menu (e.g. Edit pages), and I also added icon attributes which allowed me to use the bootstrap menu icons e.g:
<mvcSiteMapNode title="Till" controller="Home" action="Index" area="Till" iconClass="icon-home" visibility="true">
I went a bit off-topic there, but I just wanted to highlight how flexible MvcSiteMap is.

Two things I do. Either
User.IsInRole(admin)
{link somewhere}
Or what I personally do is because I use areas I have a viewstart in area admin which links to admin shared viewmodel then in admin shared view that links to the public view.
In the admin shared view. I set up a section. Inside this section I define extra nav details what that specific role will see and add them in a list tag
Then inside public shared view I then use (on phone can't remember exact name something like)
Html.IsSectionDefined
I personally like the second method using areas and sections both would work fine but with the second I find it much cleaner and you can be so much more specific and much simpler

Related

Active Admin Customization in Ruby On Rails

I'm using active admin gem. In one table i want admins page to have some extra fields apart from the default functionalities.
Is it possible to have a default active admin page + a provision to render some html.
I want something like this in picture
The extra feature that i was talking was about push notification and message and that text area part
If you want a page with your own content, you could register a page: http://www.activeadmin.info/docs/10-custom-pages.html
But if you want to customise the default html strucure of the index page, you have to re-open the ActiveAdmin::Views::Pages module and override the build_page_content method. You can do this inside your app, for example create a new file in app/lib folder and override the method.
https://github.com/gregbell/active_admin/blob/master/lib/active_admin/views/pages/base.rb#L62
or if you want to customise the index table:
https://github.com/gregbell/active_admin/blob/master/lib/active_admin/views/pages/index.rb
Not a nice solution, but it's work. :/
Unfortunately, it seems index doesn't support custom rendering:
https://github.com/gregbell/active_admin/issues/813#issuecomment-3059957
However, you might have luck using panel, except in my experience that renders above the table ActiveAdmin generates.

Change CSS style using drop down menu in MVC

I'm trying to make a drop down menu that has a bunch of items (Amelia, Cerulean, Cosmo, Cyborg, Flatly, Journal). Each of these items represent a css file.
When one of them is selected I want my website to take this selected css file and apply it to the website.
I would like the drop down menu to interact with jquery, meaning when a item is selected jquery takes over and makes a asyn/ajax call to some mvc actionresult.
By the way I'm using MVC 5.
I hope someone can help me sketch the initial groundwork.
I've implemented this in my application.
I'm not sure what to tell you though. It's easier when we have an attempted solution to fix.
Here's an overview of how mine works:
I have created a controller called SharedController. The purpose of it is to contain various actions that render common actions. All of the actions are considered ChildActionOnly.
My _Layout uses RenderAction to render the action NavbarPartial which is in my SharedController.
More importantly the Navbar partial then uses RenderAction to render the action ThemeListPartial. This action is responsible for getting a list of available themes. The list of available themes is determined at applications startup. I've created a ThemeFinder class and ThemeRepository class that are responsible for finding and storing themes. The ThemeFinder finds themes by expressions that you give it. In a new class called App_Start/ThemeConfig I've given it only one expression - "~/Content/themes/{name}.bootstrap.css". This will find all themes with that naming convention in that location.
My razor code will take the ViewModel and display a dropdown menu in the navbar.
To get the themes to change my dropdown menu contains an AJAX link to an action called SaveTheme in ThemeController. This action takes a theme name as a string and tries to save it in a cookie for the user.
If the theme is found and saved successfully, the action responds with a success message.
jQuery then changes the theme by finding the associated link attribute and changing the HREF contents to the new theme. It knows the new theme relative URL because I have it stored in data attributes.
I completed this before I made the switch to AngularJs. The one thing I plan to go back and change is to cut out as much (maybe all) jQuery as possible and replace it with better code.

How to display a different breadcrumb title for the same controller + action method?

I have a Home controller and a Business controller. The business controller has a few action methods on it: Search, Create, Update, Delete.
On my home page I have links to the Search and Create views on the Business controller. The Search view also has a link to the Create view.
I want the bread crumb to look like the following when Create is accessed from the home page:
Home > Create
…and i want it to look like the following when Create is accessed from the Search page:
Home > Business > Create
In both the cases the controller/action method is the same, but the breadcrumb I want displayed is different. Is it possible to do this using MvcSiteMapProvider?
As far as I know this is not supported out of the box. Meaning that you have to adapt the HtmlHelper template to you needs, see https://github.com/maartenba/MvcSiteMapProvider/wiki/HtmlHelper-functions.
The only way this can be done is if you add some information to your route to tell one request apart from the other, then you can configure 2 different nodes to create both breadcrumb trails.
I have a working example of how to do that on my blog: http://www.shiningtreasures.com/post/2013/08/10/mvcsitemapprovider-4-seo-features#canonical-tag. Make sure to check out the code download.

How to use a 'switch' to control Controller's Action choices in Rails?

In my new Rails project,I want to provide my user two kinds of category view mode
:one is List mode,other is Detail mode.
In List mode,the category page will just show the title of the article.In Detail mode,the page will show the title and the content.I want to use a 'switch' to control this two mode,when user choice 'List mode',all the category page will be shown in List mode.When they choice 'Detail mode',all the category page will be shown in Detail mode.
I think I could use different Action control this two view mode,so my question is how to set a 'switch' to control these different Actions in the whole site? A Variable?
Also welcome another solution.
Thank you.
You could use a variable and railscasts shows the way again. The new design of railscasts.com implements this sort of a design, where-in it provides an option to view articles in a list/grid fashion.
Since, railscasts is an open-source project, I encourage you to browse through the code in
episodes_controller
and the view episodes#index (he uses params[:view] to switch between different listing styles)
And you'll know exactly what to do.

asp.net mvc generic/custom views

the routes are as follows
site.com/{section1}/{page1}
site.com/{section1}/{page2}
site.com/{section2}/{page3}
etc.
the pages are 2 column each where the left bar is different for each section
the page content is fetched from the database based on section + page combination
I'd like to make the page markup/layout/css be done/uploaded by the user
Questions:
hopefully this is implemented somewhere? any links?
how much can this be dumbed down?
what/how can checks be enforced so that the few required tags/markup are present/visible?
any gotchas?
figured it out using ActionAttribute

Resources