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);
|
||||
|
||||
response.Secret = ApiKeyService.GenerateJwt(finalApiKey);
|
||||
var response = new CreateApiKeyResponse
|
||||
{
|
||||
Id = finalApiKey.Id,
|
||||
PermissionsJson = finalApiKey.PermissionsJson,
|
||||
Description = finalApiKey.Description,
|
||||
ExpiresAt = finalApiKey.ExpiresAt,
|
||||
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,28 +16,70 @@ 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();
|
||||
@@ -48,16 +92,36 @@ public class UsersController : Controller
|
||||
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();
|
||||
@@ -73,15 +137,35 @@ public class UsersController : Controller
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
@@ -53,18 +53,18 @@
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<DataTable @ref="Table" TItem="ApiKeyDetailResponse">
|
||||
<DataTable @ref="Table" TItem="ApiKeyResponse">
|
||||
<Configuration>
|
||||
<Pagination TItem="ApiKeyDetailResponse" ItemSource="LoadData" />
|
||||
<Pagination TItem="ApiKeyResponse" ItemSource="LoadData" />
|
||||
|
||||
<DataTableColumn TItem="ApiKeyDetailResponse" Field="@(x => x.Id)" Name="Id"/>
|
||||
<DataTableColumn TItem="ApiKeyDetailResponse" Field="@(x => x.Description)" Name="Description"/>
|
||||
<DataTableColumn TItem="ApiKeyDetailResponse" Field="@(x => x.ExpiresAt)" Name="Expires at">
|
||||
<DataTableColumn TItem="ApiKeyResponse" Field="@(x => x.Id)" Name="Id"/>
|
||||
<DataTableColumn TItem="ApiKeyResponse" Field="@(x => x.Description)" Name="Description"/>
|
||||
<DataTableColumn TItem="ApiKeyResponse" Field="@(x => x.ExpiresAt)" Name="Expires at">
|
||||
<ColumnTemplate>
|
||||
@(Formatter.FormatDate(context.ExpiresAt))
|
||||
</ColumnTemplate>
|
||||
</DataTableColumn>
|
||||
<DataTableColumn TItem="ApiKeyDetailResponse">
|
||||
<DataTableColumn TItem="ApiKeyResponse">
|
||||
<ColumnTemplate>
|
||||
<div class="flex justify-end">
|
||||
<a href="/admin/api/@(context.Id)" class="text-primary-500 mr-2 sm:mr-3">
|
||||
@@ -83,19 +83,19 @@
|
||||
|
||||
@code
|
||||
{
|
||||
private DataTable<ApiKeyDetailResponse> Table;
|
||||
private DataTable<ApiKeyResponse> Table;
|
||||
|
||||
private async Task<IPagedData<ApiKeyDetailResponse>> LoadData(PaginationOptions options)
|
||||
=> await ApiClient.GetJson<PagedData<ApiKeyDetailResponse>>($"api/admin/apikeys?page={options.Page}&pageSize={options.PerPage}");
|
||||
private async Task<IPagedData<ApiKeyResponse>> LoadData(PaginationOptions options)
|
||||
=> await ApiClient.GetJson<PagedData<ApiKeyResponse>>($"api/admin/apikeys?page={options.Page}&pageSize={options.PerPage}");
|
||||
|
||||
private async Task Delete(ApiKeyDetailResponse apiKeyDetailResponse)
|
||||
private async Task Delete(ApiKeyResponse apiKeyResponse)
|
||||
{
|
||||
await AlertService.ConfirmDanger(
|
||||
"API Key deletion",
|
||||
$"Do you really want to delete the api key '{apiKeyDetailResponse.Description}'",
|
||||
$"Do you really want to delete the api key '{apiKeyResponse.Description}'",
|
||||
async () =>
|
||||
{
|
||||
await ApiClient.Delete($"api/admin/apikeys/{apiKeyDetailResponse.Id}");
|
||||
await ApiClient.Delete($"api/admin/apikeys/{apiKeyResponse.Id}");
|
||||
await ToastService.Success("Successfully deleted api key");
|
||||
|
||||
await Table.Refresh();
|
||||
|
||||
@@ -55,7 +55,7 @@
|
||||
|
||||
private async Task Load(LazyLoader _)
|
||||
{
|
||||
var detail = await ApiClient.GetJson<ApiKeyDetailResponse>($"api/admin/apikeys/{Id}");
|
||||
var detail = await ApiClient.GetJson<ApiKeyResponse>($"api/admin/apikeys/{Id}");
|
||||
Request = Mapper.Map<UpdateApiKeyRequest>(detail);
|
||||
}
|
||||
|
||||
|
||||
@@ -17,14 +17,14 @@
|
||||
</PageHeader>
|
||||
</div>
|
||||
|
||||
<DataTable @ref="Table" TItem="UserDetailResponse">
|
||||
<DataTable @ref="Table" TItem="UserResponse">
|
||||
<Configuration>
|
||||
<Pagination TItem="UserDetailResponse" ItemSource="LoadData" />
|
||||
<Pagination TItem="UserResponse" ItemSource="LoadData" />
|
||||
|
||||
<DataTableColumn TItem="UserDetailResponse" Field="@(x => x.Id)" Name="Id"/>
|
||||
<DataTableColumn TItem="UserDetailResponse" Field="@(x => x.Username)" Name="Username"/>
|
||||
<DataTableColumn TItem="UserDetailResponse" Field="@(x => x.Email)" Name="Email"/>
|
||||
<DataTableColumn TItem="UserDetailResponse">
|
||||
<DataTableColumn TItem="UserResponse" Field="@(x => x.Id)" Name="Id"/>
|
||||
<DataTableColumn TItem="UserResponse" Field="@(x => x.Username)" Name="Username"/>
|
||||
<DataTableColumn TItem="UserResponse" Field="@(x => x.Email)" Name="Email"/>
|
||||
<DataTableColumn TItem="UserResponse">
|
||||
<ColumnTemplate>
|
||||
<div class="flex justify-end">
|
||||
<a href="/admin/users/@(context.Id)" class="text-primary-500 mr-2 sm:mr-3">
|
||||
@@ -43,19 +43,19 @@
|
||||
|
||||
@code
|
||||
{
|
||||
private DataTable<UserDetailResponse> Table;
|
||||
private DataTable<UserResponse> Table;
|
||||
|
||||
private async Task<IPagedData<UserDetailResponse>> LoadData(PaginationOptions options)
|
||||
=> await ApiClient.GetJson<PagedData<UserDetailResponse>>($"api/admin/users?page={options.Page}&pageSize={options.PerPage}");
|
||||
private async Task<IPagedData<UserResponse>> LoadData(PaginationOptions options)
|
||||
=> await ApiClient.GetJson<PagedData<UserResponse>>($"api/admin/users?page={options.Page}&pageSize={options.PerPage}");
|
||||
|
||||
private async Task Delete(UserDetailResponse detailResponse)
|
||||
private async Task Delete(UserResponse response)
|
||||
{
|
||||
await AlertService.ConfirmDanger(
|
||||
"User deletion",
|
||||
$"Do you really want to delete the user '{detailResponse.Username}'",
|
||||
$"Do you really want to delete the user '{response.Username}'",
|
||||
async () =>
|
||||
{
|
||||
await ApiClient.Delete($"api/admin/users/{detailResponse.Id}");
|
||||
await ApiClient.Delete($"api/admin/users/{response.Id}");
|
||||
await ToastService.Success("Successfully deleted user");
|
||||
|
||||
await Table.Refresh();
|
||||
|
||||
@@ -55,7 +55,7 @@
|
||||
|
||||
private async Task Load(LazyLoader _)
|
||||
{
|
||||
var detail = await ApiClient.GetJson<UserDetailResponse>($"api/admin/users/{Id}");
|
||||
var detail = await ApiClient.GetJson<UserResponse>($"api/admin/users/{Id}");
|
||||
Request = Mapper.Map<UpdateUserRequest>(detail);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace Moonlight.Shared.Http.Responses.Admin.ApiKeys;
|
||||
|
||||
public class ApiKeyDetailResponse
|
||||
public class ApiKeyResponse
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Description { get; set; }
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace Moonlight.Shared.Http.Responses.Admin.Users;
|
||||
|
||||
public class UserDetailResponse
|
||||
public class UserResponse
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Username { get; set; }
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace Moonlight.Shared.Http.Responses.Assets;
|
||||
|
||||
public class FrontendAssetResponse
|
||||
{
|
||||
public string[] JavascriptFiles { get; set; }
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
namespace Moonlight.Shared.Http.Responses.ClientPlugins;
|
||||
|
||||
public class ClientPluginsResponse
|
||||
{
|
||||
public string[] Dlls { get; set; }
|
||||
public string CacheKey { get; set; }
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
namespace Moonlight.Shared.Http.Responses.PluginsStream;
|
||||
|
||||
public class PluginsAssetManifest
|
||||
{
|
||||
public string[] CssFiles { get; set; }
|
||||
public string[] JavascriptFiles { get; set; }
|
||||
}
|
||||
Reference in New Issue
Block a user