Skip to content

Commit 07960d7

Browse files
committed
Refactor to replace PrivilegeActions.All and PrivilegeSubjects.All with PrivilegeRule.All
1 parent b9b5379 commit 07960d7

File tree

14 files changed

+47
-69
lines changed

14 files changed

+47
-69
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ Use wildcards to allow all actions on a subject or an action on all subjects:
8888

8989
```csharp
9090
var context = new PrivilegeBuilder()
91-
.Allow("test", PrivilegeSubjects.All) // Allow 'test' action on any subject
92-
.Allow(PrivilegeActions.All, "Post") // Allow any action on 'Post'
93-
.Forbid("publish", "Post") // Forbid overrides allow
91+
.Allow("test", PrivilegeRule.All) // Allow 'test' action on any subject
92+
.Allow(PrivilegeRule.All, "Post") // Allow any action on 'Post'
93+
.Forbid("publish", "Post") // Forbid overrides allow
9494
.Build();
9595

9696
context.Allowed("read", "Post").Should().BeTrue();
@@ -110,7 +110,7 @@ Qualifiers provide field-level or fine-grained permissions:
110110
```csharp
111111
var context = new PrivilegeBuilder()
112112
.Allow("read", "Post", ["title", "id"]) // Only allow reading specific fields
113-
.Allow("read", "User") // Allow reading all User fields
113+
.Allow("read", "User") // Allow reading all User fields
114114
.Build();
115115

116116
// Post permissions with qualifiers
@@ -186,7 +186,7 @@ Rules are evaluated in the order they are defined, with more specific rules taki
186186

187187
1. **Forbid rules** always take precedence over allow rules when both match
188188
2. Rules are matched based on exact string comparison (case-insensitive by default)
189-
3. Wildcard rules (`PrivilegeActions.All`, `PrivilegeSubjects.All`) match any value
189+
3. Wildcard rules (`PrivilegeRule.All`, `PrivilegeRule.All`) match any value
190190
4. Alias expansion happens during rule matching
191191

192192
### String Comparison

