Example #1
0
 public void AddRolePrivilege(int roleId, int privilegeId)
 {
     RolePrivilege entity = new RolePrivilege
     {
         PrivilegeId = privilegeId,
         RoleId = roleId
     };
     using (var reps = PrivilegeContextProvider.GetRepository())
     {
         reps.Use<RolePrivilege>().Add(entity);
         reps.SaveChanges();
     }
 }
Example #2
0
 /// <summary>
 /// 更新注册权限枚举
 /// </summary>
 /// <param name="enumType"></param>
 public static void RegisterPrivileges(Type enumType)
 {
     int num2;
     string[] names = Enum.GetNames(enumType);
     Array values = Enum.GetValues(enumType);
     int length = names.Length;
     var set = new HashSet<string>();
     for (num2 = 0; num2 < length; num2++)
     {
         if (set.Contains(names[num2]))
         {
             throw new ArgumentOutOfRangeException("权限名称重复:" + names[num2]);
         }
         set.Add(names[num2]);
     }
     using (TransactionScope scope = new TransactionScope())
     {
         var privilegeProvider = ConfigManager.PrivilegeProvider.GetRepository();
         var sysRole = privilegeProvider.Use<Role>().Get(x => x.Name == ConfigManager.PrivilegeProvider.SystemRoleName);
         Dictionary<int, Privilege> sysRolePrivileges = new Dictionary<int, Privilege>();
         if (sysRole != null)
         {
             sysRolePrivileges =
                 (from rp in privilegeProvider.Use<RolePrivilege>().Query
                  join p in privilegeProvider.Use<Privilege>().Query on rp.PrivilegeId equals p.Id
                  join r in privilegeProvider.Use<Role>().Query on rp.RoleId equals r.Id
                  where r.Name == sysRole.Name
                  select p).ToDictionary(x => x.Id);
         }
         for (num2 = 0; num2 < length; num2++)
         {
             Privilege privilege;
             string name = names[num2];
             int id = (int)values.GetValue(num2);
             PrivilegeDescriptionAttribute attribute = AttributeHelper.GetAttribute<PrivilegeDescriptionAttribute>(enumType.GetField(name));
             if (privilegeProvider.Use<Privilege>().Any(x => x.Id == id))
             {
                 privilege = privilegeProvider.Use<Privilege>().Get(x => x.Id == id);
                 if ((privilege.Name != name)
                     || ((attribute != null) && ((attribute.Description != privilege.Description))))
                 {
                     privilege.Description = (attribute == null) ? name : attribute.Description;
                     privilege.Name = name;
                 }
             }
             else
             {
                 privilege = new Privilege
                 {
                     Description = (attribute == null) ? name : attribute.Description,
                     Name = name,
                     Id = id
                 };
                 privilegeProvider.Use<Privilege>().Add(privilege);
             }
             if (!sysRolePrivileges.ContainsKey(privilege.Id))
             {
                 var rolePrivilege = new RolePrivilege()
                 {
                     RoleId = sysRole.Id,
                     PrivilegeId = privilege.Id
                 };
                 privilegeProvider.Use<RolePrivilege>().Add(rolePrivilege);
             }
         }
         privilegeProvider.SaveChanges();
         scope.Complete();
     }
 }