Merge pull request #1416 from wabbajack-tools/authored-files-admin

Start of admin pages for authored files
This commit is contained in:
Timothy Baldridge 2021-04-12 18:08:36 -07:00 committed by GitHub
commit 71e11c3779
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 195 additions and 9 deletions

View File

@ -0,0 +1,27 @@
using System.Net.Http;
using System.Threading.Tasks;
using Wabbajack.BuildServer.Test;
using Wabbajack.Common;
using Xunit;
using Xunit.Abstractions;
namespace Wabbajack.Server.Test
{
public class AuthorControlTests : ABuildServerSystemTest
{
public AuthorControlTests(ITestOutputHelper output, SingletonAdaptor<BuildServerFixture> fixture) : base(output, fixture)
{
}
[Fact]
public async Task LoginRedirects()
{
var client = new HttpClient();
var result =
await client.GetStringAsync($"{Consts.WabbajackBuildServerUri}author_controls/login/{Fixture.APIKey}");
Assert.Contains("Wabbajack Files -", result);
}
}
}

View File

@ -31,7 +31,7 @@ namespace Wabbajack.BuildServer
{
private const string ProblemDetailsContentType = "application/problem+json";
private readonly SqlService _sql;
private const string ApiKeyHeaderName = "X-Api-Key";
public const string ApiKeyHeaderName = "X-Api-Key";
private MetricsKeyCache _keyCache;
@ -71,6 +71,10 @@ namespace Wabbajack.BuildServer
var authorKey = Request.Headers[ApiKeyHeaderName].FirstOrDefault();
if (authorKey == null)
Request.Cookies.TryGetValue(ApiKeyHeaderName, out authorKey);
if (authorKey == null && metricsKey == null)
{
return AuthenticateResult.NoResult();

View File

@ -0,0 +1,90 @@
using System;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using FluentFTP;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Nettle;
using Wabbajack.Common;
using Wabbajack.Server.DataLayer;
namespace Wabbajack.BuildServer.Controllers
{
[Authorize(Roles="Author")]
[Route("/author_controls")]
public class AuthorControls : ControllerBase
{
private ILogger<AuthorControls> _logger;
private SqlService _sql;
public AuthorControls(ILogger<AuthorControls> logger, SqlService sql)
{
_logger = logger;
_sql = sql;
}
[Route("login/{authorKey}")]
[AllowAnonymous]
public async Task<IActionResult> Login(string authorKey)
{
Response.Cookies.Append(ApiKeyAuthenticationHandler.ApiKeyHeaderName, authorKey);
return Redirect($"{Consts.WabbajackBuildServerUri}author_controls/home");
}
private static Func<object, string> _homePageTemplate;
private static Func<object, string> HomePageTemplate
{
get
{
if (_homePageTemplate == null)
{
var resource = Assembly.GetExecutingAssembly()
.GetManifestResourceStream("Wabbajack.Server.Controllers.Templates.AuthorControls.html")!
.ReadAll();
_homePageTemplate = NettleEngine.GetCompiler().Compile(Encoding.UTF8.GetString(resource));
}
return _homePageTemplate;
}
}
[Route("home")]
[Authorize("")]
public async Task<IActionResult> HomePage()
{
var user = User.FindFirstValue(ClaimTypes.Name);
var files = (await _sql.AllAuthoredFiles())
.Where(af => af.Author == user)
.Select(af => new
{
Size = af.Size.FileSizeToString(),
OriginalSize = af.Size,
Name = af.OriginalFileName,
MangledName = af.MungedName,
UploadedDate = af.LastTouched
})
.OrderBy(f => f.Name)
.ThenBy(f => f.UploadedDate)
.ToList();
var result = HomePageTemplate(new
{
User = user,
TotalUsage = files.Select(f => f.OriginalSize).Sum().ToFileSizeString(),
WabbajackFiles = files.Where(f => f.Name.EndsWith(Consts.ModListExtensionString)),
OtherFiles = files.Where(f => !f.Name.EndsWith(Consts.ModListExtensionString))
});
return new ContentResult {
ContentType = "text/html",
StatusCode = (int)HttpStatusCode.OK,
Content = result};
}
}
}

View File

@ -0,0 +1,46 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Author Controls - {{$.User}}</title>
</head>
<body>
<h2>Author Controls - {{$.User}}</h2>
<br/>
<h3>Wabbajack Files - {{$.TotalUsage}}</h3>
<table>
<tr>
<td><b>Name</b></td>
<td><b>Size</b></td>
<td><b>Finished Uploading</b></td>
<td><b>Unique Name</b></td>
</tr>
{{each $.WabbajackFiles }}
<tr>
<td>{{$.Name}}</td>
<td>{{$.Size}}</td>
<td>{{$.UploadedDate}}</td>
<td>{{$.MangledName}}</td>
</tr>
{{/each}}
</table>
<h3>Other Files</h3>
<table>
<tr>
<td><b>Name</b></td>
<td><b>Size</b></td>
<td><b>Finished Uploading</b></td>
<td><b>Unique Name</b></td>
</tr>
{{each $.OtherFiles }}
<tr>
<td>{{$.Name}}</td>
<td>{{$.Size}}</td>
<td>{{$.UploadedDate}}</td>
<td>{{$.MangledName}}</td>
</tr>
{{/each}}
</table>
</body>
</html>

View File

@ -144,6 +144,7 @@ namespace Wabbajack.Server
//app.UseService<MirrorQueueService>();
app.UseService<Watchdog>();
app.UseService<DiscordFrontend>();
// Don't enable Author Files Cleanup
//app.UseService<AuthoredFilesCleanup>();
app.UseService<MetricsKeyCache>();

View File

@ -46,6 +46,8 @@
<EmbeddedResource Include="sheo_quotes.txt" />
<None Remove="Controllers\Templates\TotalListTemplate.html" />
<EmbeddedResource Include="Controllers\Templates\TotalListTemplate.html" />
<None Remove="Controllers\Templates\AuthorControls.html" />
<EmbeddedResource Include="Controllers\Templates\AuthorControls.html" />
</ItemGroup>
<ItemGroup>

View File

@ -1,6 +1,8 @@
using System;
using System.Reactive;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using ReactiveUI;
@ -19,6 +21,7 @@ namespace Wabbajack
public ICommand SelectFile { get; }
public ICommand HyperlinkCommand { get; }
public IReactiveCommand Upload { get; }
public IReactiveCommand ManageFiles { get; }
[Reactive] public double UploadProgress { get; set; }
[Reactive] public string FinalUrl { get; set; }
@ -41,6 +44,12 @@ namespace Wabbajack
SelectFile = Picker.ConstructTypicalPickerCommand(IsUploading.StartWith(false).Select(u => !u));
HyperlinkCommand = ReactiveCommand.Create(() => Clipboard.SetText(FinalUrl));
ManageFiles = ReactiveCommand.Create(async () =>
{
var authorApiKey = await AuthorAPI.GetAPIKey();
Utils.OpenWebsite(new Uri($"{Consts.WabbajackBuildServerUri}author_controls/login/{authorApiKey}"));
});
Upload = ReactiveCommand.Create(async () =>
{
@ -69,5 +78,6 @@ namespace Wabbajack
.CombineLatest(Picker.WhenAnyValue(t => t.TargetPath).Select(f => f != default),
(a, b) => a && b));
}
}
}

