Esempio n. 1
0
 public void RegAdmin(string userName, string password, string password2)
 {
     if (string.IsNullOrEmpty(userName))
     {
         throw new ArgumentNullException("userName");
     }
     if (string.IsNullOrEmpty(password))
     {
         throw new ArgumentNullException("password");
     }
     if (string.IsNullOrEmpty(password2))
     {
         throw new ArgumentNullException("password2");
     }
     if (password != password2)
     {
         throw new ApplicationException("两次输入密码不一致");
     }
     using (TransactionScope scope = new TransactionScope())
     {
         Department department = this._privilegeBase.AddDepartment("系统部门", "系统默认的部门");
         Xinchen.PrivilegeManagement.DTO.Role role = this._privilegeBase.AddRole("系统角色", BaseStatuses.Valid, "系统角色", new int?(department.Id));
         Xinchen.PrivilegeManagement.DTO.User user = new Xinchen.PrivilegeManagement.DTO.User
         {
             DepartmentId = new int?(department.Id),
             CreateTime   = DateTime.Now,
             Username     = userName,
             Password     = password,
             Status       = BaseStatuses.Valid
         };
         user = this._privilegeBase.RegAdmin(user);
         this._privilegeBase.AddUserRole(role.Id, user.Id);
         scope.Complete();
     }
 }
Esempio n. 2
0
 public Xinchen.PrivilegeManagement.DTO.Role AddRole(AddRoleModel addRoleModel, int[] privileges)
 {
     using (TransactionScope scope = new TransactionScope())
     {
         Xinchen.PrivilegeManagement.DTO.Role role = this._privilegeBase.GetRole(addRoleModel.Name);
         if (role != null)
         {
             throw new ApplicationException("该名称已存在");
         }
         role = this._privilegeBase.AddRole(addRoleModel.Name, addRoleModel.Status, addRoleModel.Description, null);
         foreach (int num in privileges)
         {
             this._privilegeBase.AddRolePrivilege(role.Id, num);
         }
         scope.Complete();
         return(role);
     }
 }
Esempio n. 3
0
 public Role AddRole(string name, BaseStatuses status, string description, int? departmentId = new int?())
 {
     Role entity;
     using (TransactionScope scope = new TransactionScope())
     {
         using (var reps = PrivilegeContextProvider.GetRepository())
         {
             entity = new Role
             {
                 Name = name,
                 DepartmentId = departmentId,
                 Status = status,
                 Id = reps.Use<Role>().GetSequenceId(),
                 Description = description
             };
             entity = reps.Use<Role>().Add(entity);
             reps.SaveChanges();
         }
         scope.Complete();
     }
     return entity;
 }
Esempio n. 4
0
 public Xinchen.PrivilegeManagement.DTO.Role UpdateRole(UpdateRoleModel updateRoleModel, int[] privilegeIds)
 {
     using (TransactionScope scope = new TransactionScope())
     {
         Xinchen.PrivilegeManagement.DTO.Role role = this._privilegeBase.GetRole(updateRoleModel.Id);
         if (role == null)
         {
             throw new ApplicationException("该角色不存在");
         }
         role.Description = updateRoleModel.Description;
         role.Status      = updateRoleModel.Status;
         role             = this._privilegeBase.UpdateRole(role);
         var   rolePrivileges = this.PrivilegeBase.GetRolePrivileges(role.Id).ToList();
         int[] numArray       = privilegeIds;
         for (int i = 0; i < numArray.Length; i++)
         {
             Predicate <Xinchen.PrivilegeManagement.DTO.Privilege> match = null;
             int privilegeId = numArray[i];
             if (match == null)
             {
                 match = x => x.Id == privilegeId;
             }
             if (!rolePrivileges.Exists(match))
             {
                 this.PrivilegeBase.AddRolePrivilege(role.Id, privilegeId);
             }
         }
         foreach (Xinchen.PrivilegeManagement.DTO.Privilege privilege in rolePrivileges)
         {
             if (!privilegeIds.Contains <int>(privilege.Id))
             {
                 this.PrivilegeBase.RemoveRolePrivilege(privilege.Id, role.Id);
             }
         }
         scope.Complete();
         return(role);
     }
 }
Esempio n. 5
0
 public Role UpdateRole(Role role)
 {
     using (var reps = PrivilegeContextProvider.GetRepository())
     {
         reps.Use<Role>().Update(role);
         reps.SaveChanges();
     }
     return role;
 }
Esempio n. 6
0
 public User RegAdmin(User user)
 {
     if (user == null)
     {
         throw new ArgumentNullException("username");
     }
     var reps = PrivilegeContextProvider.GetRepository();
     using (reps)
     {
         var userUnit = reps.Use<User>();
         if (userUnit.Any(x => true))
         {
             throw new ApplicationException("系统已存在用户,无法直接注册管理员");
         }
         var roleUnit = reps.Use<Role>();
         var role = new Role()
         {
             Description = PrivilegeContextProvider.SystemRoleName,
             Id = roleUnit.GetSequenceId(),
             Name = PrivilegeContextProvider.SystemRoleName
         };
         roleUnit.Add(role);
         User entity = new User
         {
             Password = StringHelper.EncodePassword(user.Username, user.Password),
             Status = BaseStatuses.Valid,
             CreateTime = user.CreateTime,
             Description = user.Description,
             Username = user.Username,
             DepartmentId = user.DepartmentId,
             Id = userUnit.GetSequenceId()
         };
         user = userUnit.Add(entity);
         UserRole userRole = new UserRole();
         userRole.RoleId = role.Id;
         userRole.UserId = entity.Id;
         reps.Use<UserRole>().Add(userRole);
         reps.SaveChanges();
         HttpRuntime.Cache.Remove("FirstStart");
         return user;
     }
 }