Upgraded server networks and api keys

This commit is contained in:
Marcel Baumgartner
2024-06-25 23:19:27 +02:00
parent 2838a91e3c
commit 95a5eafec2
3 changed files with 79 additions and 44 deletions

View File

@@ -1,10 +1,8 @@
@page "/admin/api/keys" @page "/admin/api/keys"
@using MoonCore.Abstractions @using MoonCore.Blazor.Models.Fast
@using MoonCore.Blazor.Models.Forms
@using MoonCore.Helpers @using MoonCore.Helpers
@using Moonlight.Core.Database.Entities @using Moonlight.Core.Database.Entities
@using Moonlight.Core.Models.Forms.ApiKeys
@using Moonlight.Core.UI.Components.Navigations @using Moonlight.Core.UI.Components.Navigations
@inject ClipboardService ClipboardService @inject ClipboardService ClipboardService
@@ -15,17 +13,15 @@
<AdminApiNavigation Index="1"/> <AdminApiNavigation Index="1"/>
<div class="mt-5"> <div class="mt-5">
<AutoCrud TItem="ApiKey" <FastCrud TItem="ApiKey"
TCreateForm="CreateApiKeyForm" OnConfigure="OnConfigure"
TUpdateForm="UpdateApiKeyForm" OnConfigureCreate="OnConfigureCreate"
Loader="ApiKeysLoader" OnConfigureEdit="OnConfigureEdit">
OnConfigure="OnConfigure">
<View> <View>
<MCBColumn TItem="ApiKey" Field="@(x => x.Key)" Title="Key"> <MCBColumn TItem="ApiKey" Field="@(x => x.Key)" Title="Key">
<Template> <Template>
@{ @{
var apiKeyHalf = Formatter.CutInHalf(context!.Key); var apiKeyHalf = Formatter.CutInHalf(context!.Key);
var bogusHalf = Formatter.IntToStringWithLeadingZeros(69, apiKeyHalf.Length);
} }
<div> <div>
@@ -49,20 +45,19 @@
</MCBColumn> </MCBColumn>
<MCBColumn TItem="ApiKey" Field="@(x => x.PermissionJson)" Title="Permissions"/> <MCBColumn TItem="ApiKey" Field="@(x => x.PermissionJson)" Title="Permissions"/>
</View> </View>
</AutoCrud> </FastCrud>
</div> </div>
@code @code
{ {
private IEnumerable<ApiKey> ApiKeysLoader(Repository<ApiKey> repository) private void OnConfigure(FastCrudConfiguration<ApiKey> configuration)
{ {
return repository.Get(); configuration.ValidateCreate = async apiKey =>
}
private void OnConfigure(AutoCrudConfiguration<ApiKey> configuration)
{
configuration.ValidateAdd = async apiKey =>
{ {
// TODO: Remove this when correct permission editor exists
if (string.IsNullOrEmpty(apiKey.PermissionJson))
apiKey.PermissionJson = "[]";
var key = Formatter.GenerateString(32); var key = Formatter.GenerateString(32);
apiKey.Key = key; apiKey.Key = key;
@@ -70,4 +65,22 @@
await ToastService.Info("Copied api key into your clipboard"); await ToastService.Info("Copied api key into your clipboard");
}; };
} }
private void OnConfigureCreate(FastConfiguration<ApiKey> configuration)
{
configuration.AddProperty(x => x.Description)
.WithDefaultComponent()
.WithDescription("Write a note here for which application the api key is used for")
.WithValidation(FastValidators.Required);
configuration.AddProperty(x => x.ExpiresAt)
.WithDefaultComponent()
.WithDescription("Specify when the api key should expire");
configuration.AddProperty(x => x.PermissionJson)
.WithDefaultComponent()
.WithName("Permissions");
}
private void OnConfigureEdit(FastConfiguration<ApiKey> configuration, ApiKey _) => OnConfigureCreate(configuration);
} }

View File

