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"
@using MoonCore.Abstractions
@using MoonCore.Blazor.Models.Forms
@using MoonCore.Blazor.Models.Fast
@using MoonCore.Helpers
@using Moonlight.Core.Database.Entities
@using Moonlight.Core.Models.Forms.ApiKeys
@using Moonlight.Core.UI.Components.Navigations
@inject ClipboardService ClipboardService
@@ -15,17 +13,15 @@
<AdminApiNavigation Index="1"/>
<div class="mt-5">
<AutoCrud TItem="ApiKey"
TCreateForm="CreateApiKeyForm"
TUpdateForm="UpdateApiKeyForm"
Loader="ApiKeysLoader"
OnConfigure="OnConfigure">
<FastCrud TItem="ApiKey"
OnConfigure="OnConfigure"
OnConfigureCreate="OnConfigureCreate"
OnConfigureEdit="OnConfigureEdit">
<View>
<MCBColumn TItem="ApiKey" Field="@(x => x.Key)" Title="Key">
<Template>
@{
var apiKeyHalf = Formatter.CutInHalf(context!.Key);
var bogusHalf = Formatter.IntToStringWithLeadingZeros(69, apiKeyHalf.Length);
}
<div>
@@ -49,20 +45,19 @@
</MCBColumn>
<MCBColumn TItem="ApiKey" Field="@(x => x.PermissionJson)" Title="Permissions"/>
</View>
</AutoCrud>
</FastCrud>
</div>
@code
{
private IEnumerable<ApiKey> ApiKeysLoader(Repository<ApiKey> repository)
private void OnConfigure(FastCrudConfiguration<ApiKey> configuration)
{
return repository.Get();
}
private void OnConfigure(AutoCrudConfiguration<ApiKey> configuration)
{
configuration.ValidateAdd = async apiKey =>
configuration.ValidateCreate = async apiKey =>
{
// TODO: Remove this when correct permission editor exists
if (string.IsNullOrEmpty(apiKey.PermissionJson))
apiKey.PermissionJson = "[]";
var key = Formatter.GenerateString(32);
apiKey.Key = key;
@@ -70,4 +65,22 @@
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"
@using System.ComponentModel.DataAnnotations
@using Moonlight.Features.Servers.UI.Components
@using Moonlight.Features.Servers.Entities
@using Moonlight.Features.Servers.Models.Forms.Users.Networks
@using MoonCore.Abstractions
@using Moonlight.Core.Services
@using Microsoft.EntityFrameworkCore
@using MoonCore.Blazor.Forms.Fast.Components
@using MoonCore.Blazor.Models.Fast
@using MoonCore.Exceptions
@inject IdentityService IdentityService
@@ -13,11 +16,11 @@
<ServersNavigation Index="1"/>
<AutoCrud TItem="ServerNetwork"
TCreateForm="CreateNetworkForm"
TUpdateForm="UpdateNetworkForm"
Loader="Load"
ValidateAdd="ValidateAdd">
<FastCrud TItem="ServerNetwork"
Loader="Loader"
OnConfigure="OnConfigure"
OnConfigureCreate="OnConfigureCreate"
OnConfigureEdit="OnConfigureEdit">
<View>
<MCBColumn TItem="ServerNetwork" Field="@(x => x.Name)" Title="Name"/>
<MCBColumn TItem="ServerNetwork" Title="Node">
@@ -28,30 +31,25 @@
<MCBColumn TItem="ServerNetwork" Title="Used by">
<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>
@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>
</Template>
</MCBColumn>
</View>@*
<NoItemsView>
<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>
</View>
</FastCrud>
@code
{
private readonly Dictionary<int, Server[]> UsedByCache = new();
private IEnumerable<ServerNetwork> Load(Repository<ServerNetwork> repository)
private IEnumerable<ServerNetwork> Loader(Repository<ServerNetwork> repository)
{
var result = repository
.Get()
@@ -74,20 +72,44 @@
return result;
}
private Task ValidateAdd(ServerNetwork network)
private void OnConfigure(FastCrudConfiguration<ServerNetwork> configuration)
{
if (!ServerRepository
.Get()
.Any(x => x.Node.Id == network.Node.Id && x.Owner.Id == IdentityService.CurrentUser.Id))
configuration.ValidateCreate += network =>
{
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)
network.User = IdentityService.CurrentUser;
// Set user as the crud is not allowed to set it (user crud and so on)
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>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="MoonCore" Version="1.4.0" />
<PackageReference Include="MoonCore.Blazor" Version="1.0.8" />
<PackageReference Include="MoonCore" Version="1.4.1" />
<PackageReference Include="MoonCore.Blazor" Version="1.0.9" />
<PackageReference Include="Otp.NET" Version="1.3.0" />
<PackageReference Include="QRCoder" Version="1.4.3" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.6.2" />