All Projects → rennokki → Guardian

rennokki / Guardian

Licence: mit
Eloquent Guardian is a simple permissions system for your users. While there are many other packages for permissions, this one solves everything in the most eloquent way.

Projects that are alternatives of or similar to Guardian

Plans
Laravel Plans is a package for SaaS apps that need management over plans, features, subscriptions, events for plans or limited, countable features.
Stars: ✭ 326 (+169.42%)
Mutual labels:  eloquent, laravel, package, model
Befriended
Eloquent Befriended brings social media-like features like following, blocking and filtering content based on following or blocked models.
Stars: ✭ 596 (+392.56%)
Mutual labels:  eloquent, laravel, package, model
Laravel Likeable
Rate Eloquent models with Likes and Dislikes in Laravel. Development moved to Laravel Love package!
Stars: ✭ 95 (-21.49%)
Mutual labels:  eloquent, laravel, package
Laravel Cascade Soft Deletes
Cascading deletes for Eloquent models that implement soft deletes
Stars: ✭ 498 (+311.57%)
Mutual labels:  eloquent, laravel, package
Laravel Model Status
Easily add statuses to your models
Stars: ✭ 510 (+321.49%)
Mutual labels:  eloquent, laravel, model
Rating
Laravel Eloquent Rating allows you to assign ratings to any model.
Stars: ✭ 175 (+44.63%)
Mutual labels:  eloquent, laravel, model
Blogetc
Easily add a full Laravel blog (with built in admin panel and public views) to your laravel project with this simple package.
Stars: ✭ 198 (+63.64%)
Mutual labels:  eloquent, laravel, package
Elasticsearch
The missing elasticsearch ORM for Laravel, Lumen and Native php applications
Stars: ✭ 375 (+209.92%)
Mutual labels:  eloquent, laravel, model
Laravel Translatable
A Laravel package for multilingual models
Stars: ✭ 624 (+415.7%)
Mutual labels:  eloquent, laravel, package
Laravel Sluggable
An opinionated package to create slugs for Eloquent models
Stars: ✭ 831 (+586.78%)
Mutual labels:  eloquent, laravel, model
Schedule
Schedule is a package that helps tracking schedules for your models. If you have workers in a company, you can set schedules for them and see their availability though the time.
Stars: ✭ 155 (+28.1%)
Mutual labels:  eloquent, laravel, model
Laravel Ownership
Laravel Ownership simplify management of Eloquent model's owner.
Stars: ✭ 71 (-41.32%)
Mutual labels:  eloquent, laravel, package
Eager Load Pivot Relations
Eager load pivot relations for Laravel Eloquent's BelongsToMany relation.
Stars: ✭ 134 (+10.74%)
Mutual labels:  eloquent, laravel, model
Bouncer
Eloquent roles and abilities.
Stars: ✭ 2,763 (+2183.47%)
Mutual labels:  eloquent, laravel, permissions
Laravel Governor
Manage authorization with granular role-based permissions in your Laravel Apps.
Stars: ✭ 131 (+8.26%)
Mutual labels:  laravel, package, permissions
Laravel Ban
Laravel Ban simplify blocking and banning Eloquent models.
Stars: ✭ 572 (+372.73%)
Mutual labels:  eloquent, laravel, package
Watchable
Enable users to watch various models in your application.
Stars: ✭ 65 (-46.28%)
Mutual labels:  eloquent, laravel, package
Laravel Nullable Fields
Handles saving empty fields as null for Eloquent models
Stars: ✭ 88 (-27.27%)
Mutual labels:  eloquent, laravel, package
Vakt
Attribute-based access control (ABAC) SDK for Python
Stars: ✭ 92 (-23.97%)
Mutual labels:  permissions, permission
Laravel Analytics
Analytics for the Laravel framework.
Stars: ✭ 91 (-24.79%)
Mutual labels:  laravel, package

Build Status codecov StyleCI Latest Stable Version Total Downloads Monthly Downloads License

PayPal

Eloquent Guardian

Eloquent Guardian is a simple permissions system for your users. While there are many other packages for permissions, this one solves everything in the most eloquent way.

Installation

Install the package:

$ composer require rennokki/guardian

If your Laravel version does not support package discovery, add this line in the providers array in your config/app.php file:

Rennokki\Guardian\GuardianServiceProvider::class,

Publish the config file & migration files:

$ php artisan vendor:publish

Migrate the database:

$ php artisan migrate

Add the HasPermissions trait to your Eloquent model:

use Rennokki\Guardian\Traits\HasPermissions;

class User extends Model {
    use HasPermissions;
    ...
}

Types of permissions

  • String Type is just a string, it's not related to any model. It is good for permissions that holds accessing abilities or features.
$user->allow('access.dashboard');
  • Global Type is related to a model, but not to a specific one. It can control any model with any ID if set.
$user->allow('edit', Post::class);
  • Global Specific Type is related to a specific model. It cannot control any other model than this specific one.
$user->allow('edit', App\Post::class, 'post_id_here');

Checking permissions

You can check permissions within the model using can(), cannot() or cant().

$user->can('access.dashboard');
$user->cannot('sell.products');
$user->cant('sell.products'); // alias to cannot()

If your user has a permission for an action on a model, it will have access to any model passed with any ID.

$user->allow('view', \App\Flight::class);
$user->can('view', \App\Flight::class, 1); // true, can view flight with ID 1

Allowing and Unprohibiting permissions

Allowing or Unprohibiting produces a grant access to that permission.

$user->allow('cloning');
$user->unprohibit('cloning'); // same as allow

Disallowing and Prohibiting permissions

Disallowing or Prohibiting permissions can be done whenever. The result will always be the same: a denied access.

$user->disallow('commenting');
$user->prohibit('commenting'); // same as disallow

Global Type over Specific Type

Let's say you have a Post class and the user is only allowed to edit or delete only his own posts. Using this way, whenever you check for a Global Type, it will return false, but not if you check for Specific Type.

$user->allow('edit', Post::class, 'his_post_id');
$user->allow('delete', Post::class, 'his_post_id');

$user->can('edit', Post::class); // false
$user->can('edit', Post::class, 'his_post_id'); // true

If you allow the user to edit the Post::class, it will be able to edit any class, with any ID.

$user->allow('edit', Post::class);
$user->can('edit', Post::class, 1); // true

Middleware

You can use the methods within the model as-is, or you can use a middleware to filter permissions for the current authenticated user.

For this, you should add the middleware to your $routeMiddleware array from app\Http\Kernel.php

'guardian' => \Rennokki\Guardian\Middleware\CheckPermission::class,

You can use it in your routes to filter permissions automatically and throw specific exceptions when something occurs.

  • String Middleware
Route::get('/admin', '[email protected]')->middleware('guardian:access.dashboard');
  • Global Type
Route::post('/admin/products', '[email protected]')->middleware('guardian:create,App\Product');
  • Global Specific Type
Route::patch('/admin/{post_id}', '[email protected]')->middleware('guardian:edit,App\Post,post_id');

Note: Instead of putting a specific Post ID, you have just to indicate where the ID of that model will be placed in the route URL.

  • Rennokki\Guardian\Exceptions\PermissionException, if the authenticated user doesn't have permissions.
  • Rennokki\Guardian\Exceptions\RouteException, if the passed route parameter is non-existent.

You can access permission(), modelType() and modelIdPlaceholder() methods within the exception to handle your exception further.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].