#nullable enable using LBPUnion.ProjectLighthouse.PlayerData.Profiles; using LBPUnion.ProjectLighthouse.PlayerData; using LBPUnion.ProjectLighthouse.Helpers; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; // ReSharper disable RouteTemplates.ActionRoutePrefixCanBeExtractedToControllerRoute namespace LBPUnion.ProjectLighthouse.Servers.API.Controllers; /// /// A collection of endpoints relating to users. /// public class UserEndpoints : ApiEndpointController { private readonly Database database; public UserEndpoints(Database database) { this.database = database; } /// /// Gets a user and their information from the database. /// /// The ID of the user /// The user /// The user, if successful. /// The user could not be found. [HttpGet("user/{id:int}")] [ProducesResponseType(typeof(User), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task GetUser(int id) { User? user = await this.database.Users.FirstOrDefaultAsync(u => u.UserId == id); if (user == null) return this.NotFound(); return this.Ok(user); } /// /// Gets a user and their information from the database. /// /// The ID of the user /// The user's status /// The user's status, if successful. /// The user could not be found. [HttpGet("user/{id:int}/status")] [ProducesResponseType(typeof(UserStatus), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] public IActionResult GetUserStatus(int id) { UserStatus userStatus = new(this.database, id); return this.Ok(userStatus); } [HttpPost("user/inviteToken")] public async Task CreateUserInviteToken() { if (Configuration.ServerConfiguration.Instance.Authentication.PrivateRegistration || Configuration.ServerConfiguration.Instance.Authentication.RegistrationEnabled) { string authHeader = this.Request.Headers["Authorization"]; if (!string.IsNullOrWhiteSpace(authHeader)) { string authToken = authHeader.Substring(authHeader.IndexOf(' ') + 1); APIKey? apiKey = await this.database.APIKeys.FirstOrDefaultAsync(k => k.Key == authToken); if (apiKey == null) return this.StatusCode(403, null); RegistrationToken token = new(); token.Created = DateTime.Now; token.Token = CryptoHelper.GenerateAuthToken(); this.database.RegistrationTokens.Add(token); await this.database.SaveChangesAsync(); return Ok(token.Token); } } return this.NotFound(); } }