PHP code example of zaichaopan / access-granted

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/ */

    

zaichaopan / access-granted example snippets


// roles
Schema::create('roles', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->unique('name');
    $table->timestamps();
});

// permissions
Schema::create('permissions', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->unique('name');
    $table->timestamps();
});

// permission_user
Schema::create('permission_user', function (Blueprint $table) {
    $table->increments('id');
    $table->unsignedInteger('user_id');
    $table->unsignedInteger('permission_id');
    $table->unique(['user_id', 'permission_id']);
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    $table->foreign('permission_id')->references('id')->on('permissions') ->onDelete('cascade');
    $table->timestamps();
});

// permission_role
Schema::create('permission_role', function (Blueprint $table) {
    $table->increments('id');
    $table->unsignedInteger('permission_id');
    $table->unsignedInteger('role_id');
    $table->unique(['permission_id', 'role_id']);
    $table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade');
    $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
    $table->timestamps();
})

// role_user
 Schema::create('role_user', function (Blueprint $table) {
    $table->increments('id');
    $table->unsignedInteger('role_id');
    $table->unsignedInteger('user_id');
    $table->unique(['role_id', 'user_id']);
    $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    $table->timestamps();
});

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


protected $routeMiddleware = [
    'role' => \Zaichaopan\AccessGranted\Middleware\RoleMiddleware::class,
    'permission' => \Zaichaopan\AccessGranted\Middleware\PermissionMiddleware::class,
];

class AdminController extends Controller
{
    public function __construct()
    {
        $this->middleware('role:admin')
    }
}

class PostsController extends Controller
{
    public function __construct()
    {
        $this->middleware('permission:edit post')
    }
}
bash
php artisan migrate