Advertisement
Guest User

AuthorizeMultipleAttribute

a guest
Jul 3rd, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. using Houseman.Erp.App_Start;
  9. using Houseman.Model.Dao.Enums;
  10. using Houseman.Model.Dto;
  11. using Houseman.Service.Service;
  12.  
  13.  
  14. namespace Houseman.Erp.Filters
  15. {
  16.     public class AuthorizeMultipleAttribute : AuthorizeAttribute
  17.     {
  18.         public string MultipleRoles { get; set; }
  19.         public UserDto User { get; set; }
  20.         public string Action { get; set; }
  21.         public ContextEnum Context { get; set; }
  22.         public static RoleService roleService;
  23.         public static UserService userService;
  24.         public static RoleRightService roleRightService;
  25.         public static List<UserDto> allUser;
  26.         public static List<RoleDto> allRole;
  27.         public static List<RoleRightDto> allRoleRights;
  28.         public static int FlagInit = 0;
  29.        
  30.  
  31.         public AuthorizeMultipleAttribute()
  32.         {
  33.             if(FlagInit==0)
  34.             {
  35.                 Init();
  36.             }
  37.         }
  38.  
  39.         public static void Init()
  40.         {
  41.             roleService = StructuremapMvc.StructureMapDependencyScope.Container.GetInstance<RoleService>();
  42.             userService = StructuremapMvc.StructureMapDependencyScope.Container.GetInstance<UserService>();
  43.             roleRightService = StructuremapMvc.StructureMapDependencyScope.Container.GetInstance<RoleRightService>();
  44.             allUser = userService.GetAll().ToList();
  45.             allRole = roleService.GetAll().ToList();
  46.             allRoleRights = roleRightService.GetAll().ToList();
  47.             FlagInit = 1;
  48.         }
  49.  
  50.        
  51.         protected override bool AuthorizeCore(HttpContextBase httpContext)
  52.         {
  53.            
  54.             var isAuthorized = base.AuthorizeCore(httpContext);
  55.             if (Action.Equals("First"))
  56.             {
  57.                 if (allRole.Any())
  58.                     return isAuthorized;
  59.             }
  60.             if (Action.Equals("FirstUser"))
  61.             {
  62.                 if (allUser.Any())
  63.                     return isAuthorized;
  64.             }
  65.  
  66.            
  67.             if (!isAuthorized)
  68.             {
  69.                 if (!allRole.Any())
  70.                 {
  71.                     return true;
  72.                 }
  73.                 if (!allUser.Any())
  74.                 {
  75.                     return true;
  76.                 }
  77.                 return false;
  78.             }
  79.  
  80.             var roleRights =allRoleRights.Where(roleRightDto => roleRightDto.Role.Id == User.Role.Id).Where(roleRightDto => roleRightDto.Context.Equals(Context));
  81.             foreach (var roleRightDto in roleRights)
  82.             {
  83.                 switch (Action)
  84.                 {
  85.                     case "View":
  86.                         return roleRightDto.View;
  87.                     case "Create":
  88.                         return roleRightDto.Create;
  89.                     case "Update":
  90.                         return roleRightDto.Update;
  91.                     case "Delete":
  92.                         return roleRightDto.Delete;
  93.                 }
  94.             }
  95.            
  96.             return false;
  97.         }
  98.  
  99.  
  100.         public override void OnAuthorization(AuthorizationContext filterContext)
  101.         {
  102.             if (!Action.Equals("First") && allRole.Any() && !Action.Equals("FirstUser") && allUser.Any())
  103.             {
  104.                 if (filterContext.HttpContext.User.Identity.Name.Equals(""))
  105.                 {
  106.                     filterContext.Result =
  107.                         new RedirectResult("/Home/Index");
  108.                 }
  109.                 else
  110.                 {
  111.                     var username = filterContext.HttpContext.User.Identity.Name;
  112.                     User = allUser.First(x => x.Username.Equals(username));
  113.                 }
  114.             }
  115.            
  116.             if (!AuthorizeCore(filterContext.HttpContext))
  117.             {
  118.  
  119.                 if (!allRole.Any())
  120.                 {
  121.                     base.OnAuthorization(filterContext);
  122.                 }
  123.  
  124.                 if (! allUser.Any())
  125.                 {
  126.                     base.OnAuthorization(filterContext);
  127.                 }
  128.                 filterContext.Result = filterContext.HttpContext.User.Identity.Name.Equals("") ? new RedirectResult("/Account/Login") : new RedirectResult("/Home/Index");
  129.             }
  130.  
  131.             else
  132.             {
  133.                 base.OnAuthorization(filterContext);
  134.             }
  135.         }
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement