- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1
Installation
This package is available to be installed through Composer, by using
composer require codestage/laravel-authorizationYou can choose to do all that the installation does automatically yourself, by following the manual installation steps:
php artisan vendor:publish --tag=configuration --provider=AuthorizationServiceProviderCreate a String-backed enum inside your app/Enums folder, that should implement the IPermissionEnum interface.
For example:
<?php
namespace App\Enums;
use Codestage\Authorization\Contracts\IPermissionEnum;
enum Permission: string implements IPermissionEnum
{
    //
}Update the authorize.php configuration file, by setting the permissions_enum to your newly created Permission class.
For example:
'permissions_enum' => App\Enums\Permission::classUpdate your application configuration file bootstrap/app.php to add the AuthorizationMiddleware to all groups where you want it to apply.
return Application::configure(...)
    ...
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->appendToGroup('web', AuthorizationMiddleware::class);
        $middleware->appendToGroup('api', AuthorizationMiddleware::class);
    })This will check on each request whether the target is decorated with the Authorize attribute and perform authentication and authorization accordingly.
Note: Adding the middleware to the global middleware will not work because of how Laravel request pipeline works. It has to be added to middleware groups (or individual routes if you so choose) in order for authorization to work.
If you are using the old application structure, add the middleware to the Kernel
Inside your HTTP Kernel, you should add the AuthorizationMiddleware to all middleware groups. For example:
    protected $middlewareGroups = [
        'web' => [
            EncryptCookies::class,
            AddQueuedCookiesToResponse::class,
            StartSession::class,
            AuthenticateSession::class,
            ShareErrorsFromSession::class,
            VerifyCsrfToken::class,
            AuthorizationMiddleware::class,
            SubstituteBindings::class,
        ],
        'api' => [
            EnsureFrontendRequestsAreStateful::class,
            'throttle:api',
	    AuthorizationMiddleware::class,
	    SubstituteBindings::class,
        ],
    ];Note 1: Adding the middleware to the global middleware will not work because of how Laravel request pipeline works. It has to be added to middleware groups (or individual routes if you so choose) in order for authorization to work.
Note 2: We strongly recommend adding the middleware before
SubstituteBindings.
Add the HasPermissions trait to your user model class (typically app/Models/User.php)
Run the migrations in order to create the required database schema.
php artisan migrate