AM2studio / Laravel-ACL by msamec

acl
1,564
0
4
Package Data
Maintainer Username: msamec
Maintainer Contact: marko.samec@am2studio.hr (Marko Šamec)
Package Create Date: 2016-01-26
Package Last Update: 2016-01-26
Language: PHP
License: MIT
Last Refreshed: 2024-11-19 03:23:35
Package Statistics
Total Downloads: 1,564
Monthly Downloads: 1
Daily Downloads: 0
Total Stars: 0
Total Watchers: 4
Total Forks: 0
Total Open Issues: 0

laravel-acl

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.