public async Task <IActionResult> DeleteUser(string id) { var user = await Usermanager.FindByIdAsync(id); if (user == null) { ViewBag.ErrorMessage = $"User with Id = {id} cannot be found"; return(View("NotFound")); } else { if (await Usermanager.IsInRoleAsync(user, "Admin") || await Usermanager.IsInRoleAsync(user, "Employee")) { var claims = await Usermanager.GetClaimsAsync(user); var claimResult = await Usermanager.RemoveClaimsAsync(user, claims); if (!claimResult.Succeeded) { ModelState.AddModelError("", "Cannot remove user existing claims"); return(View("ListUsers")); } var roles = await Usermanager.GetRolesAsync(user); var RolesResult = await Usermanager.RemoveFromRolesAsync(user, roles); if (!RolesResult.Succeeded) { ModelState.AddModelError("", "Cannot remove user existing roles"); return(View("ListUsers")); } user.isactive = false; var result1 = await Usermanager.UpdateAsync(user); if (result1.Succeeded) { return(RedirectToAction("ListUsers")); } foreach (var error in result1.Errors) { ModelState.AddModelError("", error.Description); } return(View("ListUsers")); } var result = await Usermanager.DeleteAsync(user); if (result.Succeeded) { return(RedirectToAction("ListUsers")); } foreach (var error in result.Errors) { ModelState.AddModelError("", error.Description); } return(View("ListUsers")); } }
public async Task <IActionResult> AddEmployee(RegisterEmployeeViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { FullName = model.FullName, UserName = model.Email, Email = model.Email, City = model.cityId, PhoneNumber = model.PhoneNumber, StreetAdress = model.StreetAdress, store_id = model.store_id, isactive = true, Photopath = util.ProcessPhotoproperty(model.Photo) }; var LoginUser = await Usermanager.GetUserAsync(User); user.addedBy = LoginUser.Id; var result = await Usermanager.CreateAsync(user, model.Password); if (result.Succeeded) { foreach (var role in model.Roles.Where(x => x.isSelected)) { if (User.IsInRole("Admin") && role.RoleName == "Admin" && role.RoleName == "Super Admin") { await Usermanager.DeleteAsync(user); return(Forbid()); } } var roles = await Usermanager.AddToRolesAsync(user, model.Roles.Where(x => x.isSelected).Select(y => y.RoleName)); if (!roles.Succeeded) { ModelState.AddModelError("", "Cannot add selected roles to user! Try Again"); await Usermanager.DeleteAsync(user); return(View(model)); } // Add all the claims that are selected on the UI var claims = await Usermanager.AddClaimsAsync(user, model.Claims.Select(c => new Claim(c.ClaimType, c.isSelected ? "true" : "false"))); if (!claims.Succeeded) { ModelState.AddModelError("", "Cannot add selected claims to user! Edit User and Insert Claims from there"); return(View(model)); } var token = await Usermanager.GenerateEmailConfirmationTokenAsync(user); var confirmationLink = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, token = token }, Request.Scheme); string str = await ViewToStringRenderer.RenderViewToStringAsync(HttpContext.RequestServices, $"~/Views/Template/Email_Confirmation.cshtml", confirmationLink); await _emailSender.SendEmailAsync(user.Email, "Email Confirmation", str); //util.sendemail(user.Email, "Email Confirmation", $"<h2>Here is the Confirmation Link</h2></br> <a href={confirmationLink}>{confirmationLink}</a>"); if (Signinmanager.IsSignedIn(User) && (User.IsInRole("Admin") || User.IsInRole("Super Admin"))) { return(RedirectToAction("ListUsers", "Administration")); } ViewBag.PageTitle = "Email Confirmation"; ViewBag.Title = "Registration successful"; ViewBag.Message = "Before you can Login, please confirm your " + "email, by clicking on the confirmation link we have emailed you"; return(View("EmailConfirmation")); // await Signinmanager.SignInAsync(user, isPersistent: false); //return RedirectToAction("Index", "Home"); } foreach (var error in result.Errors) { ModelState.AddModelError("", error.Description); } ViewBag.Stores = util.GetAllStores(); ViewBag.cities = util.getCities(); return(View(model)); } ViewBag.cities = util.getCities(); ViewBag.Stores = util.GetAllStores(); return(View(model)); }