rickgoemans/laravel-permission-service

A Laravel permission helper service

v4.0.0 2024-03-13 11:09 UTC

This package is auto-updated.

Last update: 2024-11-08 05:05:47 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require rickgoemans/laravel-permission-service

You can publish the config file with:

php artisan vendor:publish --tag="laravel-permission-service-config"

This is the contents of the published config file:

<?php

use Rickgoemans\LaravelPermissionService\Enums\PermissionAction;

// config for Rickgoemans/LaravelPermissionService
return [
    /*
     * The separator that's being used between the model it's name and the action.
     */
    'separator'    => '.',

    /*
     * The names of the methods that reflect the action.
     */
    'action_names' => [
        PermissionAction::VIEW->value         => 'view',
        PermissionAction::CREATE->value       => 'create',
        PermissionAction::UPDATE->value       => 'update',
        PermissionAction::DELETE->value       => 'delete',
        PermissionAction::RESTORE->value      => 'restore',
        PermissionAction::FORCE_DELETE->value => 'force_delete',
    ],
    
    /*
     * The package prefix that is being used for package permissions.
     */
    'package_prefix' => 'package',
    
    /*
     * The application prefix that is being used for application permissions.
     */
    'application_prefix' => 'app',
];

Usage

Here's an example in a configuration file containing all permissions that the system holds to sync them on deployment (through a seeder probably).

<?php

use App\Models\Activity;
use App\Models\Customer;
use App\Models\User;
use Rickgoemans\LaravelPermissionService\Services\PermissionService;

return [
    'all' => [
        PermissionService::viewPermission(Activity::class),
        ...PermissionService::crudPermissions(User::class),
        ...PermissionService::crudPermissions(Customer::class, true, true),
        ...PermissionService::packagePermission('nova'),
        ...PermissionService::packagePermission('admin_panel'),
    ],
];

Here's an example of how this can be used in a policy.

<?php

namespace App\Policies;

use App\Models\Example;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
use Illuminate\Auth\Access\Response;

class ExamplePolicy extends Controller {
    use HandlesAuthorization;
    
    public function viewAny(User $auth): Response|bool
    {
        return $auth->can(PermissionService::viewPermission(Example::class));
    }

    public function view(User $auth, Example $example): Response|bool
    {
        return $auth->can(PermissionService::viewPermission(Example::class));
    }

    public function create(User $auth): Response|bool
    {
        return $auth->can(PermissionService::createPermission(Example::class));
    }

    public function update(User $auth, Example $example): Response|bool
    {
        return $auth->can(PermissionService::updatePermission(Example::class));
    }

    public function delete(User $auth, Example $example): Response|bool
    {
        return $auth->can(PermissionService::deletePermission(Example::class));
    }

    public function restore(User $auth, Example $example): Response|bool
    {
        return $auth->can(PermissionService::restorePermission(Example::class));
    }

    public function forceDelete(User $auth, Example $example): Response|bool
    {
        return $auth->can(PermissionService::forceDeletePermission(Example::class));
    }
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.