berthott / laravel-api-permissions
Laravel Helper for API Endpoint Permissions
Installs: 1 023
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 3
Forks: 0
Open Issues: 0
pkg:composer/berthott/laravel-api-permissions
Requires (Dev)
- berthott/laravel-api-cache: ^3.0
- berthott/laravel-crudable: ^3.0
- berthott/laravel-targetable: ^3.0
- doctrine/dbal: ^3.3
- orchestra/testbench: ^8.0
- phpunit/phpunit: ^10.0
README
Laravel-API-Permissions
A helper for API Permissions in Laravel. Protect all your routes with a single middleware.
Installation
$ composer require berthott/laravel-api-permissions
Usage
- Caution: This package assumes, that you name all your routes in the way laravel does:
models.action. - Add the
permissionsmiddleware to the routes you want to protect. - Seed the permissions table by writing your own Seeder:
php artisan make:seeder PermissionTableSeeder- utilize
berthott\Permissions\Helpers\PermissionsHelperto actually seed the permissionsPermissionsHelper::resetTables()will truncate all permission related tables.PermissionsHelper::buildRoutePermissions()will build the permissions table. You might pass an array for mapping routes to permissions. E.g.[ '*.destroy' => [ '*.destroy', '*.destroy_many' ], ]
PermissionsHelper::buildUiPermissions()will add UI permissions, that will only be handle by the frontend.
- If the
migrateoption istruethe package will migrate five tables for you:roles,role_user,permissions,permissionablesandpermission_routes. - If the
migrateoption isfalseand you want to write your own migration you can have look at the default migrations by running
$ php artisan vendor:publish --provider="berthott\Permissions\ApiPermissionsServiceProvider" --tag="migrations"
- You can add
permissionsto yourUsermodel by adding theHasPermissionstrait. - You can add
rolesto youUsermodel by adding theHasRolestrait. - You can use either or both of the above options.
- You may ignore specific routes actions from the permission system by adding them to the
ignoreActionsconfig, or by added theIgnorePermissionRoutestrait.
Options
To change the default options use
$ php artisan vendor:publish --provider="berthott\Permissions\ApiPermissionsServiceProvider" --tag="config"
- Inherited from laravel-targetable
namespace: String or array with one ore multiple namespaces that should be monitored for the configured trait. Defaults toApp\Models.namespace_mode: Defines the search mode for the namespaces.ClassFinder::STANDARD_MODEwill only find the exact matching namespace,ClassFinder::RECURSIVE_MODEwill find all subnamespaces. Defaults toClassFinder::RECURSIVE_MODE.prefix: Defines the route prefix. Defaults toapi.
- General Package Configuration
middleware: An array of all middlewares to be applied to all of the generated routes. Defaults to[].ignoreActions: Defines an array of actions that should be ignored by default. Defaults to[].migrate: Defines wether or not to migrate standard tables.. Defaults totrue.
Compatibility
Tested with Laravel 10.x.
License
See License File. Copyright © 2023 Jan Bladt.