public async Task <IActionResult> GetUsers(AdminVerificationVM admin) { try { var users = await _service.GetUsers(admin); if (users.ErrorMessage == null) { return(Ok(users)); } if (users.ErrorMessage.Contains("found")) { return(NotFound(users.ErrorMessage)); } throw new Exception(users.ErrorMessage); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public async Task <AdminUsersDetailsVM> GetUsers(AdminVerificationVM verificationVM) { try { if (string.IsNullOrWhiteSpace(verificationVM.AdminId) || string.IsNullOrWhiteSpace(verificationVM.AdminToken)) { throw new Exception("Something went wrong."); } var admin = await _userManager.FindByIdAsync(verificationVM.AdminId); if (admin == null) { throw new Exception("Cannot verify user."); } var adminResult = await _userManager.VerifyUserTokenAsync(admin, "Default", "authentication-backend", verificationVM.AdminToken); if (!adminResult) { throw new Exception("Could not verify the administrator."); } var users = await _userManager.Users.ToListAsync(); if (users == null || users.Count == 0) { throw new Exception("No users could be found."); } AdminUsersDetailsVM usersVM = new AdminUsersDetailsVM { AdminId = admin.Id, FrontEndToken = VerificationToken(), AdminToken = await UserToken(admin), Users = new List <DetailsVM>() }; foreach (var item in users) { DetailsVM user = new DetailsVM { UserId = item.Id, UserName = item.UserName, FirstName = item.FirstName, LastName = item.LastName, Age = item.Age, Email = item.Email, Roles = await _userManager.GetRolesAsync(item) }; if (user == null) { throw new Exception("One or more users were invalid."); } usersVM.Users.Add(user); } return(usersVM); } catch (Exception ex) { AdminUsersDetailsVM user = new AdminUsersDetailsVM { ErrorMessage = ex.Message }; return(user); } }