2017 © Pedro Peláez
 

library laravel-object-roles

Roles & Permissions for Laravel where roles can be held in regard to a database object

image

sehrgut/laravel-object-roles

Roles & Permissions for Laravel where roles can be held in regard to a database object

  • Tuesday, July 3, 2018
  • by jsphpl
  • Repository
  • 1 Watchers
  • 0 Stars
  • 142 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Laravel Object Roles

Roles & Permissions for Laravel where roles can be held in regard to a database object, (*1)

Table of Contents

Why?

There are plenty of packages that add ACL functionalities to a Laravel application. Yet, none of the existing solutions seems to allow us to assign roles to users with respect to an object. We often come across a use case where we do not only need to define global roles, such as TECH_ADMIN, but also roles in regard to a database object, such as an Organisation, which has eg. EDITOR users that are granted certain permissions only on that specific Organisation and not on others., (*2)

Disclaimer

This package is in its very early days. It currently makes quite a few assumptions:, (*3)

  1. Users are stored in a users table
  2. The users table already exists when installing the package and running the migrations
  3. The users table has a primary key id which is an unsigned integer
  4. Neither of the following tables exists before installing the package:
    • roles
    • permissions
    • permission_role
    • role_user

Getting Started

1. Install Package

composer require sehrgut/laravel-object-roles

2. Run database migrations

php artisan migrate

3. Use HasRolesAndPermissions trait on the `User model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use SehrGut\LaravelObjectRoles\HasRolesAndPermissions;
use SehrGut\LaravelObjectRoles\Models\Permission;

class User extends Model
{
    use HasRolesAndPermissions;
}

Documentation

Create Roles & Permissions

$permission = SehrGut\LaravelObjectRoles\Models\Permission::create([
    'name' => 'posts.update',
    'description' => 'The ability to update existing Post objects',
]);
$role = SehrGut\LaravelObjectRoles\Models\Role::create([
    'name' => 'EDITOR',
    'description' => 'A User who can manage content',
]);

Assign Permissions to Roles

$role->attachPermission('posts.update');
// or:
$role->attachPermission($permission);

Assign Roles to Users

$user->assignGlobalRole('EDITOR');
$user->assignObjectRole('EDITOR', $organisation);

Check Permissions

$user->hasGlobalPermission('posts.update')
$user->hasPermissionThrough('show_statistics', $organisation)

License

This project is released under the MIT License., (*4)

The Versions

03/07 2018

dev-master

9999999-dev

Roles & Permissions for Laravel where roles can be held in regard to a database object

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joseph Paul