public ActionResult ChangePasswordDialog(ChangePasswordModel model) { try { var state = new ModelStateHelper <ChangePasswordModel>(ModelState); if (string.IsNullOrWhiteSpace(model.Password)) { state.AddErrorFor(x => x.Password, "Не указан новый пароль"); } else if (model.Password != model.PasswordConfirm) { state.AddErrorFor(x => x.PasswordConfirm, "Отличается от нового пароля"); } if (ModelState.IsValid) { var userService = new UserService(DbContext); userService.SetNewPassword(CurrentUser.AccountId, model.UserId, model.Password); var endModel = new ChangePasswordEndModel() { UserId = model.UserId, UserName = model.UserName }; return(PartialView("ChangePasswordDialogEnd", endModel)); } } catch (Exception e) { model.ErrorMessage = e.Message; } return(PartialView(model)); }
public ActionResult Add(AddUserModel model) { if (ModelState.IsValid) { if (string.IsNullOrWhiteSpace(model.DisplayName)) { var modelState = new ModelStateHelper <AddUserModel>(ModelState); modelState.AddErrorFor(x => x.DisplayName, "Значение не должно быть пустым"); } } if (!ModelState.IsValid) { return(View(model)); } try { var user = new User() { Login = model.Login, DisplayName = model.DisplayName, Post = model.Post }; var userService = new UserService(DbContext); // обновим роль if (model.RoleId.HasValue) { userService.AddUserRole(user, new UserRole() { RoleId = model.RoleId.Value }); } userService.CreateUser(user, CurrentUser.AccountId); DbContext.SaveChanges(); } catch (UserFriendlyException e) { ModelState.AddModelError("", e.Message); return(View(model)); } return(RedirectToAction("Index")); }
public ActionResult Edit(SubscriptionEditModel model) { var modelState = new ModelStateHelper <SubscriptionEditModel>(ModelState); // Проверка ComponentTypeId if (model.Object == SubscriptionObject.ComponentType && model.ComponentTypeId == null) { modelState.AddErrorFor(x => x.ComponentTypeId, "Выберите тип компонента"); } // Проверка ComponentId if (model.Object == SubscriptionObject.Component && model.ComponentId == null) { modelState.AddErrorFor(x => x.ComponentId, "Выберите компонент"); } // Проверка Channel if (model.Id == null && model.Channel == null) { // канал должен указываться явно только для новых подписок modelState.AddErrorFor(x => x.Channel, "Выберите канал"); } // Проверка UserId if (model.Id == null && model.UserId == null) { // Пользователь должен указываться явно только для новых подписок modelState.AddErrorFor(x => x.UserId, "Выберите пользователя"); } // проверка цвета var color = model.Color.GetSelectedOne(); if (color == null) { modelState.AddErrorFor(x => x.Color, "Укажите цвет"); } if (!ModelState.IsValid) { return(View(model)); } try { // проверка прав var isOtherUser = CurrentUser.Id != model.UserId; if (isOtherUser) { if (CurrentUser.IsAdmin() == false) { throw new UserFriendlyException("Нет прав на создание подписок другим пользователям"); } } var importance = ImportanceHelper.Get(color).Value; var client = GetDispatcherClient(); if (model.Id == null) { // создание новой подписки var createData = new CreateSubscriptionRequestData() { UserId = model.UserId.Value, Channel = model.Channel.Value, DurationMinimumInSeconds = TimeSpanHelper.GetSeconds(model.MinimumDuration), ResendTimeInSeconds = TimeSpanHelper.GetSeconds(model.ResendTime), Importance = importance, IsEnabled = model.IsEnabled, NotifyBetterStatus = model.NotifyBetterStatus, Object = model.Object }; if (model.Object == SubscriptionObject.Component) { createData.ComponentId = model.ComponentId; } if (model.Object == SubscriptionObject.ComponentType) { createData.ComponentTypeId = model.ComponentTypeId; } var response = client.CreateSubscription(CurrentUser.AccountId, createData); response.Check(); model.Id = response.Data.Id; } else { // редактирование существующей подписки var updateData = new UpdateSubscriptionRequestData() { Id = model.Id.Value, NotifyBetterStatus = model.NotifyBetterStatus, IsEnabled = model.IsEnabled, ResendTimeInSeconds = TimeSpanHelper.GetSeconds(model.ResendTime), DurationMinimumInSeconds = TimeSpanHelper.GetSeconds(model.MinimumDuration), Importance = importance }; var response = client.UpdateSubscription(CurrentUser.AccountId, updateData); response.Check(); } if (model.ModalMode) { return(GetSuccessJsonResponse(new { subscriptionId = model.Id })); } return(Redirect(model.ReturnUrl)); } catch (UserFriendlyException exception) { model.Exception = exception; } return(View(model)); }
public ActionResult Edit(EditUserModel model) { var userService = new UserService(DbContext); var user = userService.GetById(CurrentUser.AccountId, model.Id); if (!CurrentUser.CanManageAccount() && user.Id != CurrentUser.Id) { throw new NoAccessToPageException(); } model.Contacts = user.UserContacts.ToList(); if (ModelState.IsValid) { if (string.IsNullOrWhiteSpace(model.DisplayName)) { var modelState = new ModelStateHelper <EditUserModel>(ModelState); modelState.AddErrorFor(x => x.DisplayName, "Значение не должно быть пустым"); } } if (!ModelState.IsValid) { return(View(model)); } try { user.Login = model.Login; user.DisplayName = model.DisplayName; user.Post = model.Post; // обновим роль, если она поменялась if (CurrentUser.CanManageAccount()) { if (user.Roles.First().RoleId != model.RoleId) { foreach (var role in user.Roles.ToArray()) { userService.RemoveUserRole(user, role, CurrentUser.AccountId); } if (model.RoleId.HasValue) { userService.AddUserRole(user, new UserRole() { RoleId = model.RoleId.Value }); } } } userService.UpdateUserLogin(user); var userSettingService = CurrentAccountDbContext.GetUserSettingService(); userSettingService.SendMeNews(user.Id, model.SendMeNews); DbContext.SaveChanges(); } catch (UserFriendlyException e) { ModelState.AddModelError("", e.Message); return(View(model)); } return(RedirectToAction("Show", new { id = user.Id })); }