This package is a package to provide Roles & Permissions management for any kind of objects, at this moment, it is designed to use for Laravel 5
Use composer to download the package
composer require techexim/authorization
Add Service Provider to Laravel application
TechExim\Auth\AuthServiceProvider::class,
You might need to publish default migrations and configuration
php artisan vendor:publish
Guard is a simple implemented service which provides simplest way to check permission of a subject on an object
use TechExim\Auth\Contracts\Guard;
class MyController extends Controller
{
public function postAuth(Guard $guard)
{
// Check permissions of a subject
// Both subject and object must implement TechExim\Auth\Contracts\Item
$guard->hasPermissionTo($subject, 'section.action', $object);
}
}
use TechExim\Auth\Contracts\Role\Repository as RoleRepository;
class MyController extends Controller
{
public function postRole(RoleRepository $roleRepository)
{
// Add a new role
$roleRepository->create('my_role');
// Assign role to an object
// Object must implement TechExim\Auth\Contracts\Item
$roleRepository->assignItemRoleName($object, 'my_role');
// Assign role to a subject on an object
// Both subject and object must implement TechExim\Auth\Contracts\Item
$roleRepository->assignObjectRoleName($subject, 'my_role', $object);
// Get all assigned roles of an object
$roleRepository->getItemRoles($object);
// Check if a subject has a particular role on an object
$roleRepository->hasObjectRoleName($subject, 'my_role', $object);
// Remove subject's role on an object
$roleRepository->removeObjectRoleName($subject, 'my_role', $object);
}
}
use TechExim\Auth\Contracts\Permission\Repository as PermissionRepository;
class MyController extends Controller
{
public function postPermission(PermissionRepository $permissionRepository)
{
// A new permission
$permissionRepository->create('section.action');
// Assign permission to a subject on an object
// Both subject and object must implement TechExim\Auth\Contracts\Item
$permissionRepository->assignObjectPermissionName($subject, 'section.action', $object);
// Check whether a subject has permission on an object
$permissionRepository->hasObjectPermissionName($subject, 'section.action', $object);
}
}
The Laravel Authorization is open-sourced software licensed under the MIT license