public ChangeRoleModel GetChangeRoleModel() { int userId = AuthenticationService.CurrentUser.Id; User user = UserDao.FindById(userId); if(user == null) throw new ValidationException(string.Format("Не могу загрузить пользователя с id {0}",userId)); List<UserRolesDto> usersAndRoles = GetUserRoles(user); if(usersAndRoles.Count == 0) throw new ArgumentException("Отсутствуют роли в системе для данного пользователя"); IList<Role> allRoles = RoleDao.LoadAll(); List<IdLongNameDto> roles = new List<IdLongNameDto>(); foreach (UserRolesDto dto in usersAndRoles) { foreach (UserRole role in dto.roles) { Role r = allRoles.Where(x => x.Id == (int)role).FirstOrDefault(); if(r == null) throw new ArgumentException(string.Format("Не могу загрузить роль с id {0}",(int)role)); roles.Add(new IdLongNameDto { Id = (ulong)r.Id * 100000 + (ulong)dto.user.Id, Name = r.Name + " " + dto.user.Login }); } } ChangeRoleModel model = new ChangeRoleModel { Roles = roles}; return model; }
public ActionResult ChangeRole(ChangeRoleModel model, string returnUrl="") { LoginBl.SetUserRole(model); if (string.IsNullOrEmpty(returnUrl.Trim())) return RedirectToAction("Index", "Home"); else return Redirect(returnUrl); }
public void SetUserRole(ChangeRoleModel model) { int userId = AuthenticationService.CurrentUser.Id; User user = UserDao.FindById(userId); if (user == null) throw new ValidationException(string.Format("Не могу загрузить пользователя с id {0}", userId)); UserRole role = (UserRole)(model.RoleId/100000); int modelUserId = (model.RoleId % 100000) <= int.MaxValue ? (int)(model.RoleId % 100000) : -1; User modelUser = UserDao.FindById(modelUserId); if (modelUser == null) throw new ValidationException(string.Format("Не могу загрузить пользователя с id {0}", modelUserId)); if((modelUser.RoleId & (int)role) == 0) throw new ValidationException(string.Format("Пользователь с id {0} не имеет роли {1}", modelUserId,(int)role)); //User first = GetFirstUserWithRole(user, model.RoleId); //if(first == null) // throw new ValidationException("Не найдено активных пользователей с указанной ролью."); IUser dto = AuthenticationService.CreateUser(modelUser,role); AuthenticationService.setAuthTicket(dto); AddRecordToUserLogin(modelUser,role); }