From a923dfadc20d72b70e7792840eae769004fe449e Mon Sep 17 00:00:00 2001 From: hz Date: Tue, 13 May 2025 15:10:28 -0700 Subject: [PATCH] Adding users endpoint to return all users to authorized users --- Controllers/AuthController.cs | 9 +++++++++ Services/UserService.cs | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/Controllers/AuthController.cs b/Controllers/AuthController.cs index dec9618..0dd1911 100644 --- a/Controllers/AuthController.cs +++ b/Controllers/AuthController.cs @@ -108,6 +108,15 @@ public class AuthController : ControllerBase return Ok($"{request.Username}'s role updated to {request.NewRole}"); } + + [HttpGet("users")] + [Authorize(Roles = "SUPER")] + public async Task GetAllUsers() + { + var users = await _userService.GetAllAsync(); + return Ok(users); + } + private string GenerateJwtToken(User user) { diff --git a/Services/UserService.cs b/Services/UserService.cs index 14c63f8..4adf734 100644 --- a/Services/UserService.cs +++ b/Services/UserService.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using System.Threading.Tasks; using GameAuthApi.Models; using Microsoft.Extensions.Configuration; @@ -19,6 +20,9 @@ public class UserService public async Task GetByUsernameAsync(string username) => await _users.Find(u => u.Username == username).FirstOrDefaultAsync(); + public async Task> GetAllAsync() => + await _users.Find(_ => true).ToListAsync(); + public async Task CreateAsync(User user) => await _users.InsertOneAsync(user);