Looking to hire Laravel developers? Try LaraJobs

melange-laravel maintained by fga-php

Description
Laravel integration for the FGA Melange PostgreSQL authorization engine.
Last update
2026/12/01 22:59 (dev-main)
License
Links
Downloads
1

Comments
comments powered by Disqus

FGA Melange Laravel

Laravel integration for Melange, the PostgreSQL-native authorization engine that compiles OpenFGA schemas into SQL functions.

The package wires a Melange\Checker into Laravel, adds a facade, model traits, policies, gates, an access-filtering query scope, and Artisan commands for the Melange workflow. There is no separate authorization service and no tuple synchronization: permissions come from a melange_tuples view over your tables.

Requirements

  • PHP 8.5 or 8.6
  • Laravel 12 or 13 (illuminate/*)
  • PostgreSQL 14+ with pdo_pgsql
  • The Melange CLI v0.11.0 for the fga:* commands

Installation

composer require fga-php/melange-laravel

Publish the configuration and starter files:

php artisan fga:install

This writes:

  • config/fga.php
  • melange/schema.fga
  • melange.yaml
  • a create_melange_tuples_view migration

Edit the schema and the tuples view, then generate the SQL functions:

php artisan fga:validate
php artisan fga:migration
php artisan migrate

fga:migration wraps melange generate migration into a standard Laravel migration, so php artisan migrate remains the only migration runner and the generated SQL is reviewed with the rest of your code.

Usage

The facade

use Melange\Laravel\Facades\Fga;

Fga::check($user, 'can_read', $document);
Fga::check($user, 'can_read', 'document:123');
Fga::check('user:alice', 'can_read', $document);

Models, ObjectRef instances, and "type:id" strings are accepted on both sides of the check. A denied permission returns false; errors such as a missing view or an unexposed relation throw typed exceptions from Fga\FgaException.

Model identity

Add the trait to models that participate in authorization:

use Melange\Laravel\Concerns\HasFgaIdentity;

final class Document extends Model
{
    use HasFgaIdentity;
}

The trait maps Document to the document type and the primary key to the object ID. Override fgaType() when the type name differs.

Policies

use Melange\Laravel\Policies\FgaPolicy;

final class DocumentPolicy extends FgaPolicy
{
    public function view(User $user, Document $document): bool
    {
        return $this->allows($user, 'can_read', $document);
    }

    public function update(User $user, Document $document): bool
    {
        return $this->allows($user, 'can_write', $document);
    }
}

@can('view', $document) works through Laravel's normal policy discovery.

Gates

Map abilities to FGA relations in config/fga.php:

'gates' => [
    'can_read' => 'can_read',
    'can_write' => 'can_write',
],

Then:

@can('can_read', $document)
    ...
@endcan

Filtering queries

$documents = Document::query()
    ->accessibleBy($user, 'can_read')
    ->latest()
    ->paginate();

The scope embeds the accessible_* SQL function in the query instead of checking each row. Run php artisan fga:generate to emit the exact generated function names and use the faster inlined form when available.

Bulk checks

use Fga\CheckRequest;

$results = Fga::checkBulk([
    new CheckRequest($user->fgaObject(), 'can_read', $document->fgaObject()),
    new CheckRequest($user->fgaObject(), 'can_write', $document->fgaObject()),
]);

Explain and list

$trace = Fga::explain($user, 'can_read', $document);
$tree = Fga::expand($document, 'viewer');
$page = Fga::listObjects($user, 'can_read', 'document', new Page(50));

Commands

Command Description
php artisan fga:install Publish config, schema, and the tuples view stub.
php artisan fga:validate Validate the OpenFGA schema.
php artisan fga:migration Generate a Laravel migration from the schema.
php artisan fga:generate Generate relation constants and accessible function names.
php artisan fga:check user:alice can_read document:123 Check one permission.
php artisan fga:explain user:alice can_read document:123 Explain a decision (--json for the wire trace).
php artisan fga:list-objects user:alice can_read document List accessible object IDs.
php artisan fga:list-subjects document:123 can_read user List subjects with access.

Configuration

return [
    'connection' => env('FGA_DB_CONNECTION'), // null = default connection
    'database_schema' => env('FGA_DATABASE_SCHEMA', ''),
    'accessible_functions' => [],
    'gates' => [],
    'subject_type' => 'user',
    'melange' => [
        'binary' => env('FGA_MELANGE_BINARY', 'melange'),
        'config' => env('FGA_MELANGE_CONFIG', base_path('melange.yaml')),
        'schema' => env('FGA_MELANGE_SCHEMA', base_path('melange/schema.fga')),
    ],
    'generate' => [
        'path' => env('FGA_GENERATE_PATH', app_path('FGA/Generated')),
        'namespace' => env('FGA_GENERATE_NAMESPACE', 'App\\FGA\\Generated'),
        'config_path' => env('FGA_GENERATE_CONFIG', config_path('fga-generated.php')),
    ],
];

fga:generate writes config/fga-generated.php, which the service provider merges automatically. The generated Relations class carries type-safe constants and the exact accessible function names. The CLI JSON is validated with PSL Json and Type.

With a custom database_schema, include it in the connection's search_path (for example 'search_path' => 'authz,public' in config/database.php): the generated function bodies resolve melange_tuples through the search path.

Testing

Swap the checker for the in-memory fake:

use Melange\Laravel\Facades\Fga;

Fga::fake()->allow($user->fgaObject(), 'can_read', $document->fgaObject());

$this->assertTrue(Fga::check($user, 'can_read', $document));

The fake defaults to deny and records checks. assertChecked() and assertCheckCount() are available. The application's tests do not need PostgreSQL when the fake is installed.

License

MIT. See LICENSE.