PHP code example of guava / simple-permissions

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

    

guava / simple-permissions example snippets


return [
    'tables' => [
        'roles' => 'model_has_role',
        'permissions' => 'model_has_permission',
    ],
];

use Guava\SimplePermissions\Concerns\HasAccessControl;

public class User extends Model
{
    use HasAccessControl;
    
    // ...
}

public enum PostPermissions: string implements \Guava\SimplePermissions\Contracts\Permission 
{
    case VIEW = 'view';
    // ...Other redefined permissions
}

public class SuperAdmin implements \Guava\SimplePermissions\Contracts\Role
{

    public function permissions() : array
    {
        return [
            // Add permissions here
            // Either one by one, such as:
            PostPermissions::VIEW,
            
            // or all at once:
           ...PostPermissions::cases()
        ];
    }
}

$user->can(PostPermissions::VIEW)

use Guava\SimplePermissions\Concerns\HasAuthorization;
use App\Auth\Permissions\PostPermissions;

public class PostResource extends Resource
{
    use HasAuthorization;
    
    protected static string $permissions = PostPermissions::class;
    
    // ...
}
bash
php artisan vendor:publish --tag="simple-permissions-migrations"
bash
php artisan migrate
bash
php artisan vendor:publish --tag="simple-permissions-config"
bash
php artisan make:permission PostPermissions
bash
php artisan make:role SuperAdmin