@@ -1,11 +1,14 @@
@page "/servers/networks" @page "/servers/networks"
@using System.ComponentModel.DataAnnotations
@using Moonlight.Features.Servers.UI.Components @using Moonlight.Features.Servers.UI.Components
@using Moonlight.Features.Servers.Entities @using Moonlight.Features.Servers.Entities
@using Moonlight.Features.Servers.Models.Forms.Users.Networks @using Moonlight.Features.Servers.Models.Forms.Users.Networks
@using MoonCore.Abstractions @using MoonCore.Abstractions
@using Moonlight.Core.Services @using Moonlight.Core.Services
@using Microsoft.EntityFrameworkCore @using Microsoft.EntityFrameworkCore
@using MoonCore.Blazor.Forms.Fast.Components
@using MoonCore.Blazor.Models.Fast
@using MoonCore.Exceptions @using MoonCore.Exceptions
@inject IdentityService IdentityService @inject IdentityService IdentityService
@@ -13,11 +16,11 @@
<ServersNavigation Index="1"/> <ServersNavigation Index="1"/>
<AutoCrud TItem="ServerNetwork" <FastCrud TItem="ServerNetwork"
TCreateForm="CreateNetworkForm" Loader="Loader"
TUpdateForm="UpdateNetworkForm" OnConfigure="OnConfigure"
Loader="Load" OnConfigureCreate="OnConfigureCreate"
ValidateAdd="ValidateAdd"> OnConfigureEdit="OnConfigureEdit">
<View> <View>
<MCBColumn TItem="ServerNetwork" Field="@(x => x.Name)" Title="Name"/> <MCBColumn TItem="ServerNetwork" Field="@(x => x.Name)" Title="Name"/>
<MCBColumn TItem="ServerNetwork" Title="Node"> <MCBColumn TItem="ServerNetwork" Title="Node">
@@ -28,30 +31,25 @@
<MCBColumn TItem="ServerNetwork" Title="Used by"> <MCBColumn TItem="ServerNetwork" Title="Used by">
<Template> <Template>
@{ @{
var servers = UsedByCache.ContainsKey(context.Id) ? UsedByCache[context.Id] : Array.Empty<Server>(); var servers = UsedByCache.ContainsKey(context.Id) ? UsedByCache[context.Id] : Array.Empty<Server>();
} }
<span> <span>
@foreach (var server in servers) @foreach (var server in servers)
{ {
<span><a href="/server/@(server.Id)">@(server.Name)</a> @(server != servers.Last() ? "," : "")</span> <span><a href="/server/@(server.Id)">@(server.Name)</a> @(server != servers.Last() ? "," : "")</span>
} }
</span> </span>
</Template> </Template>
</MCBColumn> </MCBColumn>
</View>@* </View>
<NoItemsView> </FastCrud>
<IconAlert Icon="bx-search-alt" Color="primary" Title="No private network found">
Create a new private network in order to connect multiple servers on the same node
</IconAlert>
</NoItemsView>*@
</AutoCrud>
@code @code
{ {
private readonly Dictionary<int, Server[]> UsedByCache = new(); private readonly Dictionary<int, Server[]> UsedByCache = new();
private IEnumerable<ServerNetwork> Load(Repository<ServerNetwork> repository) private IEnumerable<ServerNetwork> Loader(Repository<ServerNetwork> repository)
{ {
var result = repository var result = repository
.Get() .Get()
@@ -74,20 +72,44 @@
return result; return result;
} }
private Task ValidateAdd(ServerNetwork network) private void OnConfigure(FastCrudConfiguration<ServerNetwork> configuration)
{ {
if (!ServerRepository configuration.ValidateCreate += network =>
.Get()
.Any(x => x.Node.Id == network.Node.Id && x.Owner.Id == IdentityService.CurrentUser.Id))
{ {
throw new DisplayException("You need a server on the selected node in order to create a network on the node"); if (!ServerRepository
} .Get()
.Any(x => x.Node.Id == network.Node.Id && x.Owner.Id == IdentityService.CurrentUser.Id))
{
throw new DisplayException("You need a server on the selected node in order to create a network on the node");
}
//TODO: Add config to check the amount of networks created //TODO: Add config to check the amount of networks created
// Set user as the crud is not allowed to set it (user crud and so on) // Set user as the crud is not allowed to set it (user crud and so on)
network.User = IdentityService.CurrentUser; network.User = IdentityService.CurrentUser;
return Task.CompletedTask; return Task.CompletedTask;
};
}
private void OnConfigureCreate(FastConfiguration<ServerNetwork> configuration)
{
configuration.AddProperty(x => x.Name)
.WithDefaultComponent()
.WithValidation(FastValidators.Required);
Func<ServerNode, string> nodeDisplayField = x => x.Name;
configuration.AddProperty(x => x.Node)
.WithComponent<ServerNode, SelectComponent<ServerNode>>()
.WithAdditionalOption("DisplayField", nodeDisplayField)
.WithValidation<ServerNode>(x => x != null ? ValidationResult.Success : new ValidationResult("You need to specify a node"));
}
private void OnConfigureEdit(FastConfiguration<ServerNetwork> configuration, ServerNetwork _)
{
configuration.AddProperty(x => x.Name)
.WithDefaultComponent()
.WithValidation(FastValidators.Required);
} }
} }

View File

@@ -93,8 +93,8 @@
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="MoonCore" Version="1.4.0" /> <PackageReference Include="MoonCore" Version="1.4.1" />
<PackageReference Include="MoonCore.Blazor" Version="1.0.8" /> <PackageReference Include="MoonCore.Blazor" Version="1.0.9" />
<PackageReference Include="Otp.NET" Version="1.3.0" /> <PackageReference Include="Otp.NET" Version="1.3.0" />
<PackageReference Include="QRCoder" Version="1.4.3" /> <PackageReference Include="QRCoder" Version="1.4.3" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.6.2" /> <PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.6.2" />