Removed use of crud helper. Refactored user and api key response. Removed unused request/response models
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MoonCore.Exceptions;
|
||||
using MoonCore.Extended.Abstractions;
|
||||
using MoonCore.Extended.Helpers;
|
||||
using MoonCore.Extended.PermFilter;
|
||||
using MoonCore.Helpers;
|
||||
using MoonCore.Models;
|
||||
using Moonlight.ApiServer.Database.Entities;
|
||||
using Moonlight.ApiServer.Services;
|
||||
@@ -15,26 +16,70 @@ namespace Moonlight.ApiServer.Http.Controllers.Admin.ApiKeys;
|
||||
[Route("api/admin/apikeys")]
|
||||
public class ApiKeysController : Controller
|
||||
{
|
||||
private readonly CrudHelper<ApiKey, ApiKeyDetailResponse> CrudHelper;
|
||||
private readonly DatabaseRepository<ApiKey> ApiKeyRepository;
|
||||
private readonly ApiKeyService ApiKeyService;
|
||||
|
||||
public ApiKeysController(CrudHelper<ApiKey, ApiKeyDetailResponse> crudHelper, DatabaseRepository<ApiKey> apiKeyRepository, ApiKeyService apiKeyService)
|
||||
public ApiKeysController(DatabaseRepository<ApiKey> apiKeyRepository, ApiKeyService apiKeyService)
|
||||
{
|
||||
CrudHelper = crudHelper;
|
||||
ApiKeyRepository = apiKeyRepository;
|
||||
ApiKeyService = apiKeyService;
|
||||
}
|
||||
|
||||
|
||||
[HttpGet]
|
||||
[RequirePermission("admin.apikeys.read")]
|
||||
public async Task<IPagedData<ApiKeyDetailResponse>> Get([FromQuery] int page, [FromQuery] int pageSize = 50)
|
||||
=> await CrudHelper.Get(page, pageSize);
|
||||
public async Task<IPagedData<ApiKeyResponse>> Get(
|
||||
[FromQuery] int page,
|
||||
[FromQuery] [Range(1, 100)] int pageSize = 50
|
||||
)
|
||||
{
|
||||
var count = await ApiKeyRepository.Get().CountAsync();
|
||||
|
||||
var apiKeys = await ApiKeyRepository
|
||||
.Get()
|
||||
.OrderBy(x => x.Id)
|
||||
.Skip(page * pageSize)
|
||||
.Take(pageSize)
|
||||
.ToArrayAsync();
|
||||
|
||||
var mappedApiKey = apiKeys
|
||||
.Select(x => new ApiKeyResponse()
|
||||
{
|
||||
Id = x.Id,
|
||||
PermissionsJson = x.PermissionsJson,
|
||||
Description = x.Description,
|
||||
ExpiresAt = x.ExpiresAt
|
||||
})
|
||||
.ToArray();
|
||||
|
||||
return new PagedData<ApiKeyResponse>()
|
||||
{
|
||||
CurrentPage = page,
|
||||
Items = mappedApiKey,
|
||||
PageSize = pageSize,
|
||||
TotalItems = count,
|
||||
TotalPages = count == 0 ? 0 : (count - 1) / pageSize
|
||||
};
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
[RequirePermission("admin.apikeys.read")]
|
||||
public async Task<ApiKeyDetailResponse> GetSingle(int id)
|
||||
=> await CrudHelper.GetSingle(id);
|
||||
public async Task<ApiKeyResponse> GetSingle(int id)
|
||||
{
|
||||
var apiKey = await ApiKeyRepository
|
||||
.Get()
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
if (apiKey == null)
|
||||
throw new HttpApiException("No api key with that id found", 404);
|
||||
|
||||
return new ApiKeyResponse()
|
||||
{
|
||||
Id = apiKey.Id,
|
||||
PermissionsJson = apiKey.PermissionsJson,
|
||||
Description = apiKey.Description,
|
||||
ExpiresAt = apiKey.ExpiresAt
|
||||
};
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[RequirePermission("admin.apikeys.create")]
|
||||
@@ -49,20 +94,55 @@ public class ApiKeysController : Controller
|
||||
|
||||
var finalApiKey = await ApiKeyRepository.Add(apiKey);
|
||||
|
||||
var response = Mapper.Map<CreateApiKeyResponse>(finalApiKey);
|
||||
var response = new CreateApiKeyResponse
|
||||
{
|
||||
Id = finalApiKey.Id,
|
||||
PermissionsJson = finalApiKey.PermissionsJson,
|
||||
Description = finalApiKey.Description,
|
||||
ExpiresAt = finalApiKey.ExpiresAt,
|
||||
Secret = ApiKeyService.GenerateJwt(finalApiKey)
|
||||
};
|
||||
|
||||
response.Secret = ApiKeyService.GenerateJwt(finalApiKey);
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
[HttpPatch("{id}")]
|
||||
[RequirePermission("admin.apikeys.update")]
|
||||
public async Task<ApiKeyDetailResponse> Update([FromRoute] int id, [FromBody] UpdateApiKeyRequest request)
|
||||
=> await CrudHelper.Update(id, request);
|
||||
public async Task<ApiKeyResponse> Update([FromRoute] int id, [FromBody] UpdateApiKeyRequest request)
|
||||
{
|
||||
var apiKey = await ApiKeyRepository
|
||||
.Get()
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
if (apiKey == null)
|
||||
throw new HttpApiException("No api key with that id found", 404);
|
||||
|
||||
apiKey.Description = request.Description;
|
||||
apiKey.PermissionsJson = request.PermissionsJson;
|
||||
apiKey.ExpiresAt = request.ExpiresAt;
|
||||
|
||||
await ApiKeyRepository.Update(apiKey);
|
||||
|
||||
return new ApiKeyResponse()
|
||||
{
|
||||
Id = apiKey.Id,
|
||||
Description = apiKey.Description,
|
||||
PermissionsJson = apiKey.PermissionsJson,
|
||||
ExpiresAt = apiKey.ExpiresAt
|
||||
};
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[RequirePermission("admin.apikeys.delete")]
|
||||
public async Task Delete([FromRoute] int id)
|
||||
=> await CrudHelper.Delete(id);
|
||||
{
|
||||
var apiKey = await ApiKeyRepository
|
||||
.Get()
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
if (apiKey == null)
|
||||
throw new HttpApiException("No api key with that id found", 404);
|
||||
|
||||
await ApiKeyRepository.Remove(apiKey);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MoonCore.Exceptions;
|
||||
using MoonCore.Extended.Abstractions;
|
||||
using MoonCore.Extended.Helpers;
|
||||
@@ -14,74 +16,156 @@ namespace Moonlight.ApiServer.Http.Controllers.Admin.Users;
|
||||
[Route("api/admin/users")]
|
||||
public class UsersController : Controller
|
||||
{
|
||||
private readonly CrudHelper<User, UserDetailResponse> CrudHelper;
|
||||
private readonly DatabaseRepository<User> UserRepository;
|
||||
|
||||
public UsersController(CrudHelper<User, UserDetailResponse> crudHelper, DatabaseRepository<User> userRepository)
|
||||
public UsersController(DatabaseRepository<User> userRepository)
|
||||
{
|
||||
CrudHelper = crudHelper;
|
||||
UserRepository = userRepository;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[RequirePermission("admin.users.read")]
|
||||
public async Task<IPagedData<UserDetailResponse>> Get([FromQuery] int page, [FromQuery] int pageSize = 50)
|
||||
=> await CrudHelper.Get(page, pageSize);
|
||||
public async Task<IPagedData<UserResponse>> Get(
|
||||
[FromQuery] int page,
|
||||
[FromQuery] [Range(1, 100)] int pageSize = 50
|
||||
)
|
||||
{
|
||||
var count = await UserRepository.Get().CountAsync();
|
||||
|
||||
var users = await UserRepository
|
||||
.Get()
|
||||
.OrderBy(x => x.Id)
|
||||
.Skip(page * pageSize)
|
||||
.Take(pageSize)
|
||||
.ToArrayAsync();
|
||||
|
||||
var mappedUsers = users
|
||||
.Select(x => new UserResponse()
|
||||
{
|
||||
Id = x.Id,
|
||||
Email = x.Email,
|
||||
Username = x.Username
|
||||
})
|
||||
.ToArray();
|
||||
|
||||
return new PagedData<UserResponse>()
|
||||
{
|
||||
CurrentPage = page,
|
||||
Items = mappedUsers,
|
||||
PageSize = pageSize,
|
||||
TotalItems = count,
|
||||
TotalPages = count == 0 ? 0 : (count - 1) / pageSize
|
||||
};
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
[RequirePermission("admin.users.read")]
|
||||
public async Task<UserDetailResponse> GetSingle(int id)
|
||||
=> await CrudHelper.GetSingle(id);
|
||||
public async Task<UserResponse> GetSingle(int id)
|
||||
{
|
||||
var user = await UserRepository
|
||||
.Get()
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
if (user == null)
|
||||
throw new HttpApiException("No user with that id found", 404);
|
||||
|
||||
return new UserResponse()
|
||||
{
|
||||
Id = user.Id,
|
||||
Email = user.Email,
|
||||
Username = user.Username
|
||||
};
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[RequirePermission("admin.users.create")]
|
||||
public async Task<UserDetailResponse> Create([FromBody] CreateUserRequest request)
|
||||
public async Task<UserResponse> Create([FromBody] CreateUserRequest request)
|
||||
{
|
||||
// Reformat values
|
||||
request.Username = request.Username.ToLower().Trim();
|
||||
request.Email = request.Email.ToLower().Trim();
|
||||
|
||||
|
||||
// Check for users with the same values
|
||||
if (UserRepository.Get().Any(x => x.Username == request.Username))
|
||||
throw new HttpApiException("A user with that username already exists", 400);
|
||||
|
||||
|
||||
if (UserRepository.Get().Any(x => x.Email == request.Email))
|
||||
throw new HttpApiException("A user with that email address already exists", 400);
|
||||
|
||||
request.Password = HashHelper.Hash(request.Password);
|
||||
var hashedPassword = HashHelper.Hash(request.Password);
|
||||
|
||||
return await CrudHelper.Create(request);
|
||||
var user = new User()
|
||||
{
|
||||
Email = request.Email,
|
||||
Username = request.Username,
|
||||
Password = hashedPassword,
|
||||
PermissionsJson = request.PermissionsJson
|
||||
};
|
||||
|
||||
var finalUser = await UserRepository.Add(user);
|
||||
|
||||
return new UserResponse()
|
||||
{
|
||||
Id = finalUser.Id,
|
||||
Email = finalUser.Email,
|
||||
Username = finalUser.Username
|
||||
};
|
||||
}
|
||||
|
||||
[HttpPatch("{id}")]
|
||||
[RequirePermission("admin.users.update")]
|
||||
public async Task<UserDetailResponse> Update([FromRoute] int id, [FromBody] UpdateUserRequest request)
|
||||
public async Task<UserResponse> Update([FromRoute] int id, [FromBody] UpdateUserRequest request)
|
||||
{
|
||||
var user = await CrudHelper.GetSingleModel(id);
|
||||
|
||||
var user = await UserRepository
|
||||
.Get()
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
if (user == null)
|
||||
throw new HttpApiException("No user with that id found", 404);
|
||||
|
||||
// Reformat values
|
||||
request.Username = request.Username.ToLower().Trim();
|
||||
request.Email = request.Email.ToLower().Trim();
|
||||
|
||||
|
||||
// Check for users with the same values
|
||||
if (UserRepository.Get().Any(x => x.Username == request.Username && x.Id != user.Id))
|
||||
throw new HttpApiException("A user with that username already exists", 400);
|
||||
|
||||
|
||||
if (UserRepository.Get().Any(x => x.Email == request.Email && x.Id != user.Id))
|
||||
throw new HttpApiException("A user with that email address already exists", 400);
|
||||
|
||||
|
||||
// Perform hashing the password if required
|
||||
if (!string.IsNullOrEmpty(request.Password))
|
||||
{
|
||||
request.Password = HashHelper.Hash(request.Password);
|
||||
user.Password = HashHelper.Hash(request.Password);
|
||||
user.TokenValidTimestamp = DateTime.UtcNow; // This change will get applied by the crud helper
|
||||
}
|
||||
|
||||
return await CrudHelper.Update(user, request);
|
||||
|
||||
user.Email = request.Email;
|
||||
user.Username = request.Username;
|
||||
// TODO: Add permissions update here
|
||||
|
||||
await UserRepository.Update(user);
|
||||
|
||||
return new UserResponse()
|
||||
{
|
||||
Id = user.Id,
|
||||
Email = user.Email,
|
||||
Username = user.Username
|
||||
};
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[RequirePermission("admin.users.delete")]
|
||||
public async Task Delete([FromRoute] int id)
|
||||
=> await CrudHelper.Delete(id);
|
||||
{
|
||||
var user = await UserRepository
|
||||
.Get()
|
||||
.FirstOrDefaultAsync(x => x.Id == id);
|
||||
|
||||
if (user == null)
|
||||
throw new HttpApiException("No user with that id found", 404);
|
||||
|
||||
await UserRepository.Remove(user);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user