parsampsh/simpermission

A Package to handle permissions for users in the laravel applications

v0.1.2 2022-01-11 06:50 UTC

This package is auto-updated.

Last update: 2024-06-11 12:08:44 UTC


README

Simpermission is a PHP library for implementing a simple permission management system for users in laravel apps.

Features:

$user->addPermission('what.i.want');
$user->deletePermission('what.i.want');
$user->getPermissions(); // ['foo.bar', 'hello.world'...]

if (! $user->hasPermission('some.thing')) {
    abort(403);
}

Getting started

To get started with this package, first you need to install it in your laravel app using composer:

$ composer install parsampsh/simpermission

Then, you need to run the migrations:

$ php artisan migrate

Now, you have to add two things in your User model:

use Simpermission\HasPermissions;

class User
{
    // ...

    use HasPermissions;

    public function userIsManager()
    {
        return false;
    }

    // ...
}

You saw some methods like addPermission above that was added to your user model. The HasPermissions trait adds them to your model.

Also, there is another concept named Manager user or Super user. The manager user should access anything. When you use hasPermission method for checking a permission for a user, The same permission should be added via addPermission method. But for manager user, anything is allowed.

To determine that which user is manager(full permission access) and which one is not, you should implement a method named userIsManager in your user model:

class User
{
    // ...

    public function userIsManager()
    {
        // example
        return $this->is_manager === 1;
    }

    // ...
}

License

This project is licensed under MIT.