// 将指定的用户从角色中移除。 public void UnassignRole(User user, Role role = null) { if (user == null) throw new ArgumentNullException("user"); Expression<Func<UserRole, bool>> specExpression = null; if (role == null) specExpression = ur => ur.UserId == user.Id; else specExpression = ur => ur.UserId == user.Id && ur.RoleId == role.Id; UserRole userRole = _userRoleRepository.GetBySpecification(Specification<UserRole>.Eval(specExpression)); if (userRole == null) return; _userRoleRepository.Remove(userRole); _repositoryContext.Commit(); }
public static UserRole CreateUserRole(User user, Role role) { return new UserRole() { UserId = user.Id, RoleId = role.Id }; }
// 将指定的用户赋予特定的角色。 public UserRole AssignRole(User user, Role role) { if (user == null) throw new ArgumentNullException("user"); if (role == null) throw new ArgumentNullException("role"); var userRole = _userRoleRepository.GetBySpecification(Specification<UserRole>.Eval(ur => ur.UserId == user.Id)); if (userRole == null) { userRole = UserRole.CreateUserRole(user, role); _userRoleRepository.Add(userRole); } else { userRole.RoleId = role.Id; _userRoleRepository.Update(userRole); } _repositoryContext.Commit(); return userRole; }