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