PHP code example of althinect / enum-permission

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

    

althinect / enum-permission example snippets


return [
    'models_path' => 'Models', // Path to your models
    'user_model' => \App\Models\User::class, // Your User model
    'permissions' => [
        [
            'method' => 'viewAny',
            'arguments' => ['User $user'],
            'enum_case' => 'VIEW_ANY',
            'enum_value' => '{{modelName}}.viewAny'
        ],
        // ... other permissions
    ]
];

// In your policies
public function view(User $user, Post $post): bool
{
    return $user->hasPermissionTo(PostPermission::VIEW);
}

namespace App\Permissions;

enum UserPermission: string
{
    case VIEW_ANY = 'User.viewAny';
    case VIEW = 'User.view';
    case CREATE = 'User.create';
    case UPDATE = 'User.update';
    case DELETE = 'User.delete';
    case RESTORE = 'User.restore';
    case FORCE_DELETE = 'User.forceDelete';
}

use App\Permissions\UserPermission;

class UserPolicy
{
    public function view(User $user, User $model): bool
    {
        return $user->hasPermissionTo(UserPermission::VIEW);
    }
}
bash
php artisan vendor:publish --tag="enum-permission-config"
bash
# Sync all permissions
php artisan permission:sync

# Clean existing permissions before sync
php artisan permission:sync --clean

app/
├── Models/
│   └── User.php
├── Permissions/
│   └── UserPermission.php
└── Policies/
    └── UserPolicy.php