Skip to content

Commit 32ebe2d

Browse files
committed
Assume all privileges when PrivilegeContext is missing
1 parent 6c4043d commit 32ebe2d

17 files changed

+191
-152
lines changed

src/Privileged.Components/PrivilegeButton.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -502,12 +502,10 @@ protected override void OnParametersSet()
502502
{
503503
base.OnParametersSet();
504504

505-
if (PrivilegeContext == null)
506-
throw new InvalidOperationException("Component requires a cascading parameter of type PrivilegeContext.");
507-
508505
BusyTemplate ??= builder => builder.AddContent(0, BusyText);
509506

510-
HasPermission = string.IsNullOrWhiteSpace(Action)
507+
HasPermission = PrivilegeContext == null
508+
|| string.IsNullOrWhiteSpace(Action)
511509
|| string.IsNullOrWhiteSpace(Subject)
512510
|| PrivilegeContext.Allowed(Action, Subject, Qualifier);
513511
}

src/Privileged.Components/PrivilegeInputCheckbox.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,18 +184,17 @@ protected override void OnParametersSet()
184184
{
185185
base.OnParametersSet();
186186

187-
if (PrivilegeContext == null)
188-
throw new InvalidOperationException("Component requires a cascading parameter of type PrivilegeContext.");
189-
190187
var subject = Subject ?? PrivilegeFormState?.Subject ?? EditContext?.Model.GetType().Name;
191188
var qualifier = Field ?? NameAttributeValue;
192189
var readAction = ReadAction ?? PrivilegeFormState?.ReadAction ?? "read";
193190
var updateAction = UpdateAction ?? PrivilegeFormState?.UpdateAction ?? "update";
194191

195-
HasReadPermission = string.IsNullOrWhiteSpace(subject)
192+
HasReadPermission = PrivilegeContext == null
193+
|| string.IsNullOrWhiteSpace(subject)
196194
|| PrivilegeContext.Allowed(readAction, subject, qualifier);
197195

198-
HasUpdatePermission = string.IsNullOrWhiteSpace(subject)
196+
HasUpdatePermission = PrivilegeContext == null
197+
|| string.IsNullOrWhiteSpace(subject)
199198
|| PrivilegeContext.Allowed(updateAction, subject, qualifier);
200199

201200
if (HasUpdatePermission)

src/Privileged.Components/PrivilegeInputDate.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,18 +182,17 @@ protected override void OnParametersSet()
182182
{
183183
base.OnParametersSet();
184184

185-
if (PrivilegeContext == null)
186-
throw new InvalidOperationException("Component requires a cascading parameter of type PrivilegeContext.");
187-
188185
var subject = Subject ?? PrivilegeFormState?.Subject ?? EditContext?.Model.GetType().Name;
189186
var qualifier = Field ?? NameAttributeValue;
190187
var readAction = ReadAction ?? PrivilegeFormState?.ReadAction ?? "read";
191188
var updateAction = UpdateAction ?? PrivilegeFormState?.UpdateAction ?? "update";
192189

193-
HasReadPermission = string.IsNullOrWhiteSpace(subject)
190+
HasReadPermission = PrivilegeContext == null
191+
|| string.IsNullOrWhiteSpace(subject)
194192
|| PrivilegeContext.Allowed(readAction, subject, qualifier);
195193

196-
HasUpdatePermission = string.IsNullOrWhiteSpace(subject)
194+
HasUpdatePermission = PrivilegeContext == null
195+
|| string.IsNullOrWhiteSpace(subject)
197196
|| PrivilegeContext.Allowed(updateAction, subject, qualifier);
198197

199198
if (HasUpdatePermission)

src/Privileged.Components/PrivilegeInputNumber.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,18 +182,17 @@ protected override void OnParametersSet()
182182
{
183183
base.OnParametersSet();
184184

185-
if (PrivilegeContext == null)
186-
throw new InvalidOperationException($"Component requires a cascading parameter of type {nameof(PrivilegeContext)}.");
187-
188185
var subject = Subject ?? PrivilegeFormState?.Subject ?? EditContext?.Model.GetType().Name;
189186
var qualifier = Field ?? NameAttributeValue;
190187
var readAction = ReadAction ?? PrivilegeFormState?.ReadAction ?? "read";
191188
var updateAction = UpdateAction ?? PrivilegeFormState?.UpdateAction ?? "update";
192189

193-
HasReadPermission = string.IsNullOrWhiteSpace(subject)
190+
HasReadPermission = PrivilegeContext == null
191+
|| string.IsNullOrWhiteSpace(subject)
194192
|| PrivilegeContext.Allowed(readAction, subject, qualifier);
195193

196-
HasUpdatePermission = string.IsNullOrWhiteSpace(subject)
194+
HasUpdatePermission = PrivilegeContext == null
195+
|| string.IsNullOrWhiteSpace(subject)
197196
|| PrivilegeContext.Allowed(updateAction, subject, qualifier);
198197

199198
if (HasUpdatePermission)

src/Privileged.Components/PrivilegeInputSelect.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,18 +185,17 @@ protected override void OnParametersSet()
185185
{
186186
base.OnParametersSet();
187187

188-
if (PrivilegeContext == null)
189-
throw new InvalidOperationException($"Component requires a cascading parameter of type {nameof(PrivilegeContext)}.");
190-
191188
var subject = Subject ?? PrivilegeFormState?.Subject ?? EditContext?.Model.GetType().Name;
192189
var qualifier = Field ?? NameAttributeValue;
193190
var readAction = ReadAction ?? PrivilegeFormState?.ReadAction ?? "read";
194191
var updateAction = UpdateAction ?? PrivilegeFormState?.UpdateAction ?? "update";
195192

196-
HasReadPermission = string.IsNullOrWhiteSpace(subject)
193+
HasReadPermission = PrivilegeContext == null
194+
|| string.IsNullOrWhiteSpace(subject)
197195
|| PrivilegeContext.Allowed(readAction, subject, qualifier);
198196

199-
HasUpdatePermission = string.IsNullOrWhiteSpace(subject)
197+
HasUpdatePermission = PrivilegeContext == null
198+
|| string.IsNullOrWhiteSpace(subject)
200199
|| PrivilegeContext.Allowed(updateAction, subject, qualifier);
201200

202201
if (HasUpdatePermission)

src/Privileged.Components/PrivilegeInputText.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,18 +180,17 @@ protected override void OnParametersSet()
180180
{
181181
base.OnParametersSet();
182182

183-
if (PrivilegeContext == null)
184-
throw new InvalidOperationException("Component requires a cascading parameter of type PrivilegeContext.");
185-
186183
var subject = Subject ?? PrivilegeFormState?.Subject ?? EditContext?.Model.GetType().Name;
187184
var qualifier = Field ?? NameAttributeValue;
188185
var readAction = ReadAction ?? PrivilegeFormState?.ReadAction ?? "read";
189186
var updateAction = UpdateAction ?? PrivilegeFormState?.UpdateAction ?? "update";
190187

191-
HasReadPermission = string.IsNullOrWhiteSpace(subject)
188+
HasReadPermission = PrivilegeContext == null
189+
|| string.IsNullOrWhiteSpace(subject)
192190
|| PrivilegeContext.Allowed(readAction, subject, qualifier);
193191

194-
HasUpdatePermission = string.IsNullOrWhiteSpace(subject)
192+
HasUpdatePermission = PrivilegeContext == null
193+
|| string.IsNullOrWhiteSpace(subject)
195194
|| PrivilegeContext.Allowed(updateAction, subject, qualifier);
196195

197196
if (HasUpdatePermission)

src/Privileged.Components/PrivilegeInputTextArea.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,19 +181,18 @@ protected override void OnParametersSet()
181181
{
182182
base.OnParametersSet();
183183

184-
if (PrivilegeContext == null)
185-
throw new InvalidOperationException($"Component requires a cascading parameter of type {nameof(PrivilegeContext)}.");
186-
187184
var subject = Subject ?? PrivilegeFormState?.Subject ?? EditContext?.Model.GetType().Name;
188185
var qualifier = Field ?? NameAttributeValue;
189186
var readAction = ReadAction ?? PrivilegeFormState?.ReadAction ?? "read";
190187
var updateAction = UpdateAction ?? PrivilegeFormState?.UpdateAction ?? "update";
191188

192189

193-
HasReadPermission = string.IsNullOrWhiteSpace(subject)
190+
HasReadPermission = PrivilegeContext == null
191+
|| string.IsNullOrWhiteSpace(subject)
194192
|| PrivilegeContext.Allowed(readAction, subject, qualifier);
195193

196-
HasUpdatePermission = string.IsNullOrWhiteSpace(subject)
194+
HasUpdatePermission = PrivilegeContext == null
195+
|| string.IsNullOrWhiteSpace(subject)
197196
|| PrivilegeContext.Allowed(updateAction, subject, qualifier);
198197

199198
if (HasUpdatePermission)

src/Privileged.Components/PrivilegeLink .cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,16 +262,16 @@ protected override void OnParametersSet()
262262
{
263263
base.OnParametersSet();
264264

265-
if (PrivilegeContext == null)
266-
throw new InvalidOperationException("Component requires a cascading parameter of type PrivilegeContext.");
267-
268265
if (Subjects?.Any() == true)
269266
{
270-
HasPermission = PrivilegeContext.Any(Action, Subjects);
267+
HasPermission = PrivilegeContext == null
268+
|| PrivilegeContext.Any(Action, Subjects);
269+
271270
return;
272271
}
273272

274-
HasPermission = string.IsNullOrWhiteSpace(Subject)
273+
HasPermission = PrivilegeContext == null
274+
|| string.IsNullOrWhiteSpace(Subject)
275275
|| PrivilegeContext.Allowed(Action, Subject, Qualifier);
276276
}
277277

src/Privileged.Components/PrivilegeView.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,14 +337,10 @@ protected override void OnParametersSet()
337337
base.OnParametersSet();
338338

339339
if (ChildContent != null && Allowed != null)
340-
{
341340
throw new InvalidOperationException($"Do not specify both '{nameof(Allowed)}' and '{nameof(ChildContent)}'.");
342-
}
343341

344342
if (PrivilegeContext == null)
345-
{
346343
throw new InvalidOperationException("PrivilegedView requires a cascading parameter of type PrivilegeContext.");
347-
}
348344

349345
if (Subjects?.Any() == true)
350346
{

test/Privileged.Components.Tests/PrivilegeButtonTests.cs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -402,18 +402,36 @@ public void Button_Applies_Additional_Attributes()
402402
}
403403

404404
[Fact]
405-
public void Throws_When_No_PrivilegeContext_Provided()
405+
public void NoPrivilegeContext_AssumeAllPrivileges_RendersEnabledButton()
406406
{
407-
var exception = Assert.Throws<InvalidOperationException>(() =>
408-
{
409-
RenderComponent<PrivilegeButton>(ps => ps
410-
.Add(p => p.Action, "create")
411-
.Add(p => p.Subject, "Post")
412-
.Add(p => p.ChildContent, builder => builder.AddContent(0, "Create Post"))
413-
);
414-
});
407+
// When no PrivilegeContext is provided, component should assume all privileges
408+
var cut = RenderComponent<PrivilegeButton>(ps => ps
409+
.Add(p => p.Action, "create")
410+
.Add(p => p.Subject, "Post")
411+
.Add(p => p.ChildContent, builder => builder.AddContent(0, "Create Post"))
412+
);
413+
414+
var button = cut.Find("button");
415+
button.Should().NotBeNull();
416+
button.HasAttribute("disabled").Should().BeFalse();
417+
button.TextContent.Should().Be("Create Post");
418+
}
415419

416-
exception.Message.Should().Contain("PrivilegeContext");
420+
[Fact]
421+
public void NoPrivilegeContext_WithHideForbidden_DoesNotHide()
422+
{
423+
// When no PrivilegeContext is provided, all privileges are assumed, so button should not hide
424+
var cut = RenderComponent<PrivilegeButton>(ps => ps
425+
.Add(p => p.Action, "create")
426+
.Add(p => p.Subject, "Post")
427+
.Add(p => p.HideForbidden, true)
428+
.Add(p => p.ChildContent, builder => builder.AddContent(0, "Create Post"))
429+
);
430+
431+
var button = cut.Find("button");
432+
button.Should().NotBeNull();
433+
button.HasAttribute("disabled").Should().BeFalse();
434+
button.TextContent.Should().Be("Create Post");
417435
}
418436

419437
[Fact]

0 commit comments

Comments
 (0)