From 035d2e3858dd55580c294031573c3be9e1999449 Mon Sep 17 00:00:00 2001 From: Jake Mannens Date: Tue, 3 Oct 2023 02:20:13 +1100 Subject: AclDialog --- Pages/Component/AclDialog.razor | 76 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 4 deletions(-) (limited to 'Pages/Component/AclDialog.razor') diff --git a/Pages/Component/AclDialog.razor b/Pages/Component/AclDialog.razor index 33d1f03..7c3267d 100644 --- a/Pages/Component/AclDialog.razor +++ b/Pages/Component/AclDialog.razor @@ -1,19 +1,87 @@ -@implements IDialog +@using System.Numerics; +@inject IDbContextFactory dbFactory; +@implements IDialog + @if(obj?.Acl is not null) { + + + + + + + @foreach(var rule in Object.Acl.Rules) { + + + + + + } +
ActionSubjectPermissions
@rule.Action.ToString()@rule.Principal.ToString()@GetActivePermissions(rule)
+ + + + } else { +
This item does not have any permissions set!
+ + + + }
@code { - [Parameter] - public HBObject Object { get; set; } - public bool Visible { get => dialog.Visible; set => dialog.Visible = value; } + private HBObject? obj; + private Dialog dialog; public void Show() => Visible = true; public void Hide() => Visible = false; + + public HBObject? Object { + get => obj; + set { + if(value is null) { + obj = null; + return; + } + + using var db = dbFactory.CreateDbContext(); + + obj = db.Objects + .Include(o => o.Acl) + .First(o => o.ObjectId == value.ObjectId); + + if(obj.Acl is not null) + db.Entry(obj.Acl).Collection(a => a.Rules).Load(); + } + } + + public string GetActivePermissions(AclRule rule) { + var perms = Acl.GetPermissionDescriptions(obj!) + .Where(kv => (rule.Permissions & kv.Value) == kv.Value) + .ToList(); + + // Filter the list of matching permissions to include the + // most relevant encapsulation permissions only. E.g. if + // 'Full access' includes 'Read' and 'Write', then only + // show 'Full access'. + List toRemove = new(); + for(int i = 0; i < perms.Count(); i++) + for(int j = 0; j < perms.Count(); j++) + if(i != j) + if((perms[i].Value & perms[j].Value) == perms[i].Value) + toRemove.Add(i); + for(int i = toRemove.Count() - 1; i >= 0; i--) + perms.RemoveAt(toRemove[i]); + + return string.Join(", ", perms + .OrderByDescending(kv => BitOperations.PopCount(kv.Value)) + .ThenByDescending(kv => kv.Value) + .Select(kv => kv.Key)); + } } \ No newline at end of file -- cgit v1.3