moffhub / maker-checker
A laravel package to help manage with maker checker tight requirement
Installs: 3 388
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:project
Requires
- php: ^8.2
- illuminate/support: ^12.0
- sourcetoad/enhanced-resources: ^7.0
Requires (Dev)
- laravel/pint: ^1.17
- orchestra/testbench: ^10.1
- phpstan/phpstan: ^1.12
- phpunit/phpunit: ^11.3
README
MakerChecker is a package that provides a simple way to implement the Maker-Checker pattern in your Laravel application.
Installation
You can install the package via composer:
composer require moffhub/maker-checker
This package depends on the sourcetoad\enhanced-resources
which you can install via composer:
composer require sourcetoad\enhanced-resources
You can publish the config file with:
php artisan vendor:publish --provider="Moffhub\MakerChecker\MakerCheckerServiceProvider" --tag="config"
This is the contents of the published config file:
Usage
First, you need to add the ChecksRequests
trait to the model you want to check against for roles and permissions typically the User
Model.
use Moffhub\MakerChecker\Traits\ChecksRequests; class Post extends Model { use ChecksRequests; }
Then, you can use the makerChecker
method to create a new record.
if (auth()->user()->requiresApproval('create', Post::class)) { $approvalRequest = MakerChecker::request() ->toExecute( $approvableActionClass, (array) $data, ) ->madeBy(auth()->user()) ->description('Create Posts') ->save(); return MakerCheckerResource::make($approvalRequest) ->format(MakerCheckerResource::SIMPLE) ->response() ->setStatusCode(Response::HTTP_MULTI_STATUS); }
Approvable actions
This library supports create, delete, update and execute actions. You can create your own actions by extending the ApprovableAction
class.
This is configured by either calling
for execute Additionally you can pass an array of roles that can approve the request and the number of approvals required for the request to be approved by each role
MakerChecker::request()->toExecute( $approvableActionClass, (array) $data, [ 'admin'=> 2, 'client' => 3, ], )
or for create
MakerChecker::request()->toCreate( $createActionClass, (array) $data, )
or for update
MakerChecker::request()->toUpdate( $updateActionClass, (array) $data, )
or for delete
MakerChecker::request()->toDelete( $deleteActionClass, (array) $data, )