Skip to content
This repository has been archived by the owner on Mar 23, 2021. It is now read-only.

thohng/authorization

Repository files navigation

Laravel Authorization Mechanism

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

Installation

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

How to use

Guard

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);
	}
}

Roles

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);
	}
}

Permissions

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);
	}
}

License

The Laravel Authorization is open-sourced software licensed under the MIT license

About

Roles based Authorization

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages