am2studio/laravel-acl

1.0.0 2016-01-26 13:00 UTC

This package is not auto-updated.

Last update: 2024-11-09 19:40:41 UTC


README

Latest Version on Packagist Software License Total Downloads

Install

Via Composer

$ composer require am2studio/laravel-acl

Service Provider

in config/app.php

under 'providers' add

AM2Studio\LaravelAcl\LaravelAclServiceProvider::class

Config and Migration

php artisan vendor:publish --provider="AM2Studio\LaravelACL\LaravelACLServiceProvider" --tag=config
php artisan vendor:publish --provider="AM2Studio\LaravelACL\LaravelACLServiceProvider" --tag=migrations

Trait

In user model add following

...
use AM2Studio\LaravelAcl\Traits\LaravelACLTrait;

class User extends Model {
    use LaravelACLTrait, ... ;
}

Usage

Creating roles

use AM2Studio\LaravelACL\Models\Role;

$userRole = Role::create([
    'name' => 'User',
    'slug' => 'user',
    'description => '',
]);

Attaching and detaching roles

$user = User::find(1);
$user->attachRole($userRole);
$user->detachRole($userRole);
$user->detachAllRoles();

Checking for role

$user->is('user'); // Checkes if user has user role
$user->is('admin|user'); // Checks if user has user OR admin role
$user->is('admin|user', true); // Checks if user has user AND admin role

Creating permissions

use AM2Studio\LaravelACL\Models\Permssion;

$p = Role::create([
    'name' => 'Event edit',
    'slug' => 'event.edit',
    'description => '',
]);

Attaching and detaching permissions

$user = User::find(1);
$user->attachPermission($p);
$user->detachPermission($p);
$user->detachAllPermissions();

$role = Role::find(1);
$role->attachPermission($p);
$role->detachPermission($p);
$role->detachAllPermissions();

Checking for role

$user->can('event.edit'); // Checkes if user has permission
$user->can('event.edit|event.create'); // Checks if user has event.edit OR event.create permission
$user->can('event.edit|event.create', true); // Checks if user has event.edit AND event.create permission

Model permissions

$p2 = Role::create([
    'name' => 'Event edit',
    'slug' => 'event.edit',
    'description => '',
    'model' => 'event',
]);
$p3 = Role::create([
    'name' => 'Event edit',
    'slug' => 'event.edit',
    'description => '',
    'model' => 'event',
    'resource_id' => 1, 
]);

$event = Event::find(1);

$user->attachPermisson($p2);
$user->attachPermisson($p3);

$user->allowed('event.edit', $event); // Checks if user has rights to Event model
$user->allowed('event.edit', $event, $event->id); // Checks if user has rights to Event model with selected id

Blade Extensions

@role('admin')
@endrole

@permission('edit.event')
@endpermission

@allowed('edit.event', $event)
@endallowed

@role('admin|user', 'all')
@endrole

Change log

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Credits

License

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