Class RequirePermissionAttribute
- Namespace
- Excalibur.A3.Authorization
- Assembly
- Excalibur.A3.dll
Marks a message type as requiring a specific permission to execute. This attribute works alongside the interface-based authorization system (IRequireAuthorization). When both are present, AND logic applies (both must pass).
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public sealed class RequirePermissionAttribute : Attribute
- Inheritance
-
RequirePermissionAttribute
- Inherited Members
Remarks
Use this attribute for simple permission checks with clean syntax:
[RequirePermission("users.delete")]
public class DeleteUserCommand : IDispatchAction { }
For complex authorization with custom requirements, use IRequireCustomAuthorization instead.
Constructors
RequirePermissionAttribute(string)
Initializes a new instance of the RequirePermissionAttribute class with the required permission.
public RequirePermissionAttribute(string permission)
Parameters
permissionstringThe permission name (e.g., "users.delete").
Exceptions
- ArgumentException
Thrown when
permissionis null or whitespace.
Properties
Permission
Gets the permission name required (e.g., "users.delete", "orders.create").
public string Permission { get; }
Property Value
- string
The permission name that must be granted for authorization to succeed.
ResourceIdProperty
Gets or sets the property name containing the resource ID. When specified, the middleware will extract the value from this property using reflection.
public string? ResourceIdProperty { get; init; }
Property Value
Examples
[RequirePermission("orders.update", ResourceIdProperty = nameof(OrderId))]
public class UpdateOrderCommand : IDispatchAction
{
public Guid OrderId { get; set; }
}
ResourceTypes
Gets or sets the resource types this permission applies to.
public string[]? ResourceTypes { get; init; }
Property Value
When
Gets or sets a condition expression that must evaluate to true for authorization to succeed.
public string? When { get; init; }
Property Value
Examples
[RequirePermission("orders.approve", When = "resource.Amount <= 10000")]
Remarks
Expressions are parsed at startup and cached as ASTs. Evaluation allocation depends on the expression and attribute values.
Supports subject.*, action.*, and resource.* attribute references.
Operators: ==, !=, >, <, >=, <=, contains, startsWith.
Logic: AND, OR, NOT, parentheses.