Class Grant
- Namespace
- Excalibur.A3.Authorization.Grants
- Assembly
- Excalibur.A3.Core.dll
Represents a user permission or access grant with a specific scope and expiration.
public sealed class Grant : AggregateRoot, IAggregateSnapshotSupport, IAggregateRoot<Grant, string>, IAggregateRoot<string>, IAggregateRoot
- Inheritance
-
Grant
- Implements
- Inherited Members
Remarks
The Grant aggregate uses a composite key in the format "{UserId}:{Scope}" where Scope is "{TenantId}:{GrantType}:{Qualifier}".
This aggregate implements IAggregateRoot<TAggregate, TKey> to support static factory methods for repository integration with event-sourced repositories.
Constructors
Grant(string, string, GrantScope, DateTimeOffset?, string)
Initializes a new instance of the Grant class with a specified GrantScope.
public Grant(string userId, string fullName, GrantScope scope, DateTimeOffset? expiresOn, string grantedBy)
Parameters
userIdstringThe ID of the user to whom the grant is issued.
fullNamestringThe full name of the user.
scopeGrantScopeThe scope of the grant.
expiresOnDateTimeOffset?The optional expiration date of the grant.
grantedBystringThe identifier of the entity or user who issued the grant.
Grant(string, string, string, string, string, DateTimeOffset?, string)
Initializes a new instance of the Grant class.
public Grant(string userId, string fullName, string tenantId, string grantType, string qualifier, DateTimeOffset? expiresOn, string grantedBy)
Parameters
userIdstringThe ID of the user to whom the grant is issued.
fullNamestringThe full name of the user.
tenantIdstringThe tenant ID associated with the grant.
grantTypestringThe type of grant issued.
qualifierstringAdditional qualifier for the grant type.
expiresOnDateTimeOffset?The optional expiration date of the grant.
grantedBystringThe identifier of the entity or user who issued the grant.
Grant(string, string, string, string, string, DateTimeOffset?, string, DateTimeOffset)
Initializes a new instance of the Grant class with additional information about the grant date.
public Grant(string userId, string fullName, string tenantId, string grantType, string qualifier, DateTimeOffset? expiresOn, string grantedBy, DateTimeOffset grantedOn)
Parameters
userIdstringThe ID of the user to whom the grant is issued.
fullNamestringThe full name of the user.
tenantIdstringThe tenant ID associated with the grant.
grantTypestringThe type of grant issued.
qualifierstringAdditional qualifier for the grant type.
expiresOnDateTimeOffset?The optional expiration date of the grant.
grantedBystringThe identifier of the entity or user who issued the grant.
grantedOnDateTimeOffsetThe date and time when the grant was issued.
Properties
ExpiresOn
Gets the optional expiration date of the grant.
public DateTimeOffset? ExpiresOn { get; }
Property Value
- DateTimeOffset?
The expiration date, or null if the grant does not expire.
FullName
Gets the full name of the user to whom the grant was issued.
public string? FullName { get; }
Property Value
GrantedBy
Gets the identifier of the entity or user who issued the grant.
public string? GrantedBy { get; }
Property Value
GrantedOn
Gets the date and time when the grant was issued.
public DateTimeOffset GrantedOn { get; }
Property Value
- DateTimeOffset
The date and time when the grant was issued.
RevokedBy
Gets the identifier of the entity or user who revoked the grant.
public string? RevokedBy { get; }
Property Value
RevokedOn
Gets the date and time when the grant was revoked.
public DateTimeOffset? RevokedOn { get; }
Property Value
- DateTimeOffset?
The date and time when the grant was revoked, or null if not revoked.
Scope
Gets the scope of the grant.
public GrantScope? Scope { get; }
Property Value
- GrantScope
The scope of the grant, or null if not available.
UserId
Gets the ID of the user to whom the grant was issued.
public string? UserId { get; }
Property Value
Methods
ApplyEventInternal(IDomainEvent)
Applies the event's changes to the aggregate's state using pattern matching.
protected override bool ApplyEventInternal(IDomainEvent @event)
Parameters
eventIDomainEventThe domain event to apply.
Returns
- bool
true if the aggregate recognized and applied the event type; otherwise false. Returning false (a missing switch arm) causes the caller to throw UnhandledDomainEventException — an unrecognized event is refused, never silently ignored.
Remarks
Implement this method using a switch expression for optimal performance. The default arm returns false so an unhandled event fails loud:
protected override bool ApplyEventInternal(IDomainEvent @event) => @event switch
{
OrderCreated e => Apply(e),
OrderConfirmed e => Apply(e),
_ => false
};
Each Apply overload returns true.
Create(string)
Creates a new Grant instance with the specified identifier.
public static Grant Create(string id)
Parameters
idstringThe composite key in format "{UserId}:{Scope}".
Returns
- Grant
A new Grant instance.
FromEvents(string, IEnumerable<HistoricEvent>)
Rebuilds a Grant from a stream of historical events.
public static Grant FromEvents(string id, IEnumerable<HistoricEvent> events)
Parameters
idstringThe composite key in format "{UserId}:{Scope}".
eventsIEnumerable<HistoricEvent>The stream of events to apply.
Returns
- Grant
The Grant rebuilt from the events.
IsActive()
Determines whether the grant is currently active.
public bool IsActive()
Returns
- bool
trueif the grant is active; otherwise,false.
IsExpired()
Determines whether the grant has expired.
public bool IsExpired()
Returns
- bool
trueif the grant has expired; otherwise,false.
IsRevoked()
Determines whether the grant has been revoked.
public bool IsRevoked()
Returns
- bool
trueif the grant has been revoked; otherwise,false.
Revoke(string)
Revokes the grant.
public void Revoke(string revokedBy)
Parameters
revokedBystringThe identifier of the entity or user who revoked the grant.