emmanix2002/spatie-permissions-installer

A package that adds an artisan command to simplify managing roles, and permissions using the spatie/laravel-permission package

v0.4 2019-09-23 23:36 UTC

This package is auto-updated.

Last update: 2020-07-24 01:26:54 UTC


README

A package that adds an artisan command to simplify managing roles, and permissions using the spatie/laravel-permission package.

Installation

The best option would be to install it via composer:

composer require emmanix2002/spatie-permissions-installer

Lumen

Because Lumen doesn't auto-discover service providers, you'll need to add the ServiceProvider to your bootstrap/app.php file.

$app->register(Emmanix2002\SpatiePermissionInstaller\SpatiePermissionServiceProvider::class);

Requirements

This package requires the spatie/laravel-permission package in your Laravel, or Lumen project.
It's a dependency of this package 😎

Using the Package

The first step is creating your configuration; all configurations must implement the Emmanix2002\SpatiePermissionInstaller\ConfigurationInterface interface.

ConfigurationInterface

The configuration interface exposes a few methods that make it easy to setup your roles, and permissions using spatie/laravel-permission.

ConfigurationInterface::permissions()

This method should return an array of strings; where these strings are the permission names you want to setup in your project.

ConfigurationInterface::roles()

This method is similar to the ConfigurationInterface::permissions() in that it also returns an array of strings as well; the only difference being, these strings are the role names you want to configure in your project.

ConfigurationInterface::guard()

This method only returns a string. This string will be used as the default guard when a guard isn't provided as an argument on the command line.

ConfigurationInterface::getPermissionsForRole(string $roleName)

This method should return either a string, or an array of strings; where each string is a permission name.
This list will later be filtered with existing permissions in the records, and synced on the role.

Running the Command

You run it from your terminal like this:

php artisan permission:install-permission "\App\MyPackage\Configuration\RolesAndPermissions" web

Example Configuration

Below is a sample configuration class.

<?php

namespace App\MyPackage\Configuration;


use Emmanix2002\SpatiePermissionInstaller\ConfigurationInterface;

class RolesAndPermissions implements ConfigurationInterface
{
    const PERM_READ = 'read';
    const PERM_WRITE = 'write';
    const PERM_EXECUTE = 'execute';
    
    const ROLE_USER = 'user';
    const ROLE_GROUP = 'group';
    const ROLE_OTHERS = 'others';
    
    /**
     * Returns an array of strings, where the strings are the permission names.
     *
     * @return string[]
     */
    public function permissions(): array
    {
        return [
            static::PERM_READ,
            static::PERM_WRITE,
            static::PERM_EXECUTE
        ];
    }
    
    /**
     * The guard to use for creating the roles and permissions.
     *
     * @return string
     */
    public function guard(): string
    {
        return 'api';
    }
    
    /**
     * Returns an array of strings, where the strings are the role names.
     *
     * @return string[]
     */
    public function roles(): array
    {
        return [
            static::ROLE_USER,
            static::ROLE_GROUP,
            static::ROLE_OTHERS
        ];
    }
    
    /**
     * Returns either a string, or an array of strings for the permissions that should be added on the role.
     * It supports some special characters like so:
     *
     *  * - this means all configured permissions
     *
     * @param string $roleName
     *
     * @return string|string[]
     */
    public function getPermissionsForRole(string $roleName)
    {
        switch ($roleName) {
            case static::ROLE_USER:
                return '*';
                break;
            case static::ROLE_GROUP:
                return [
                    static::PERM_READ,
                    static::PERM_EXECUTE
                ];
                break;
            case static::ROLE_OTHERS:
                return static::PERM_READ;
                break;
        }
        return '';
    }
}