From 07728d1048f34e1d048da63684b341ab30bc1d06 Mon Sep 17 00:00:00 2001 From: Jake Mannens Date: Mon, 16 Oct 2023 02:01:27 +1100 Subject: FeedService and AclDialog --- Services/FeedService.cs | 78 +++++++++++++++++++++++++++++++++++++++++++ Services/PrincipalProvider.cs | 7 +++- Services/SecurityService.cs | 10 ++++-- 3 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 Services/FeedService.cs (limited to 'Services') diff --git a/Services/FeedService.cs b/Services/FeedService.cs new file mode 100644 index 0000000..c66b9ee --- /dev/null +++ b/Services/FeedService.cs @@ -0,0 +1,78 @@ +using Microsoft.EntityFrameworkCore; +using System.Data.Common; + +namespace HyperBooru.Services; + +public enum FeedOrder { + Chronological, + Rating +} + +public interface IFeedService { + public IEnumerable Feed { get; } + + public void InitializeFeed( + FeedOrder order = FeedOrder.Chronological, + bool descending = true, + bool randomPosition = false); +} + +public class FeedService : IFeedService { + private FeedConfiguration? feedConfig; + + private IDbContextFactory dbFactory; + + public FeedService(IDbContextFactory dbFactory) => + this.dbFactory = dbFactory; + + public void InitializeFeed( + FeedOrder order, + bool descending, + bool randomPosition) { + + feedConfig = new() { + Order = order, + Descending = descending, + RandomPosition = randomPosition + }; + } + + public IEnumerable Feed { + get { + if(feedConfig is null) + throw new InvalidOperationException("Feed must be initialized first"); + + while(true) { + var db = dbFactory.CreateDbContext(); + + IOrderedQueryable media; + + switch(feedConfig.Order) { + default: + case FeedOrder.Chronological: + if(feedConfig.Descending) + media = db.Media.OrderByDescending(m => m.ObjectId); + else + media = db.Media.OrderBy(m => m.ObjectId); + break; + } + + Media[] mediaArray = media.Take(50).ToArray(); + + db.Dispose(); + + if(mediaArray.Count() == 0) + break; + + foreach(var m in mediaArray) + yield return m; + } + } + } + + private record FeedConfiguration { + public FeedOrder Order { get; set; } + public bool Descending { get; set; } + public bool RandomPosition { get; set; } + } +} diff --git a/Services/PrincipalProvider.cs b/Services/PrincipalProvider.cs index 4b2cf42..7039379 100644 --- a/Services/PrincipalProvider.cs +++ b/Services/PrincipalProvider.cs @@ -5,6 +5,9 @@ public interface IPrincipalProvider { public IUser? GetUser(string name); public IGroup? GetGroup(string name); + public SecurityIdentifier? TranslateName(string name); + public string? TranslateName(SecurityIdentifier sid); + /// /// Perform a search for any principals whose account name /// matches the search term specified by name. @@ -29,13 +32,15 @@ public abstract class PrincipalProvider : IPrincipalProvider { public abstract IUser? GetUser(string name); public abstract IGroup? GetGroup(string name); + public abstract SecurityIdentifier? TranslateName(string name); + public abstract string? TranslateName(SecurityIdentifier sid); + public abstract IPrincipal[]? SearchPrincipals(string name); public IGroup[] GetGroups(IPrincipal principal) => GetGroups(principal.Sid, false); public IGroup[] GetGroups(IPrincipal principal, bool recurse) => GetGroups(principal.Sid, recurse); - public IGroup[] GetGroups(SecurityIdentifier sid) => GetGroups(sid, false); public abstract IGroup[] GetGroups(SecurityIdentifier sid, bool recurse); diff --git a/Services/SecurityService.cs b/Services/SecurityService.cs index 2d23a58..85513ec 100644 --- a/Services/SecurityService.cs +++ b/Services/SecurityService.cs @@ -80,15 +80,21 @@ public class SecurityService : ISecurityService { if(wellKnownSid is not null) return wellKnownSid; - return sid.SddlForm; + return principalProvider.TranslateName(sid) ?? sid.SddlForm; } public SecurityIdentifier? TranslateName(string name) { + name = name.Trim().ToLower(); + + try { + return new SecurityIdentifier(name.ToUpper()); + } catch(ArgumentException) {} + var wellKnownSid = WellKnownSid.TranslateName(name); if(wellKnownSid is not null) return wellKnownSid; - throw new NotImplementedException(); + return principalProvider.TranslateName(name); } public IEnumerable Filter( -- cgit v1.3