Допустим, у вас есть следующий вид
<div id="CallList">
@Html.Action("List","Call",new {id=Model.id})
</div>
<div class="Order">
@Html.Action("Create","Order",new {id=Model.id})
</div>
С последующими действиями:
[Authorize(Roles = "manager, caller")] //if a user is not 'manager' or 'caller'
public PartialViewResult List() // nothing is shown
{
//...private
return PartialView();
}
[Authorize(Roles = "manager, admin")]
public PartialViewResult Create()
{
//...private
return PartialView();
}
Тогда вы можете ограничить доступ через этот доп. method
public static MvcHtmlString ActionBaseRole(this HtmlHelper value, string actionName, string controllerName, object routeValues , IPrincipal user)
{
bool userHasRequeredRole = false;
Type t = Type.GetType((string.Format("MyProject.Controllers.{0}Controller",controllerName))); // MyProject.Controllers... replace on you namespace
MethodInfo method = t.GetMethod(actionName);
var attr = (method.GetCustomAttribute(typeof(AuthorizeAttribute), true) as AuthorizeAttribute);
if (attr != null)
{
string[] methodRequeredRoles = attr.Roles.Split(',');
userHasRequeredRole = methodRequeredRoles.Any(r => user.IsInRole(r.Trim())); // user roles check in depends on implementation authorization in you site
// In a simple version that might look like
}
else userHasRequeredRole = true; //method don't have Authorize Attribute
return userHasRequeredRole ? value.Action(actionName, controllerName, routeValues) : MvcHtmlString.Empty;
}
Пример использования:
@Html.ActionBaseRole("List","Call",new {id=Model.id},User)
Обратите внимание, что ваши атрибуты безопасности отличаются, но я думаю, вы можете легко заменить его.