0
votes

I am creating a custom HtmlHelper extension for Html.RenderAction. My Parent view will contain many different partial views which will be rendered byt calling Html.Renderaction. But the the admin can sort off switch of a partial view for a role or he can completely deactivate the action for entire application So i am planning to have an extension method for Html.RenderAction which will in turn check for the role and see if the role has access to a particular action. This role to action mapping is dine in xml and I am planning to load this xml as in memory data structure only once. And have the html helper extension look into that data structure. Is that a good way to go? Any better ways?

 @section column2 {
        @{Html.RenderActionIfIfAllowed("DashboardItem_Users", "DashBoard",User);}
        }

        @section column3 {
        @{Html.RenderActionIfIfAllowed("DashboardItem_Orders", "DashBoard", User);}
        }

I have to render the above partialviews. So i have created a html helper extension called Html.RenderActionIfIfAllowed.

public static class HtmlHelperExtensions 
{
   public static void RenderActionIfIfAllowed<TModel>(this HtmlHelper<TModel> htmlHelper, string actionName, string controllerName, IPrincipal user)
    {
       //We can use the layour manager class to check if a particular role has access to an action and also if the action is active.
       //Hard coding here just for demo purpose
        if (user.IsInRole("Admin") && actionName != "DashboardItem_Users")
        {
            System.Web.Mvc.Html.ChildActionExtensions.RenderAction(htmlHelper, actionName, controllerName);
        }
        else if (user.Identity.IsAuthenticated && !user.IsInRole("Admin"))
        {
            System.Web.Mvc.Html.ChildActionExtensions.RenderAction(htmlHelper, actionName, controllerName);
        }
    }

}

The reason for doing that way is because we would like to dynamically show or not show a aprtial view to user based on whether the view is active or not. We will read an xml file that will say whether the view is active not for a user and render it accordingly

1
Hello Hari, I can't understand the question correctly. Can you try to explain yourself better? perhaps with code examples of what you're trying to accomplish?amhed
The only thing wrong here is that you have hard-coded checks for things like the "DashboardItems_Users" actionName. The purpose of abstracting a method like this is to write less code on the View which improves readability. If you can find a way for the checks to be generic (like reading from a role list in XML, or from permissions specified on a Database Table) then yeah, I think this is good :)amhed

1 Answers

0
votes

I used to create ViewModel for this and set bool properties

public class DashBoardViewModel{

public DashBoard dashBoard{get;set;}

bool showItemDashBoard{get;set;}

bool showOrderDashBoard{get;set;}

}

In Controller I validate user role and set those boolean properties.

In View

if(Model.showItemDashBoard){
  @Html.RenderAction("Action","Controller")
}