PHP code example of halaabdulmottleeb / bitmask-permissions

1. Go to this page and download the library: Download halaabdulmottleeb/bitmask-permissions 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/ */

    

halaabdulmottleeb / bitmask-permissions example snippets


enum PostPermission: int
{
    case READ   = 1;   // 0001
    case WRITE  = 2;   // 0010
    case UPDATE = 4;   // 0100
    case DELETE = 8;   // 1000
}

use HalaAbdulmottleb\BitmaskPermissions\Traits\HasBitmaskPermissions;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use HasBitmaskPermissions;
}

$user->grantPermissionTo(PostPermission::READ->value, $post);
$user->grantPermissionTo(PostPermission::WRITE->value, $post);

$user->hasPermissionTo(PostPermission::READ->value, $post);  // true
$user->hasPermissionTo(PostPermission::DELETE->value, $post); // false

$user->revokePermission(PostPermission::WRITE->value, $post);

$user->grantPermissionTo(
    PostPermission::READ->value | PostPermission::WRITE->value,
    $post
);

return [
    'table' => 'permission_grants',
];
bash
php artisan vendor:publish --tag=bitmask-permissions-migrations
php artisan migrate
bash
php artisan vendor:publish --tag=bitmask-permissions-config