fga-php / melange
PDO client for Melange, the PostgreSQL-native OpenFGA-compatible authorization engine.
Requires
- php: ~8.5.0 || ~8.6.0
- ext-pdo: *
- ext-pdo_pgsql: *
- fga-php/fga: ^0.1.1
- php-standard-library/json: ^6.4
- php-standard-library/type: ^6.4
Requires (Dev)
- carthage-software/mago: 1.61.0
- phpunit/phpunit: 13.4.8
- rector/rector: 2.9.2
- symfony/process: ^7.0 || ^8.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-12-01 21:54:30 UTC
README
A PDO client for Melange, the PostgreSQL-native authorization engine that compiles OpenFGA schemas into SQL functions.
- No HTTP service and no tuple synchronization.
- Works with any PDO PostgreSQL connection, including Laravel's.
- Checks see uncommitted changes in the current transaction.
- Contextual tuples temporarily shadow
melange_tupleson the same connection. - Cursor pagination, explain traces, and OpenFGA-shaped expand trees.
Installation
composer require fga-php/melange
Requirements: PHP 8.5 or 8.6, pdo_pgsql, PostgreSQL 14+, and a database migrated with
melange migrate (or the SQL files from melange generate migration).
Usage
use Fga; use Melange; $checker = new Melange\Checker($pdo); $allowed = $checker->check( Fga\ObjectRef::of('user', 'alice'), 'can_read', Fga\ObjectRef::of('document', '123'), );
Custom database schema
use Melange; $checker = new Melange\Checker($pdo, new Melange\Config(databaseSchema: 'authz'));
The schema also has to be on the connection's search_path, because the
generated function bodies resolve melange_tuples through it.
Contextual tuples
use Fga; $allowed = $checker->check( Fga\ObjectRef::of('user', 'alice'), 'can_read', Fga\ObjectRef::of('document', '123'), [ new Fga\ContextualTuple( Fga\ObjectRef::of('user', 'alice'), 'ip_allowed', Fga\ObjectRef::of('network', 'office'), ), ], );
Filtering a query
Melange\Checker implements Fga\AccessFilter:
$set = $checker->accessSet( Fga\ObjectRef::of('user', 'alice'), 'can_read', 'document', ); $pdo->query("SELECT * FROM documents WHERE id::text IN ({$set->sql})");
Pass accessibleFunctions to Melange\Config to use the inlined
accessible_{type}_{relation} functions instead of accessible_objects:
new Melange\Config( accessibleFunctions: [ 'document.can_read' => 'accessible_document_can_read', ], );
Errors
All exceptions implement Fga\FgaException. SQLSTATE M2002
(ResolutionTooComplex), M2003 (RelationNotExposed), and 42883
(MissingFunction) map to typed exceptions.
Testing
The package uses PSL JSON and Type for the wire data returned by the generated functions. The integration suite runs against PostgreSQL and installs the functions with the Melange CLI. The Docker image includes the pinned CLI.
just docker test-all
License
MIT. See LICENSE.