1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
@using System.Numerics;
@inject HBContext db
@implements IDialog
<Dialog HeightPixels=500 WidthPixels=900 Title="Edit permissions" @ref=dialog>
<div class="vcontainer">
<div class="hcontainer">
<div>
@if(obj?.Acl is not null) {
<MiniPrincipalSelect Label="Owner"/>
<table class="data-table">
<tr>
<th>Action</th>
<th>Subject</th>
<th colspan="2">Permissions</th>
</tr>
@foreach(var rule in obj.Acl.Rules.OrderByDescending(r => r.Action)) {
<tr>
<td>
<div>
<AclActionSwitch
InitialValue=@(rule.Action == AclRuleAction.Allow)
OnToggle=@((v) => rule.Action = v ? AclRuleAction.Allow : AclRuleAction.Deny)/>
</div>
</td>
<td>
@rule.Principal.ToString()
</td>
<td>@GetActivePermissions(rule)</td>
<td>
<a title="Edit" href="javascript:;" @onclick=@(() => EditRule(rule))>🖉</a>
<a title="Delete" href="javascript:;" @onclick=@(() => RemoveRule(rule))>✖</a>
</td>
</tr>
}
</table>
<br/>
<center><a href="javascript:;" @onclick=AddRule>Add new</a></center>
}
</div>
<div>
@if(ruleToEdit is not null && permissionCheckboxes is not null) {
<MiniPrincipalSelect Label="Subject"/>
var permissions = Acl.GetPermissionDescriptions(obj)
.OrderByDescending(kv => BitOperations.PopCount(kv.Value))
.ThenBy(kv => kv.Value);
foreach(var perm in permissionCheckboxes) {
<label>
<input
type="checkbox"
@bind=perm.Value/>
@perm.Description
</label>
}
} else {
<p><i>Click 'Edit' next to a rule to edit it's permissions</i></p>
}
</div>
</div>
<ButtonContainer>
<button class="secondary" @onclick=Hide>Cancel</button>
@if(obj?.Acl is not null) {
<button data-keyboard-shortcut="a" @onclick=ApplyAcl disabled=@(ApplyDisabled)>
<u>A</u>pply
</button>
}
</ButtonContainer>
</div>
</Dialog>
@code {
public bool Visible {
get => dialog.Visible;
set {
if(value && obj is null)
throw new ArgumentException("Object must not be null!", "Object");
dialog.Visible = value;
if(value)
StateHasChanged();
}
}
private HBObject obj;
private AclRule? ruleToEdit;
private PermissionCheckbox[]? permissionCheckboxes;
private int lastHashCode;
private bool addedAcl;
private string? editOwner;
private string? editSubject;
private Dialog dialog;
public void Show() => Visible = true;
public void Hide() => Visible = false;
public void Show(HBObject obj) {
Object = obj;
Show();
}
public HBObject Object {
get => obj;
set {
editOwner = null;
CancelEditRule();
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();
addedAcl = false;
} else {
obj.Acl = new() {
Rules = new()
};
addedAcl = true;
}
lastHashCode = GetAclHashCode(obj.Acl);
}
}
public bool ApplyDisabled =>
#if DEBUG
false;
#else
GetAclHashCode(obj.Acl!) == lastHashCode ||
obj.Acl!.Rules.Select(r => r.Principal).Contains(WellKnownSid.NullSid);
#endif
private 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<int> 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);
toRemove = toRemove.Order().Distinct().ToList();
for(int i = toRemove.Count() - 1; i >= 0; i--)
perms.RemoveAt(toRemove[i]);
return string.Join(", ", perms
.OrderByDescending(kv => BitOperations.PopCount(kv.Value))
.ThenBy(kv => kv.Value)
.Select(kv => kv.Key));
}
private void ApplyAcl() {
db.SaveChanges();
Hide();
}
private void AddRule() {
var rule = new AclRule() {
Principal = WellKnownSid.NullSid,
Action = AclRuleAction.Allow,
Permissions = 0
};
obj.Acl!.Rules.Add(rule);
EditRule(rule);
}
private void RemoveRule(AclRule rule) {
if(rule == ruleToEdit)
CancelEditRule();
obj.Acl!.Rules.Remove(rule);
}
private void EditRule(AclRule rule) {
ruleToEdit = rule;
editSubject = null;
permissionCheckboxes = Acl.GetPermissionDescriptions(obj)
.OrderByDescending(kv => BitOperations.PopCount(kv.Value))
.ThenBy(kv => kv.Value)
.Select(kv => new PermissionCheckbox(rule, kv))
.ToArray();
}
private void CancelEditRule() {
ruleToEdit = null;
permissionCheckboxes = null;
}
// Special hash function to identify only the elements of
// the ACL that may have been changed by the user via this
// dialog.
private int GetAclHashCode(Acl acl) =>
!acl.Rules.Any() ? 0 : acl.Rules
.Select(r => (
r.Action,
r.Permissions,
r.Principal.GetHashCode()).GetHashCode())
.Aggregate((a, v) => HashCode.Combine(a, v));
private class PermissionCheckbox {
public string Description { get; private init; }
private AclRule rule;
private ulong mask;
public PermissionCheckbox(AclRule rule, KeyValuePair<string, ulong> kv) {
this.rule = rule;
this.mask = kv.Value;
Description = kv.Key;
}
public bool Value {
get => (rule.Permissions & mask) == mask;
set {
if(value)
rule.Permissions |= mask;
else
rule.Permissions &= ~mask;
}
}
}
}
|