Skip to content

[Feature] Role-Based Authorization Integration #11

@devalade

Description

@devalade

Description

Integrate with Laravel's authorization system beyond basic policies. Support Spatie Permission or Laravel's built-in gates.

Proposed Syntax

php artisan crudify:generate Product --fields="name:string" --roles="admin,editor,viewer"
# or
php artisan crudify:generate Product --fields="name:string" --policy=roles

Generated Components

  • Policy with roles: viewAny, view, create, update, delete checks against roles
  • Middleware: Route-level role middleware
  • UI-level hiding: Hide/show buttons based on permissions
  • Seeders: Generate roles and permissions seeders

Example Policy

class ProductPolicy
{
    public function viewAny(User $user): bool
    {
        return $user->hasAnyRole(['admin', 'editor', 'viewer']);
    }
    
    public function create(User $user): bool
    {
        return $user->hasAnyRole(['admin', 'editor']);
    }
    
    public function delete(User $user, Product $product): bool
    {
        return $user->hasRole('admin') || $user->id === $product->user_id;
    }
}

Blade Directives

@can('create', App\Models\Product::class)
    <a href="{{ route('products.create') }}">Create Product</a>
@endcan

@can('delete', $product)
    <button wire:click="delete({{ $product->id }})" class="text-red-600">Delete</button>
@endcan

Supported Systems

  • Spatie Laravel Permission (preferred)
  • Laravel Breeze/Jetstream teams
  • Custom gate definitions

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions