Search by

fga-php / melange

Perer876

PDO client for Melange, the PostgreSQL-native OpenFGA-compatible authorization engine.

Package info

github.com/fga-php/melange

pkg:composer/fga-php/melange

Statistics

Installs: 33

Dependents: 1

Suggesters: 0

Stars: 1

Open Issues: 0

v0.1.0 2026-12-01 21:35 UTC

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_tuples on 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.