AbilityBuilder class

Collects rules in the order they are written, then builds an ability.

final builder = AbilityBuilder()
  ..can('read', 'Article')
  ..can(['update', 'delete'], 'Article', conditions: {'authorId': userId})
  ..cannot('delete', 'Article', conditions: {'published': true})
      .because('a published article cannot be deleted');

final ability = builder.build();

Order is meaning. Rules are checked last-first, so each line overrides the ones above it. Write the broad grant first and narrow it afterwards, the way the example above does — that is the shape that reads correctly.

Constructors

AbilityBuilder()
Starts an empty builder.

Properties

hashCode int
The hash code for this object.
no setterinherited
rules List<RawRule>
The rules collected so far, in the order they were written.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

build<T extends Ability>({T create(List<RawRule> rules)?}) → T
Builds an ability over everything collected.
can(Object action, [Object? subject, Map<String, Object?>? conditions, Object? fields]) RuleRef
Permits action on subject.
cannot(Object action, [Object? subject, Map<String, Object?>? conditions, Object? fields]) RuleRef
Forbids action on subject.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited