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(); } }
public Xinchen.PrivilegeManagement.DTO.User AddUser(AddUserModel addUserModel) { Xinchen.PrivilegeManagement.DTO.User user = this._privilegeBase.GetUser(addUserModel.Username); if (user != null) { throw new ApplicationException("该名称已存在"); } using (TransactionScope scope = new TransactionScope()) { user = this._privilegeBase.AddUser(addUserModel.Username, addUserModel.Description, addUserModel.Status); foreach (int num in addUserModel.RoleIds) { this._privilegeBase.AddUserRole(num, user.Id); } scope.Complete(); } return(user); }
public Xinchen.PrivilegeManagement.DTO.User UpdateUser(UpdateUserModel updateUserModel, int[] rolesId) { using (TransactionScope scope = new TransactionScope()) { Xinchen.PrivilegeManagement.DTO.User user = this._privilegeBase.GetUser(updateUserModel.Id); if (user == null) { throw new ApplicationException("该用户不存在"); } user.Description = updateUserModel.Description; user.Status = updateUserModel.Status; user = this._privilegeBase.UpdateUser(user); var userRoles = this.PrivilegeBase.GetUserRoles(user.Id).ToList(); int[] numArray = rolesId; for (int i = 0; i < numArray.Length; i++) { Predicate <Xinchen.PrivilegeManagement.DTO.Role> match = null; int roleId = numArray[i]; if (match == null) { match = x => x.Id == roleId; } if (!userRoles.Exists(match)) { this.PrivilegeBase.AddUserRole(roleId, user.Id); } } foreach (Xinchen.PrivilegeManagement.DTO.Role role in userRoles) { if (!rolesId.Contains <int>(role.Id)) { this.PrivilegeBase.RemoveUserRole(role.Id, user.Id); } } scope.Complete(); return(user); } }
public User AddUser(string username, string description, BaseStatuses enabled) { using (TransactionScope scope = new TransactionScope()) { User entity = new User { Username = username, Status = enabled, Password = StringHelper.EncodePassword(username, "123456"), Id = this.PrivilegeContextProvider.GetRepository().Use<User>().GetSequenceId(), CreateTime = DateTime.Now, Description = description }; var reps = this.PrivilegeContextProvider.GetRepository(); reps.Use<User>().Add(entity); reps.SaveChanges(); scope.Complete(); return entity; } }
public User UpdateUser(User user) { using (var reps = PrivilegeContextProvider.GetRepository()) { reps.Use<User>().Update(user); reps.SaveChanges(); } return user; }
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; } }