public async Task <JsonResult> OnPostDeleteAsync(string id) { var existingRole = await _roleManager.FindByIdAsync(id); if (existingRole.Name != "SuperAdmin" && existingRole.Name != "Basic") { //TODO Check if Any Users already uses this Role bool roleIsNotUsed = true; var allUsers = await _userManager.Users.ToListAsync(); foreach (var user in allUsers) { if (await _userManager.IsInRoleAsync(user, existingRole.Name)) { roleIsNotUsed = false; } } if (roleIsNotUsed) { await _roleManager.DeleteAsync(existingRole); } else { Notify.AddErrorToastMessage("Role is being Used by another User. Cannot Delete"); } } var roles = await _roleManager.Roles.ToListAsync(); Roles = Mapper.Map <IEnumerable <RolesViewModel> >(roles); var html = await Renderer.RenderPartialToStringAsync("_ViewAllRoles", Roles); return(new JsonResult(new { isValid = true, html = html })); }
public async Task <JsonResult> OnPostCreateAsync(UserViewModel userModel) { if (ModelState.IsValid) { MailAddress address = new MailAddress(userModel.Email); string userName = address.User; var user = new ApplicationUser { UserName = userName, Email = userModel.Email, FirstName = userModel.FirstName, LastName = userModel.LastName, EmailConfirmed = true, }; var result = await _userManager.CreateAsync(user, userModel.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); await _userManager.AddToRoleAsync(user, Roles.Basic.ToString()); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var currentUser = await _userManager.GetUserAsync(HttpContext.User); var allUsersExceptCurrentUser = await _userManager.Users.Where(a => a.Id != currentUser.Id).ToListAsync(); Users = Mapper.Map <IEnumerable <UserViewModel> >(allUsersExceptCurrentUser); var htmlData = await Renderer.RenderPartialToStringAsync("_ViewAllUsers", Users); return(new JsonResult(new { isValid = true, html = htmlData })); } foreach (var error in result.Errors) { Notify.AddErrorToastMessage(error.Description); } var html = await Renderer.RenderPartialToStringAsync <UserViewModel>("_CreateUser", userModel); return(new JsonResult(new { isValid = false, html = html })); } return(default);
public async Task <IActionResult> OnPostAsync(string returnUrl = null) { returnUrl = returnUrl ?? Url.Content("~/"); if (ModelState.IsValid) { // This doesn't count login failures towards account lockout // To enable password failures to trigger account lockout, set lockoutOnFailure: true var userName = Input.Email; if (IsValidEmail(Input.Email)) { var userCheck = await _userManager.FindByEmailAsync(Input.Email); if (userCheck != null) { userName = userCheck.UserName; } } var user = await _userManager.FindByNameAsync(userName); if (user != null) { if (!user.IsActive) { return(RedirectToPage("./Deactivated")); } else if (!user.EmailConfirmed) { Notify.AddErrorToastMessage("Email Not Confirmed."); ModelState.AddModelError(string.Empty, "Email Not Confirmed."); return(Page()); } else { var result = await _signInManager.PasswordSignInAsync(userName, Input.Password, Input.RememberMe, lockoutOnFailure : false); if (result.Succeeded) { _logger.LogInformation("User logged in."); Notify.AddSuccessToastMessage($"Logged in as {userName}."); return(LocalRedirect(returnUrl)); } if (result.RequiresTwoFactor) { return(RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe })); } if (result.IsLockedOut) { Notify.AddWarningToastMessage("User account locked out."); _logger.LogWarning("User account locked out."); return(RedirectToPage("./Lockout")); } else { Notify.AddErrorToastMessage("Invalid login attempt."); ModelState.AddModelError(string.Empty, "Invalid login attempt."); return(Page()); } } } else { Notify.AddErrorToastMessage("Email / Username Not Found."); ModelState.AddModelError(string.Empty, "Email / Username Not Found."); } } // If we got this far, something failed, redisplay form return(Page()); }
public async Task <JsonResult> OnPostCreateOrEditAsync(int id, ProductCategoryViewModel product) { if (ModelState.IsValid) { try { if (id == 0) { User.HasRequiredClaims(new List <string> { MasterPermissions.Create, ProductCategoryPermissions.Create }); var createProductCommand = Mapper.Map <CreateProductCategoryCommand>(product); var result = await Mediator.Send(createProductCommand); if (result.Succeeded) { Notify.AddSuccessToastMessage($"Product Created."); } } else { User.HasRequiredClaims(new List <string> { MasterPermissions.Update, ProductCategoryPermissions.Update }); var updateProductCommand = Mapper.Map <UpdateProductCategoryCommand>(product); try { var result = await Mediator.Send(updateProductCommand); if (result.Succeeded) { Notify.AddSuccessToastMessage($"Product Updated."); } } catch (Exception ex) { Logger.LogInformation(ex.Message); throw; } } var response = await Mediator.Send(new GetAllProductCategoriesQuery()); if (response.Succeeded) { var data = response.Data; ProductCategories = Mapper.Map <IEnumerable <ProductCategoryViewModel> >(data); } var html = await Renderer.RenderPartialToStringAsync("_ViewAll", ProductCategories); return(new JsonResult(new { isValid = true, html = html })); } catch (Exception ex) { Notify.AddErrorToastMessage(ex.Message); throw; } } else { var html = await Renderer.RenderPartialToStringAsync <ProductCategoryViewModel>("_CreateOrEdit", product); return(new JsonResult(new { isValid = false, html = html })); } }
public async Task <JsonResult> OnPostCreateOrEditAsync(int id, ProductViewModel product) { if (ModelState.IsValid) { try { if (Request.Form.Files.Count > 0) { IFormFile file = Request.Form.Files.FirstOrDefault(); product.Image = "";// file.OptimizeImageSize(700, 700); } if (id == 0) { User.HasRequiredClaims(new List <string> { MasterPermissions.Create, ProductPermissions.Create }); var createProductCommand = Mapper.Map <CreateProductCommand>(product); var result = await Mediator.Send(createProductCommand); if (result.Succeeded) { Notify.AddSuccessToastMessage($"Product Created."); } } else { User.HasRequiredClaims(new List <string> { MasterPermissions.Update, ProductPermissions.Update }); if (product.Image == null) { var oldProduct = await Mediator.Send(new GetProductByIdQuery { Id = id }); product.Image = oldProduct.Data.Image; } var updateProductCommand = Mapper.Map <UpdateProductCommand>(product); try { var result = await Mediator.Send(updateProductCommand); if (result.Succeeded) { Notify.AddSuccessToastMessage($"Product Updated."); } } catch (Exception ex) { Logger.LogInformation(ex.Message); throw; } } var response = await Mediator.Send(new GetAllProductsQuery()); if (response.Succeeded) { var data = response.Data; Products = Mapper.Map <IEnumerable <ProductViewModel> >(data); } var html = await Renderer.RenderPartialToStringAsync("_ViewAll", Products); return(new JsonResult(new { isValid = true, html = html })); } catch (Exception ex) { Notify.AddErrorToastMessage(ex.Message); throw; } } else { var html = await Renderer.RenderPartialToStringAsync <ProductViewModel>("_CreateOrEdit", product); return(new JsonResult(new { isValid = false, html = html })); } }