1. Go to this page and download the library: Download uwla/lacl library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
uwla / lacl example snippets
use Uwla\Lacl\Traits\HasRole;
use Uwla\Lacl\Contracts\HasRoleContract;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements HasRoleContract
{
use HasRole;
//
}
// single role
$user->addRole(Role::first()); // using Eloquent model
$user->addRole('administrator'); // using string name
// multiple roles
$user->addRole(Role::all()); // using Eloquent Collection
$user->addRole(['editor', 'manager', 'senior']); // using string names
// single role
$user->setRole(Role::first()); // using Eloquent model
$user->setRole('administrator'); // using string name
// multiple roles
$user->setRole(Role::all()); // using Eloquent Collection
$user->setRole(['editor', 'manager', 'senior']); // using string names
$user->getRoles(); // get Eloquent models
$user->getRoleNames(); // get string names
// single role
$user->delRole($role); // using Eloquent model
$user->delRole('editor'); // using string name
// multiple roles
$user->delRole($roles); // using Eloquent Collection
$user->delRole(['editor', 'manager']); // using string names
// all roles
$user->delAllRoles();
// check whether the user has a role
$user->hasRole($role); // using Eloquent model
$user->hasRole('editor'); // using string name
// check whether the user has all of the given multiple roles
$user->hasRoles($roles); // using Eloquent Collection
$user->hasRoles(['editor', 'manager']); // using string names
// check whether the user has at least one of the given roles
$user->hasAnyRole($roles); // using Eloquent Collection
$user->hasAnyRole(['editor', 'manager']); // using string names
$user->countRoles();
$users = User::withRoles($users);
// access the roles via the 'roles' property
$roles_of_first_user = $users[0]->roles
$users = User::withRoleNames($users);
// access the role names via the 'roles' property
$role_names_of_first_user = $users[0]->roles;
use Uwla\Lacl\Traits\HasPermission;
use Uwla\Lacl\Contracts\HasPermissionContract;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements HasPermissionContract
{
use HasPermission;
//
}
// single role
$role->addPermission(Permission::first()); // using Eloquent model
$role->addPermission('manage client emails'); // using string name
// multiple permissions
$role->addPermission(Permission::all()); // using Eloquent Collection
$role->addPermission(['user.view', 'user.create', 'user.delete']); // using string names
// single permission
$role->setPermission(Permission::first()); // using Eloquent model
$role->setPermission('manage client emails'); // using string name
// multiple permissions
$role->setPermissions(Permission::all()); // using Eloquent Collection
$role->setPermissions(['user.view', 'user.create', 'user.delete']); // using string names
$role->getPermissions(); // get Eloquent models
$role->getPermissionNames(); // get string names
// single permission
$role->delPermission($permission); // using Eloquent model
$role->delPermission('view emails'); // using string name
// multiple permissions
$role->delPermission($permissions); // using Eloquent Collection
$role->delPermission(['user.view', 'user.del']); // using string names
// all permissions
$role->delAllPermissions();
// check whether the role has a permission
$role->hasPermission($permission); // using Eloquent model
$role->hasPermission('user.view'); // using string name
// check whether the role has all of the given permissions
$role->hasPermissions($permissions); // using Eloquent Collection
$role->hasPermissions(['user.view', 'user.del']); // using string names
// check whether the role has at least one of the given permissions
$role->hasAnyPermissions($permissions); // using Eloquent Collection
$role->hasAnyPermissions(['user.view', 'user.del']); // using string names
$role->countPermissions();
$roles = Role::withPermissions($roles);
// access the permissions via the 'permission' property
$permissions_of_first_role = $roles[0]->permissions
$roles = Role::withPermissionNames($roles);
// access the permission names via the 'permission' property
$permission_names_of_first_role = $roles[0]->permissions
$permission = Permission::create([
'name' => $customName,
'description' => $optionalDescription,
]);
// shorter way
$permission = Permission::createOne('View confidential documents');
// or many at once
$permissions = Permission::createMany([
'view documents', 'edit documents', 'upload files',
]);
$article = Article::first();
$permission = Permission::create([
'name' => 'article.edit', // can be any name, but standards help automation
'model_type' => Article::class,
'model_id' => $article->id;
]);
// now you could do something like
$user->add($permission);
$user->hasPermission('article.edit', Article::class, $article->id); // true
// standard Eloquent way
$permission = Permission::where('name', 'view documents')->first();
// shorter way
$permission = Permission::getByName('view documents');
// or many at once
$permissions = Permission::getByName([
'view documents', 'edit documents', 'upload files'
]);
$roles = $permission->getRoles();
// or, get the role names
$roleNames = $permission->getRoleNames();
$permission = Permission::getByName('vip content');
// we get all users with the 'VIP' permission.
// the first parameter is the class of the model.
// the second parameter is the name of the id column of the model.
$users = $permission->getModels(User::class, 'id');
// it can be used on users, roles, or any model such as a Team.
// a team could have permissions associated with the team members.
$teams = $permissions->getModels(Team::class, 'id');
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Uwla\Lacl\Traits\Permissionable;
class Article extends Model
{
use Permissionable;
}
// you can fetch the permissions manually and then grant it to the user or role
$viewPermission = $article->getViewPermission();
$role->addPermission($viewPermission); // assign to a role
$user->addPermission($viewPermission); // assign to a specific user
$crudPermissions = $article->getCrudPermissions();
$user->addPermissions($crudPermissions);
$role->addPermissions($crudPermissions);
// but it is easier to grant them via the model
$article->grantViewPermission($role);
$article->grantViewPermission($user);
// grant all crud permissions to the given user/role
$article->grantCrudPermissions($user);
$article->grantCrudPermissions($role);
// you could fetch the permissions manually, then revoke from the user or role
$viewPermission = $article->getViewPermission();
$role->delPermission($viewPermission); // revoke from a role
$user->delPermission($viewPermission); // revoke from a specific user
$crudPermissions = $article->getCrudPermissions();
$user->delPermissions($crudPermissions);
$role->delPermissions($crudPermissions);
// it is easier to revoke them via the model
$article->revokeViewPermission($role);
$article->revokeViewPermission($user);
// revoke all crud permissions to the given user/role
$article->revokeCrudPermissions($user);
$article->revokeCrudPermissions($role);
if ($user->hasPermission($article->getViewPermission())
{
// user can view the article
return new Response(['data' => $article]);
}
if ($user->hasPermission($article->getDeletePermission())
{
// user can delete the article
$article->delete();
return new Response(['success' => true]);
}
$model->deleteThisModelPermissions();
/*
* Register callback to delete permissions associated with this model when it
* gets deleted.
*
* @return void
*/
protected static function boot() {
parent::boot();
static::deleted(function($model) {
Permission::where([
'model_type' => $model::class,
'model_id' => $model->id,
])->delete();
});
}
// create a permission to send emails
Permission::create(['name' => 'sendEmails']);
// shorter way to add, check, and del single permission:
$user->addPermissionToSendEmails();
$user->hasPermissionToSendEmails(); // true
$user->delPermissionToSendEmails();
$user->hasPermissionToSendEmails(); // false
$user->addPermissionToView($article);
$user->hasPermissionToView($article); // true
$user->delPermissionToView($article);
$user->addPermissionToDeleteForever($article);
$user->hasPermissionToView($article); // false, since we deleted it
$user->hasPermissionToDeleteForever($article); // true
// of course, this works for roles too
$role->addPermissionToUpdate($article);
// per user
$articles = $user->getModels(Article::class);
// per role
$articles = $role->getModels(Article::class);
// get all articles this user can view
$articles = $user->getModels(Article::class, 'view');
// get all articles this user can edit
$articles = $user->getModels(Article::class, 'update');
// get all the users this role can delete
$users = $role->getModels(User::class, 'delete');
// get all products this user is able to cancel the delivery of
$products = $user->getModels(Product::class, 'cancelDelivery');
// so, instead of
$article->createCrudPermissions();
$article->deleteUpdatePermission();
$article->grantDeletePermission($user);
// we basically do:
Article::createCrudPermissions();
Article::deleteUpdateAnyPermission();
Article::grantDeleteAnyPermission($user);
Article::deleteGenericModelPermissions();
Article::deleteAllModelPermissions();
namespace App\Policies;
use App\Models\Article;
use Uwla\Lacl\Traits\ResourcePolicy;
use Uwla\Lacl\Contracts\ResourcePolicy as ResourcePolicyContract;
class ArticlePolicy implements ResourcePolicyContract
{
use ResourcePolicy;
public function getResourceModel()
{
return Article::class;
}
}
namespace App\Http\Controllers;
use App\Http\Requests\StoreArticleRequest;
use App\Http\Requests\UpdateArticleRequest;
use App\Models\Article;
use Illuminate\Http\Response;
class ArticleController extends Controller
{
public function __construct()
{
$this->authorizeResource(Article::class, 'article');
}
public function index(): Response
{
return new Response(Article::all());
}
public function store(StoreArticleRequest $request): Response
{
$article = Article::create($request->all());
return new Response($article);
}
public function show(Article $article): Response
{
return new Response($article);
}
public function update(UpdateArticleRequest $request, Article $article): Response
{
$article->update($request->all());
return new Response($article);
}
public function destroy(Article $article): Response
{
return new Response($article);
}
}
protected static function Permission()
{
return CustomPermission::class;
}
protected static function Role()
{
return CustomRole::class;
}