User role permission control package laravel permission usage notes (larave5 +)

Posted by rxero on Thu, 05 Dec 2019 03:03:56 +0100

Part I installation

This package can be used with Laravel 5.4 or later. If you are using an older version of Laravel, look at the v1 branch of the package.

Step 1: install the package through composer:
composer require spatie/laravel-permission

(optional) in Laravel 5.5, the service provider is automatically registered. In the old version of the framework, just add a service provider in the config/app.php file:

<?php
'providers' => [
    // other code
    Spatie\Permission\PermissionServiceProvider::class,
];

Step 2: publish migration
php artisan vendor:publish --provider="SpatiePermissionPermissionServiceProvider" --tag="migrations"
After the migration is published successfully, run the migration to create the roles and permissions table:
php artisan migrate

Step 4: publish the configuration file:
php artisan vendor:publish --provider="SpatiePermissionPermissionServiceProvider" --tag="config"
The config/permission.php configuration file contains:

<?php
return [

    'models' => [

        /*
         * When using the "HasRoles" feature in this package, we need to know that we should
         * Which Eloquent model to use to get your permissions.
         * Of course, it's usually just a "Permission" model, and you can use any model you like.
         * 
         * The permission model you use must implement
         *  `Spatie\Permission\Contracts\Permission` Contract.
         */

        'permission' => Spatie\Permission\Models\Permission::class,

        /*
         * When using the "HasRoles" feature in this package,
         * We need to know which Eloquent model to use to retrieve your characters.
         * Of course, it's usually just a "Role" model, and you can use any model you like.
         *
         * The permission model you use must implement
         * `Spatie\Permission\Contracts\Role` Contract.
         */

        'role' => Spatie\Permission\Models\Role::class,

    ],

    'table_names' => [

        /*
         * When using the "HasRoles" feature in this package,
         * We need to know which table should be used to retrieve your role. 
         * We chose a basic default, but you can easily change it to what you like.
         */

        'roles' => 'roles',

        /*
         * When using the "HasRoles" feature in this package,
         * We need to know which table should be used to retrieve your permissions. 
         * We chose a basic default, but you can easily change it to any table you like.
         */

        'permissions' => 'permissions',

        /*
         * 
         * When using the "HasRoles" feature in this package,
         * We need to know which table to use to retrieve your model permissions. 
         * We chose a basic default, but you can easily change it to any table you like.
         * 
         */

        'model_has_permissions' => 'model_has_permissions',

        /*
         * When using the "HasRoles" feature in this package,
         * We need to know which table should be used to retrieve your model roles. 
         * We chose a basic default, but you can easily change it to any table you like.
         */

        'model_has_roles' => 'model_has_roles',

        /*
         * When using the "HasRoles" feature in this package,
         * We need to know which table to use to retrieve your role permissions. 
         * We chose a basic default, but you can easily change it to any table you like.
         */

        'role_has_permissions' => 'role_has_permissions',
    ],

    /*
     * By default, all permissions are cached for 24 hours,
     * Unless you update the license or update the role to refresh the cache immediately.
     */

    'cache_expiration_time' => 60 * 24,

    /*
     * When set to true, the required permission/role name is added to the exception message.
     * In some cases, this may be considered information disclosure,
     * So for best security, the default setting is false.
     */

    'display_permission_in_exception' => false,
];

The second part starts to use

Step 5. Add the spatiepermissiontraitsasroles feature to your User model:`

<?php
namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use Notifiable, HasRoles;
    // other code
}

Topics: PHP Laravel