Testing and Tooling
Generators
make:control and make:perimeter scaffold the controls and the perimeter classes, from stubs that you can publish and change.
php artisan make:perimeter ClientPerimeter
php artisan make:perimeter EditorialPerimeter --hooks
php artisan make:control PostControl --model=Post --perimeters=ClientPerimeter --perimeters=EditorialPerimeter
php artisan make:control CommentControl --parent=post
make:control
Writes a control into app/Access/Controls.
| Option | Effect |
|---|---|
--model, -m | The model of the control: --model=Post for App\Models\Post, or a fully qualified class such as --model='Domain\Blog\Post'. Without it, the model is named after the control: App\Models\Post for PostControl. |
--perimeters, -p | A perimeter class of app/Access/Perimeters, repeatable. The control gets an allowed(), should() and query() callback for each one, which refuse everything until you write them, except the callbacks of the hooks that the class overrides. |
--parent | A belongsTo relation of the model, repeatable: writes a ThroughParent entry for it. |
- The command refuses, and writes nothing, when the model is not an Eloquent model class.
- The perimeters are listed as loose perimeters, which add up: group the levels of a hierarchy in
Levels::of()yourself. - A perimeter class whose constructor requires arguments, or is not public, is left out with a warning: list it in the control with its named constructor.
- The control declares no boundary: add a
boundary()method when the model needs one.
See Controls.
make:perimeter
Writes a perimeter class into app/Access/Perimeters.
- Without option, the class is empty: its rules are the callbacks declared in the control.
--hooksgenerates the protected hooksallows(),includes()andscope(), which refuse everything until you write them, for a rule shared by several controls. Removeincludes()to check the rows in SQL.
See Perimeter classes.
Stubs
Publish the stubs to change what the commands write:
php artisan stub:publish
| Stub | Used by |
|---|---|
stubs/control.stub | make:control |
stubs/perimeter.plain.stub | make:perimeter |
stubs/perimeter.hooks.stub | make:perimeter --hooks |
A published stub replaces the one of the package. After an upgrade, publish them again with php artisan stub:publish --force, or apply the changes of the package to your copies.
Cache and about
Cache the control of each model with access:cache in every deployment, and read the state of the package in the Access Control section of php artisan about.
Abilities
The Ability value object names what a reader wants to do: how a policy method maps to an ability, how to change the mapping, and how business abilities work.