public async Task <IActionResult> DeleteConfirmed(User model) { var user = await _userManager.FindByIdAsync(model.Id.ToString()); if (user == null) { ModelState.AddModelError(string.Empty, UserNotFound); } else { var result = await _userManager.DeleteAsync(user); if (result.Succeeded) { FileExtensions.DeleteFile($"{_env.WebRootPath}/avatars/{user.Image}"); TempData["notification"] = DeleteSuccess; return(PartialView("_DeleteConfirmation", user)); } else { ModelState.AddErrorsFromResult(result); } } return(PartialView("_DeleteConfirmation")); }
public async Task <IActionResult> ResetPassword(ResetPasswordViewModel viewModel) { if (ModelState.IsValid) { var User = await _userManager.FindByIdAsync(viewModel.userId.ToString()); if (User == null) { return(NotFound()); } await _userManager.RemovePasswordAsync(User); var result = await _userManager.AddPasswordAsync(User, viewModel.NewPassword); if (result.Succeeded) { ViewBag.AlertSuccess = "بازنشانی کلمه عبور با موفقیت انجام شد."; } else { ModelState.AddErrorsFromResult(result); } viewModel.Email = User.Email; } return(View(viewModel)); }
public async Task <IActionResult> CreateOrUpdate(RolesViewModel viewModel) { if (ModelState.IsValid) { IdentityResult result; if (viewModel.Id != null) { var role = await _roleManager.FindByIdAsync(viewModel.Id.ToString()); result = await _roleManager.UpdateAsync(_mapper.Map(viewModel, role)); } else { result = await _roleManager.CreateAsync(_mapper.Map <Role>(viewModel)); } if (result.Succeeded) { TempData["notification"] = OperationSuccess; } else { ModelState.AddErrorsFromResult(result); } } return(PartialView("_RenderRole", viewModel)); }
public async Task <IActionResult> Edit(RoleViewModel vm) { if (ModelState.IsValid) { var role = await _roleManager.FindByIdAsync(vm.Id); if (role == null) { //ارور return(View(nameof(Index))); } else { role.Name = vm.Name; var result = await _roleManager.UpdateAsync(role); if (result.Succeeded) { return(Json(new { success = true })); } ModelState.AddErrorsFromResult(result); } } return(View(nameof(Edit), new { RoleId = vm.Id.ToString() })); }
public async Task <ActionResult> ResetPassword(ResetPasswordViewModel model) { if (!ModelState.IsValid) { return(View(model)); } var user = await _userManager.FindByEmailAsync(model.Email); if (user == null) { ModelState.AddModelError("Email", "چنین کاربری در سیستم موجود نمی باشد."); return(View(model)); } var result = await _userManager.ResetPasswordAsync(user, model.Code, model.Password); if (result.Succeeded) { return(RedirectToAction("Index", "Login")); } ModelState.AddErrorsFromResult(result); return(View(model)); }
public async Task <IActionResult> ChangePassword(ChangePasswordViewModel ViewModel) { var user = await _userManager.GetUserAsync(User); if (user == null) { return(NotFound()); } if (ModelState.IsValid) { var changePassResult = await _userManager.ChangePasswordAsync(user, ViewModel.OldPassword, ViewModel.NewPassword); if (changePassResult.Succeeded) { ViewBag.Alert = "کلمه عبور شما با موفقیت تغییر یافت."; } else { ModelState.AddErrorsFromResult(changePassResult); } } return(View(ViewModel)); }
public async Task <IActionResult> Delete(RoleViewModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (string.IsNullOrWhiteSpace(model?.Id)) { return(BadRequest("model is null.")); } var role = await _roleManager.FindByIdAsync(model.Id); if (role == null) { ModelState.AddModelError("", RoleNotFound); } else { var result = await _roleManager.DeleteAsync(role); if (result.Succeeded) { return(Json(new { success = true })); } ModelState.AddErrorsFromResult(result); } return(PartialView("_Delete", model: model)); }
public async Task <IActionResult> Edit(UserViewModel model) { if (string.IsNullOrEmpty(model.Password) && string.IsNullOrEmpty(model.ConfirmPassword)) { ModelState.Remove("Password"); ModelState.Remove("ConfirmPassword"); } if (ModelState.IsValid) { var user = await _userManager.FindByIdAsync(model.Guid.ToString()); if (user == null) { ModelState.AddModelError(string.Empty, "چنین کاربری در سیستم تعریف نشده است."); } else { user.UserName = model.Username; user.FirstName = model.FirstName; user.LastName = model.LastName; user.Email = model.Email; user.IsActive = model.IsActive; user.LockoutEnabled = model.LockoutEnabled; user.EmailConfirmed = model.EmailConfirmed; user.Roles = null; if (!string.IsNullOrEmpty(model.Password) && !string.IsNullOrEmpty(model.Password)) { user.PasswordHash = _userManager.PasswordHasher.HashPassword(user, model.Password); } var result = await _userManager.UpdateAsync(user); if (result.Succeeded) { var role = await _roleManager.FindByGuidAsync(model.RoleGuid); var userRole = await _userRoles.Where(x => x.UserId == user.Id).ToListAsync(); _uow.RemoveRange(userRole); await _userRoles.AddAsync(new UserRole { Role = role, User = user, RoleId = role.Id, UserId = user.Id }); await _uow.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } else { ModelState.AddErrorsFromResult(result); } } } model.RolesSelectList = _roleManager.GetRolesSelectList(model.RoleGuid); return(View(model)); }
public async Task <IActionResult> AddRole(RoleViewModel model) { if (ModelState.IsValid) { var result = await _roleManager.CreateAsync(new Role(model.Name)).ConfigureAwait(false); if (result.Succeeded) { return(Json(new { success = true })); } ModelState.AddErrorsFromResult(result); } return(PartialView("_Create", model: model)); }
public async Task <IActionResult> Create(RoleViewModel vm) { if (ModelState.IsValid) { var result = await _roleManager.CreateAsync(new DomainModels.Entities.Identity.Role { Name = vm.Name }); if (result.Succeeded) { return(Utility.CloseAndRefresh()); } ModelState.AddErrorsFromResult(result); } return(Utility.CloseAndRefresh()); }
public async Task <IActionResult> Profile(ProfileViewModel viewModel) { if (viewModel.Id == null) { return(NotFound()); } else { var user = await _userManager.FindByIdAsync(viewModel.Id.ToString()); if (user == null) { return(NotFound()); } else { if (viewModel.ImageFile != null) { viewModel.Image = viewModel.ImageFile.FileName; await viewModel.ImageFile.UploadFileAsync($"{_env.WebRootPath}/avatars/{viewModel.Image}"); } else { viewModel.Image = user.Image; } viewModel.BirthDate = viewModel.PersianBirthDate.ConvertShamsiToMiladi(); var result = await _userManager.UpdateAsync(_mapper.Map(viewModel, user)); if (result.Succeeded) { ViewBag.Alert = EditSuccess; } else { ModelState.AddErrorsFromResult(result); } } return(View(viewModel)); } }
public async Task <IActionResult> Delete(RoleViewModel model) { var role = await _roleManager.FindByIdAsync(model.Id).ConfigureAwait(false); if (role == null) { ModelState.AddModelError("", RoleNotFound); } else { var result = await _roleManager.DeleteAsync(role).ConfigureAwait(false); if (result.Succeeded) { return(Json(new { success = true })); } ModelState.AddErrorsFromResult(result); } return(PartialView("_Delete", model: model)); }
public async Task <IActionResult> Edit(DynmicRoleViewModel model) { if (string.IsNullOrEmpty(model.NodeSelected)) { ModelState.AddModelError("JsonJSTree", "حداقل باید یک سطح دسترسی انتخاب کنید."); } if (ModelState.IsValid) { var role = await _roleManager.FindByIdAsync(model.Guid.ToString()); if (role == null) { ModelState.AddModelError(string.Empty, "چنین سطحدسترسی در سیستم تعریف نشده است."); } else { role.ActionArray = model.NodeSelected; role.Name = role.Title = model.Title; role.Description = model.Description; role.Enable = model.Enable; var result = await _roleManager.UpdateAsync(role); if (result.Succeeded) { await _roleManager.AddOrUpdateRoleClaims(model.Guid, GlobalEnum.DynamicRole, model.NodeSelected); return(RedirectToAction(nameof(Index))); } else { ModelState.AddErrorsFromResult(result); } } } model.JsonJSTree = JsonConvert.SerializeObject(_mvcControllerDiscovery.GetAdminActionInTree(model.NodeSelected)); return(View(model)); }
//[HttpPost] public async Task <IActionResult> Delete(int RoleId) { var role = await _roleManager.FindByIdAsync(RoleId.ToString()); if (role == null) { //ارور نات فاند //ModelState.AddModelError(""); return(View("Index")); } else { var result = await _roleManager.DeleteAsync(role); if (result.Succeeded) { return(Json(new { success = true })); } ModelState.AddErrorsFromResult(result); return(View("Index")); } }
public async Task <IActionResult> Add(DynmicRoleViewModel model) { if (ModelState.IsValid) { if (!string.IsNullOrEmpty(model.NodeSelected)) { var guid = Guid.NewGuid(); var role = new Role { Id = guid, Name = model.Title, Title = model.Title, Enable = model.Enable, Description = model.Description, ActionArray = model.NodeSelected, }; var result = await _roleManager.CreateAsync(role); if (result.Succeeded) { await _roleManager.AddOrUpdateRoleClaims(guid, GlobalEnum.DynamicRole, model.NodeSelected); return(RedirectToAction(nameof(Index))); } else { ModelState.AddErrorsFromResult(result); } } else { ModelState.AddModelError("JsonJSTree", "حداقل باید یک سطح دسترسی انتخاب کنید."); } } return(View(model)); }
public async Task <IActionResult> EditRole(RoleViewModel model) { if (ModelState.IsValid) { var role = await _roleManager.FindByIdAsync(model.Id); if (role == null) { ModelState.AddModelError("", RoleNotFound); } else { role.Name = model.Name; var result = await _roleManager.UpdateAsync(role); if (result.Succeeded) { return(Json(new { success = true })); } ModelState.AddErrorsFromResult(result); } } return(PartialView("_Create", model: model)); }
public async Task <IActionResult> DeleteConfirmed(Role model) { var role = await _roleManager.FindByIdAsync(model.Id.ToString()); if (role == null) { ModelState.AddModelError(string.Empty, RoleNotFound); } else { var result = await _roleManager.DeleteAsync(role); if (result.Succeeded) { TempData["notification"] = DeleteSuccess; return(PartialView("_DeleteConfirmation", role)); } else { ModelState.AddErrorsFromResult(result); } } return(PartialView("_DeleteConfirmation")); }
public async Task <IActionResult> Register(RegisterViewModel viewModel) { if (ModelState.IsValid) { var user = new User { UserName = viewModel.UserName, Email = viewModel.Email, RegisterDateTime = DateTime.Now, IsActive = true, FirstName = "", LastName = "" }; IdentityResult result = await _userManager.CreateAsync(user, viewModel.Password); if (result.Succeeded) { var role = await _roleManager.FindByNameAsync("کاربر"); if (role == null) { await _roleManager.CreateAsync(new Role("کاربر")); } result = await _userManager.AddToRoleAsync(user, "کاربر"); if (result.Succeeded) { var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.Action("ConfirmEmail", "Account", values: new { userId = user.Id, code = code }, protocol: Request.Scheme); await _emailSender.SendEmailAsync(viewModel.Email, "تایید حساب کاربری - سایت میزفا", $"<div dir='rtl' style='font-family:tahoma;font-size:14px'>لطفا با کلیک روی لینک رویه رو حساب کاربری خود را فعال کنید. <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>کلیک کنید</a></div>"); TempData["notification"] = $" ایمیل فعال سازی حساب کاربری به {viewModel.Email} ارسال شد. "; } } ModelState.AddErrorsFromResult(result); } return(PartialView("_Register")); }
public async Task <IActionResult> Add(UserViewModel model) { if (ModelState.IsValid) { var user = new User { UserName = model.Username, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, EmailConfirmed = true, IsActive = model.IsActive, }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { user = await _userManager.FindByNameAsync(model.Username); var role = await _roleManager.FindByGuidAsync(model.RoleGuid); await _userRoles.AddAsync(new UserRole { Role = role, User = user, RoleId = role.Id, UserId = user.Id }); await _uow.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } else { ModelState.AddErrorsFromResult(result); } } model.RolesSelectList = _roleManager.GetRolesSelectList(); return(View(model)); }
public async Task <IActionResult> CreateOrUpdate(UsersViewModel viewModel) { ViewBag.Roles = _roleManager.GetAllRoles(); if (viewModel.Id != null) { ModelState.Remove("Password"); ModelState.Remove("ConfirmPassword"); ModelState.Remove("ImageFile"); } if (ModelState.IsValid) { IdentityResult result = null; if (viewModel.ImageFile != null) { viewModel.Image = _userManager.CheckAvatarFileName(viewModel.ImageFile.FileName); } viewModel.Roles = new List <UserRole> { new UserRole { RoleId = (int)viewModel.RoleId } }; viewModel.BirthDate = viewModel.PersianBirthDate.ConvertShamsiToMiladi(); if (viewModel.Id != null) { var user = await _userManager.FindByIdAsync(viewModel.Id.ToString()); user.FirstName = viewModel.FirstName; user.LastName = viewModel.LastName; user.BirthDate = viewModel.BirthDate; user.Email = viewModel.Email; user.UserName = viewModel.UserName; user.Gender = viewModel.Gender.Value; user.PhoneNumber = viewModel.PhoneNumber; user.Roles = viewModel.Roles; user.Bio = viewModel.Bio; var userRoles = await _userManager.GetRolesAsync(user); if (viewModel.ImageFile != null) { await viewModel.ImageFile.UploadFileAsync($"{_env.WebRootPath}/avatars/{viewModel.Image}"); FileExtensions.DeleteFile($"{_env.WebRootPath}/avatars/{user.Image}"); user.Image = viewModel.Image; } result = await _userManager.RemoveFromRolesAsync(user, userRoles); if (result.Succeeded) { result = await _userManager.UpdateAsync(user); } } else { await viewModel.ImageFile.UploadFileAsync($"{_env.WebRootPath}/avatars/{viewModel.Image}"); viewModel.EmailConfirmed = true; result = await _userManager.CreateAsync(_mapper.Map <User>(viewModel), viewModel.Password); } if (result.Succeeded) { TempData["notification"] = OperationSuccess; } else { ModelState.AddErrorsFromResult(result); } } return(PartialView("_RenderUser", viewModel)); }