code_junkie

ASP.NET MVC - adding action filters programmatically

Nov 13th, 2011
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. [LogRequest]
  2. [PermissionRequired(Permits.View_users, Permits.Edit_users)]
  3. public ActionResult Edit(int id, .....)
  4. {
  5. ...
  6. }
  7.  
  8. public class PermissionRequired : ActionFilterAttribute, IActionFilter
  9. {
  10. private readonly PermissionsList permits;
  11.  
  12. public PermissionRequired(params Permits[] perm)
  13. {
  14. permits = new PermissionsList(perm);
  15. }
  16.  
  17. #region IActionFilter Members
  18.  
  19. void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
  20. {
  21. ...
  22. IEnumerable<int> intersection = (from up in User.CurrentUser.UserPermission
  23. select up.PermissionID).ToList().Intersect(permits.Cast<int>());
  24. if (intersection.Count() != permits.Count)
  25. {
  26. filterContext.Result = null;
  27. HttpContext.Current.Response.Redirect("/Error/PermissionsRequired.htm");
  28. }
  29. }
  30.  
  31. #endregion
  32. }
Add Comment
Please, Sign In to add comment