public async Task <IActionResult> AddAdmin([FromForm] GetAllAdminViewModel model) { if (!this.ModelState.IsValid) { return(this.View(model)); } var admin = this.dbContext.Roles .FirstOrDefault(x => x.Name == GlobalConstants.AdministratorRoleName); var user = this.dbContext.Users .FirstOrDefault(x => x.Email == model.Add.Email); if (admin != null && user != null) { await this.dbContext.UserRoles .AddAsync(new IdentityUserRole <string> { UserId = user.Id, RoleId = admin.Id, }); await this.dbContext.SaveChangesAsync(); return(this.View("AdminCreateAdmin")); } else if (user != null) { var isAdmin = this.dbContext.UserRoles .FirstOrDefault(x => x.RoleId == admin.Id && x.UserId == user.Id); if (isAdmin == null) { await this.dbContext.UserRoles .AddAsync(new IdentityUserRole <string> { UserId = user.Id, RoleId = admin.Id, }); await this.dbContext.SaveChangesAsync(); return(this.View("AdminCreateAdmin")); } } return(this.RedirectToAction("AddAdmin")); }
public IActionResult All() { var adminRole = this.dbContext.Roles .Where(x => x.Name == GlobalConstants.AdministratorRoleName) .FirstOrDefault(); var user = this.dbContext.Users .OrderBy(x => x.Name) .Where(x => x.Roles.Any(y => y.RoleId == adminRole.Id) && x.Name != "Admin") .ToList(); var viewModel = new GetAllAdminViewModel() { Users = user, }; return(this.View(viewModel)); }