tpetry / laravel-postgresql-rls
Row-level security for Laravel applications using PostgreSQL
Requires
- php: ^8.4
- laravel/framework: ^13.0
- tpetry/laravel-postgresql-enhanced: ^3.11
Requires (Dev)
- larastan/larastan: ^3.9
- laravel/pint: ^1.29
- mockery/mockery: ^1.6.13
- orchestra/testbench: ^11.0
- pestphp/pest: ^5.0
- pestphp/pest-plugin-laravel: ^5.0
- pestphp/pest-plugin-phpstan: ^5.2.1
- pestphp/pest-plugin-type-coverage: ^5.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan: 2.3.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Laravel PostgreSQL Row-Level Security
Row-level security for Laravel applications using PostgreSQL
Installation
You can install the package via Composer:
composer require tpetry/laravel-postgresql-rls
Usage
Register an identity resolver, e.g. in the boot() method of your AppServiceProvider, which returns the claims for the RLS policies:
use Illuminate\Support\Facades\Auth; use Tpetry\PostgresqlRls\Facades\RlsIdentityResolver; use Tpetry\PostgresqlRls\RlsIdentity; RlsIdentityResolver::register(fn (array $params, array $context) => new RlsIdentity( claims: ['tenant_id' => Auth::user()->tenant_id], ));
All queries within the authenticated() closure are executed within a transaction with those claims applied:
use Tpetry\PostgresqlRls\Facades\Rls; $invoices = Rls::authenticated(function () { return Invoice::all(); });
The claims are available to your RLS policies as a JSON object in the rls.claims setting, e.g. for a policy created with the row-level security migrations:
use Tpetry\PostgresqlEnhanced\Schema\Blueprint; use Tpetry\PostgresqlEnhanced\Support\Facades\Schema; Schema::table('invoices', function (Blueprint $table) { $table->rowLevelSecurity(); $table->createPolicy( name: 'tenant_isolation', user: 'app', condition: "tenant_id = (SELECT NULLIF(current_setting('rls.claims', true), '')::jsonb ->> 'tenant_id')::bigint", ); });
Changelog
Please see CHANGELOG for more information on what has changed recently.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
License
Laravel PostgreSQL Row-Level Security is open-sourced software licensed under the MIT license.