casl_flutter library

Flutter bindings for casl.

Put an ability in the tree, then ask what the user may do wherever you are drawing something:

AbilityProvider(
  ability: ability,
  child: MaterialApp(...),
);

// anywhere below it
Can('delete', article, child: DeleteButton(article: article));
CanBuilder('delete', article, builder: (_, allowed) => ...);
if (context.can('create', 'Article')) ...;

Every question here is answered by casl itself, so a rule means the same thing on the server, in a unit test and on screen. This package adds only the wiring: where the ability lives, and how a widget hears that it changed.

package:casl is re-exported, so one import is enough.

Classes

Ability
What a user may do, and the only thing the rest of an app needs to ask.
AbilityBuilder
Collects rules in the order they are written, then builds an ability.
AbilityProvider
Puts an ability in the tree, and rebuilds what depends on it when the rules change.
AbilityScope
The inherited widget AbilityProvider publishes.
AbilityUpdate
A rule change, as reported to an Ability.on listener.
Can
Shows child only when the action is permitted.
CanBuilder
Builds either way, told whether the action is permitted.
CanResult
The answer, and why, handed to a CanBuilder.
CompoundCondition
Several conditions combined — and, or, nor, not.
Condition
A parsed condition, ready to be evaluated.
ConditionInterpreter
Evaluates a parsed Condition against a subject.
ConditionsMatch
The result of compiling one rule's conditions.
FieldCondition
A test applied to one field of the subject.
MongoConditionsMatch
A rule's conditions, parsed once and ready to be asked.
MongoQueryParser
Turns a MongoDB-shaped query into a Condition tree.
OperatorCall
Everything an operator needs in order to parse itself.
ParsedConditions
A ConditionsMatch that can hand back the tree it parsed.
QueryLanguage<R>
How that language combines things.
RawRule
One rule, exactly as it travels over the wire.
Rule
A RawRule compiled for asking questions of.
RuleIndex
Rules, indexed by subject type and action so a check does not scan them all.
RuleRef
A handle on the rule just written, so a reason can be attached to it.
Subject
An object paired with the subject type it should be checked as.

Mixins

CaslRecord
A type whose fields conditions can be matched against.
CaslSubject
A type that knows which subject type rules are written about it.

Extensions

AbilityContext on BuildContext
Asking what the user may do, from anywhere with a BuildContext.
AbilityGuard on Ability
Turns a permission check into a guard that throws.

Constants

alwaysTrue → const Condition
The condition every subject satisfies.
anyAction → const String
The action that stands for every action.
anySubjectType → const String
The subject type that stands for every subject type.
defaultFieldsMatcher → const FieldsMatcher
The default FieldsMatcherfieldPatternMatcher.
defaultOperators → const Map<String, OperatorParser>
The field operators CASL.js ships with, and only those.

Functions

caslCompare(Object? a, Object? b) int
Orders two values the way the condition operators need them ordered.
createAliasResolver(Map<String, Object> aliases, {String anyActionName = anyAction, bool validate = true}) ResolveActions
Builds a resolver from a map of alias to what it means.
createMongoAbility(List<RawRule> rules, {MongoQueryParser parser = const MongoQueryParser(), FieldReader read = readField, String? detectSubjectType(Object value)?, String anyActionName = 'manage', String anySubjectTypeName = 'all', List<String> resolveActions(List<String>)?}) Ability
An ability that understands MongoDB-style conditions. The usual entry point.
detectSubjectTypeByRuntimeType(Object value) String
The default, in order of how much it can be trusted.
fieldPatternMatcher(List<String> fields) bool Function(String field)
Matches a field name against patterns, with * and **.
hasField(Object? target, String field) bool
Whether target has field at all, as $exists means it.
isSubjectType(Object? value) bool
Whether value names a kind of thing rather than being one.
mongoConditionsMatcher({MongoQueryParser parser = const MongoQueryParser(), FieldReader read = readField}) ConditionsMatcher
Builds the conditions matcher, optionally over a custom parser or reader.
oneOrMany(Object value, String what) List<String>
Normalises the one-or-many shape every CASL field accepts.
packRules(List<RawRule> rules, {String packSubject(String subjectType)?}) List<PackedRule>
Squeezes rules into the array form CASL.js's packRules produces.
permittedFieldsOf(Ability ability, String action, Object? subject, {required List<String> allFields}) List<String>
Which fields of subject may be touched by action.
readField(Object target, String field) Object?
Reads a field from a Map or a CaslRecord.
readParent(Object? target, String path, {FieldReader read = readField}) → (Object?, String)
The object a path's last segment should be read from, and that segment.
readPath(Object? target, String path, {FieldReader read = readField}) Object?
Reads path out of target, following dots and flattening lists.
rulesToAst(Ability ability, String action, String subjectType) Condition?
The Condition form of rulesToCondition — the one most callers want.
rulesToCondition<R>(List<Rule> rules, RuleConverter<R> convert, QueryLanguage<R> language) → R?
Turns a grant into a filter: "which records may this user act on".
rulesToFields(Ability ability, String action, String subjectType) Map<String, Object?>
The values a new subject must have for action to be permitted on it.
subject(String type, Object? value) Subject
Pairs value with the subject type it should be checked as.
subjectValue(Object? subject) Object?
The object conditions should be matched against.
unpackRules(List<Object?> packed, {String unpackSubject(String subjectType)?}) List<RawRule>
Reads what packRules wrote, or what CASL.js's packRules wrote.

Typedefs

ConditionsMatcher = ConditionsMatch Function(Map<String, Object?> conditions)
Compiles a rule's conditions into something that can be asked.
DetectSubjectType = String Function(Object value)
Works out which subject type an arbitrary object should be checked as.
FieldReader = Object? Function(Object target, String field)
Reads one field from one object.
FieldsMatcher = bool Function(String field) Function(List<String> fields)
Compiles a rule's fields into a predicate over one field name.
ForbiddenMessageBuilder = String Function(ForbiddenError error)
Builds the message shown when something is refused.
OperatorParser = Condition Function(OperatorCall call)
How one $operator becomes a Condition.
PackedRule = List<Object?>
One rule in its compact wire form: a JSON array, not an object.
ResolveActions = List<String> Function(List<String> actions)
Expands an action into itself plus everything it stands for.
RuleConverter<R> = R Function(Rule rule)
How the conditions of one rule become whatever a query builder speaks.

Exceptions / Errors

ForbiddenError
Thrown when an action is not permitted.