samples/Sample.Application/Sample.Application.Client/Services/PrivilegeContextProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public ValueTask<PrivilegeContext> GetContextAsync(ClaimsPrincipal? claimsPrinci
2222
.Alias("SensitiveFields", new[] { "Cost", "InternalNotes" }, PrivilegeMatch.Qualifier)
2323

2424
// Global allowances
25-
.Allow("read", PrivilegeSubjects.All) // read everything
25+
.Allow("read", PrivilegeRule.All) // read everything
2626
.Allow("update", "Product", new[] { "Title", "Summary" }) // update selected product fields
2727
.Allow("create", "Product")
2828
.Allow("read", "Order")

src/Privileged.Components/PrivilegeView.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ public class PrivilegeView : ComponentBase
132132
/// <see cref="StringComparer"/> configured in the privilege context.
133133
/// </para>
134134
/// <para>
135-
/// Actions can be literal values or constants from <see cref="PrivilegeActions"/> if using
136-
/// predefined action types. Wildcard actions like <see cref="PrivilegeActions.All"/> are supported
135+
/// Actions can be literal values or constants from <see cref="PrivilegeRule"/> if using
136+
/// predefined action types. Wildcard actions like <see cref="PrivilegeRule.All"/> are supported
137137
/// if defined in the privilege rules.
138138
/// </para>
139139
/// </remarks>
@@ -161,8 +161,8 @@ public class PrivilegeView : ComponentBase
161161
/// should be specified for meaningful authorization checks.
162162
/// </para>
163163
/// <para>
164-
/// Subjects can be literal values or constants from <see cref="PrivilegeSubjects"/> if using
165-
/// predefined subject types. Wildcard subjects like <see cref="PrivilegeSubjects.All"/> are supported
164+
/// Subjects can be literal values or constants from <see cref="PrivilegeRule"/> if using
165+
/// predefined subject types. Wildcard subjects like <see cref="PrivilegeRule.All"/> are supported
166166
/// if defined in the privilege rules.
167167
/// </para>
168168
/// <para>

src/Privileged/PrivilegeActions.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/Privileged/PrivilegeBuilder.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,16 @@ namespace Privileged;
5050
/// <para>Using wildcard constants for global rules:</para>
5151
/// <code>
5252
/// var context = new PrivilegeBuilder()
53-
/// .Allow("read", PrivilegeSubjects.All) // Allow reading any subject
54-
/// .Allow(PrivilegeActions.All, "Post") // Allow any action on posts
55-
/// .Forbid("delete", PrivilegeSubjects.All) // Forbid deleting anything
53+
/// .Allow("read", PrivilegeRule.All) // Allow reading any subject
54+
/// .Allow(PrivilegeRule.All, "Post") // Allow any action on posts
55+
/// .Forbid("delete", PrivilegeRule.All) // Forbid deleting anything
5656
/// .Build();
5757
/// </code>
5858
/// </example>
5959
/// <seealso cref="PrivilegeContext"/>
6060
/// <seealso cref="PrivilegeBuilderExtensions"/>
61-
/// <seealso cref="PrivilegeSubjects"/>
62-
/// <seealso cref="PrivilegeActions"/>
61+
/// <seealso cref="PrivilegeRule"/>
62+
/// <seealso cref="PrivilegeRule"/>
6363
public class PrivilegeBuilder
6464
{
6565
private readonly List<PrivilegeRule> _rules = [];
@@ -149,11 +149,11 @@ public PrivilegeBuilder Comparer(StringComparer comparer)
149149
/// </summary>
150150
/// <param name="action">
151151
/// The action to allow (e.g., "read", "create", "update").
152-
/// Can be a wildcard using <see cref="PrivilegeActions.All"/> to match any action.
152+
/// Can be a wildcard using <see cref="PrivilegeRule.All"/> to match any action.
153153
/// </param>
154154
/// <param name="subject">
155155
/// The subject to allow (e.g., a resource or entity name like "Post", "User").
156-
/// Can be a wildcard using <see cref="PrivilegeSubjects.All"/> to match any subject.
156+
/// Can be a wildcard using <see cref="PrivilegeRule.All"/> to match any subject.
157157
/// </param>
158158
/// <param name="qualifiers">
159159
/// An optional collection of qualifiers that further scope the rule (e.g., field names, tags, or regions).
@@ -180,8 +180,8 @@ public PrivilegeBuilder Comparer(StringComparer comparer)
180180
/// var builder = new PrivilegeBuilder()
181181
/// .Allow("read", "Post") // Basic rule
182182
/// .Allow("edit", "Post", new[] { "title", "content" }) // Rule with qualifiers
183-
/// .Allow(PrivilegeActions.All, "Comment") // Wildcard action
184-
/// .Allow("manage", PrivilegeSubjects.All); // Wildcard subject
183+
/// .Allow(PrivilegeRule.All, "Comment") // Wildcard action
184+
/// .Allow("manage", PrivilegeRule.All); // Wildcard subject
185185
/// </code>
186186
/// </example>
187187
/// <seealso cref="Forbid(string, string, IEnumerable{string}?)"/>
@@ -209,11 +209,11 @@ public PrivilegeBuilder Allow(string action, string subject, IEnumerable<string>
209209
/// </summary>
210210
/// <param name="action">
211211
/// The action to forbid (e.g., "delete", "update", "publish").
212-
/// Can be a wildcard using <see cref="PrivilegeActions.All"/> to forbid any action.
212+
/// Can be a wildcard using <see cref="PrivilegeRule.All"/> to forbid any action.
213213
/// </param>
214214
/// <param name="subject">
215215
/// The subject to forbid (e.g., a resource or entity name like "Post", "User").
216-
/// Can be a wildcard using <see cref="PrivilegeSubjects.All"/> to forbid on any subject.
216+
/// Can be a wildcard using <see cref="PrivilegeRule.All"/> to forbid on any subject.
217217
/// </param>
218218
/// <param name="qualifiers">
219219
/// An optional collection of qualifiers that further scope the rule (e.g., field names, tags, or regions).
@@ -239,10 +239,10 @@ public PrivilegeBuilder Allow(string action, string subject, IEnumerable<string>
239239
/// <example>
240240
/// <code>
241241
/// var builder = new PrivilegeBuilder()
242-
/// .Allow(PrivilegeActions.All, "Post") // Allow all actions on posts
242+
/// .Allow(PrivilegeRule.All, "Post") // Allow all actions on posts
243243
/// .Forbid("delete", "Post") // Except deletion
244244
/// .Forbid("edit", "Post", ["sensitive_data"]) // Forbid editing sensitive fields
245-
/// .Forbid(PrivilegeActions.All, "AdminSettings"); // Forbid all actions on admin settings
245+
/// .Forbid(PrivilegeRule.All, "AdminSettings"); // Forbid all actions on admin settings
246246
/// </code>
247247
/// </example>
248248
/// <seealso cref="Allow(string, string, IEnumerable{string}?)"/>
@@ -383,7 +383,7 @@ public PrivilegeBuilder Merge(PrivilegeModel model)
383383
/// <code>
384384
/// var builder = new PrivilegeBuilder()
385385
/// .Allow("read", "Post")
386-
/// .Forbid("delete", PrivilegeSubjects.All);
386+
/// .Forbid("delete", PrivilegeRule.All);
387387
///
388388
/// var context = builder.Build();
389389
///

src/Privileged/PrivilegeContext.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Privileged;
1616
/// <list type="bullet">
1717
/// <item><description>Rule-based authorization with allow and forbid permissions.</description></item>
1818
/// <item><description>Alias expansion for actions, subjects, and qualifiers.</description></item>
19-
/// <item><description>Wildcard matching using <see cref="PrivilegeActions.All"/> and <see cref="PrivilegeSubjects.All"/>.</description></item>
19+
/// <item><description>Wildcard matching using <see cref="PrivilegeRule.All"/> and <see cref="PrivilegeRule.All"/>.</description></item>
2020
/// <item><description>Field-level permissions through qualifiers.</description></item>
2121
/// <item><description>Customizable string comparison for rule matching.</description></item>
2222
/// </list>
@@ -221,7 +221,7 @@ private bool SubjectMatcher(PrivilegeRule rule, string subject)
221221
return true;
222222

223223
// wildcard match optimization
224-
if (StringComparer.Equals(rule.Subject, PrivilegeSubjects.All))
224+
if (StringComparer.Equals(rule.Subject, PrivilegeRule.All))
225225
return true;
226226

227227
// Alias match
@@ -235,7 +235,7 @@ private bool ActionMatcher(PrivilegeRule rule, string action)
235235
return true;
236236

237237
// wildcard match optimization
238-
if (StringComparer.Equals(rule.Action, PrivilegeActions.All))
238+
if (StringComparer.Equals(rule.Action, PrivilegeRule.All))
239239
return true;
240240

241241
// Alias match

src/Privileged/PrivilegeRule.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,20 @@ namespace Privileged;
99
[Equatable]
1010
public partial record PrivilegeRule
1111
{
12+
/// <summary>
13+
/// A special keyword indicating that the rule applies to all subjects, actions and qualifiers.
14+
/// When used in a rule, it matches any value.
15+
/// </summary>
16+
public const string All = "*";
17+
1218
/// <summary>
1319
/// Represents a privilege rule that allows all actions and subjects.
1420
/// </summary>
1521
[IgnoreEquality]
1622
public static PrivilegeRule AllowAll { get; } = new()
1723
{
18-
Action = PrivilegeActions.All,
19-
Subject = PrivilegeSubjects.All
24+
Action = All,
25+
Subject = All
2026
};
2127

2228
/// <summary>

src/Privileged/PrivilegeSubjects.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

test/Privileged.Authorization.Tests/PrivilegeRequirementHandlerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ public async Task HandleRequirement_WithWildcardRules_WorksCorrectly()
199199
{
200200
// Arrange
201201
var privilegeContext = new PrivilegeBuilder()
202-
.Allow("read", PrivilegeSubjects.All)
203-
.Allow(PrivilegeActions.All, "Post")
202+
.Allow("read", PrivilegeRule.All)
203+
.Allow(PrivilegeRule.All, "Post")
204204
.Forbid("delete", "Post")
205205
.Build();
206206

test/Privileged.Benchmarks/Optimizations/PrivilegeContextOriginal.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ private bool SubjectMatcher(PrivilegeRule rule, string subject)
7171
{
7272
// can match global all or requested subject
7373
return StringComparer.Equals(rule.Subject, subject)
74-
|| StringComparer.Equals(rule.Subject, PrivilegeSubjects.All)
74+
|| StringComparer.Equals(rule.Subject, PrivilegeRule.All)
7575
|| AliasMatcher(rule.Subject, subject, PrivilegeMatch.Subject);
7676
}
7777

7878
private bool ActionMatcher(PrivilegeRule rule, string action)
7979
{
8080
// can match global manage action or requested action
8181
return StringComparer.Equals(rule.Action, action)
82-
|| StringComparer.Equals(rule.Action, PrivilegeActions.All)
82+
|| StringComparer.Equals(rule.Action, PrivilegeRule.All)
8383
|| AliasMatcher(rule.Action, action, PrivilegeMatch.Action);
8484
}
8585

0 commit comments

Comments
 (0)