View File

@ -26,24 +26,25 @@
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300" />
<ColumnDefinition Width="300" />
<ColumnDefinition Width="200" />
<ColumnDefinition Width="200" />
<ColumnDefinition Width="200" />
</Grid.ColumnDefinitions>
<TextBlock Grid.ColumnSpan="2"
<TextBlock Grid.ColumnSpan="3"
Margin="5,0"
FontFamily="Lucida Sans"
FontSize="20"
FontWeight="Bold"
Text="File Uploader" />
<TextBlock Grid.Row="1" Grid.ColumnSpan="2"
<TextBlock Grid.Row="1" Grid.ColumnSpan="3"
Margin="5"
Text="{Binding AuthorFile.Picker.TargetPath}" />
<ProgressBar Grid.Row="2" Grid.ColumnSpan="2"
<ProgressBar Grid.Row="2" Grid.ColumnSpan="3"
Margin="5"
Maximum="1"
Minimum="0"
Value="{Binding AuthorFile.UploadProgress, Mode=OneWay}" />
<TextBlock Grid.Row="3" Grid.ColumnSpan="2"
<TextBlock Grid.Row="3" Grid.ColumnSpan="3"
Margin="5">
<Hyperlink Command="{Binding AuthorFile.HyperlinkCommand}">
<TextBlock Text="{Binding AuthorFile.FinalUrl}" />
@ -55,10 +56,15 @@
Select
</Button>
<Button Grid.Row="4" Grid.Column="1"
Margin="5"
Command="{Binding AuthorFile.Upload}">
Margin="5"
Command="{Binding AuthorFile.Upload}">
Upload
</Button>
<Button Grid.Row="4" Grid.Column="2"
Margin="5"
Command="{Binding AuthorFile.ManageFiles}">
Manage Files
</Button>
</Grid>
</Border>
</rxui:ReactiveUserControl>