1. Go to this page and download the library: Download zaichaopan/access-granted 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/ */
// User.php
class User extends Model
{
use HasRoles;
}
public function giveRole(string ...$roleName): bool
// you have roles in your admin and manger
$user->giveRole('admin');
// or both
$user->giveRole('admin', 'manager');
public function updateRole(string ...$roleNames) : self
public function updateRole(string ...$roleNames) : self
public function removeRole(string ...$roleNames) : self
// $user have admin and manager roles
// to move manger role
$user->removeRole('manager');
// to move both
$user->removeRole('manager', 'admin');
public function removeAllRoles() : self
$user->removeAllRoles();
public function hasRole(string ...$roles) : bool
$user->hasRole('admin');
// it will return true if the user is admin or manager
$user->hasRole('admin', 'manager');
public function givePermissionTo(string ...$permissionNames): bool
// your have a role admin and you have permissions which names are: write post and delete post
$adminRole->givePermissionTo('write post');
// or both
$adminRole->givePermissionTo('delete post');
public function updatePermissionTo(string ...$permissionNames): bool
$role->updatePermissionTo('read post');
// or both
$role->updatePermissionTo('read post', 'delete post');
public function withdrawPermissionTo(string ...$permissionNames): bool
$role->withdrawPermissionTo('read post');
// or both
$role->withdrawPermissionTo('read post', 'delete post');
public function withdrawAllPermissions(): self
$role->withdrawPermissions();
public function hasPermission(string $permission): bool
$role->hasPermission('read post');
class User extends Model
{
use HasRoles,
HasPermissions;
// ...
}
class User extends Model
{
use HasRoles, HasPermissions, HasPermissionThroughRole;
}
$user->hasPermissionThroughRole('write post');
use Zaichaopan\AccessGranted\Traits\{HasPermissions, HasRolePermissions, HasRoles};
class User extends Model
{
use HasRoles,
HasRolePermissions,
HasPermissions { hasPermission as hasPermissionThroughPermissionTrait; }
protected $connection = 'testbench';
protected $table = 'users';
public function hasPermission(string $permission): bool
{
return $this->hasPermissionThroughPermissionTrait($permission) || $this->hasPermissionThroughRole($permission);
}
}