From f859a5da06fd06860dde3fa37d8ebd28ac7eb7d0 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Thu, 15 Jun 2023 10:27:18 +0200 Subject: [PATCH] Move RLS toggling and model directories config to `tenancy.rls` --- assets/config.php | 6 +++++- src/Database/Concerns/BelongsToTenant.php | 4 ++-- src/Database/Concerns/DealsWithModels.php | 4 +--- tests/PostgresRLSTest.php | 15 ++++++--------- 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/assets/config.php b/assets/config.php index 3518a623..225a67d5 100644 --- a/assets/config.php +++ b/assets/config.php @@ -181,7 +181,11 @@ return [ * * Requires Postgres with single-database tenancy. */ - 'rls' => false, + ], + + 'rls' => [ + 'enabled' => false, + 'model_directories' => ['app/Models'], ], /** diff --git a/src/Database/Concerns/BelongsToTenant.php b/src/Database/Concerns/BelongsToTenant.php index d0fbd708..c4d89039 100644 --- a/src/Database/Concerns/BelongsToTenant.php +++ b/src/Database/Concerns/BelongsToTenant.php @@ -21,9 +21,9 @@ trait BelongsToTenant public static function bootBelongsToTenant(): void { - // If tenancy.database.rls is true or this model implements RlsModel + // If tenancy.rls.enabled is true or this model implements RlsModel // Scope queries using Postgres RLS instead of TenantScope - if (! (config('tenancy.database.rls') || (new static) instanceof RLSModel)) { + if (! (config('tenancy.rls.enabled') || (new static) instanceof RLSModel)) { static::addGlobalScope(new TenantScope); } diff --git a/src/Database/Concerns/DealsWithModels.php b/src/Database/Concerns/DealsWithModels.php index 9254213b..f02f739a 100644 --- a/src/Database/Concerns/DealsWithModels.php +++ b/src/Database/Concerns/DealsWithModels.php @@ -12,8 +12,6 @@ use Symfony\Component\Finder\SplFileInfo; trait DealsWithModels { - public static array $modelDirectories = ['App/Models']; - public static Closure|null $modelDiscoveryOverride = null; public static function getModels(): Collection @@ -22,7 +20,7 @@ trait DealsWithModels return (static::$modelDiscoveryOverride)(); } - $modelFiles = Finder::create()->files()->name('*.php')->in(static::$modelDirectories); + $modelFiles = Finder::create()->files()->name('*.php')->in(config('tenancy.rls.model_directories')); $classes = collect($modelFiles)->map(function (SplFileInfo $file) { $fileContents = str($file->getContents()); diff --git a/tests/PostgresRLSTest.php b/tests/PostgresRLSTest.php index 80702c64..f1b53a2b 100644 --- a/tests/PostgresRLSTest.php +++ b/tests/PostgresRLSTest.php @@ -4,6 +4,7 @@ declare(strict_types=1); use Illuminate\Support\Str; use Illuminate\Support\Facades\DB; +use Stancl\Tenancy\Tests\Etc\Post; use Stancl\Tenancy\Tests\Etc\Tenant; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Schema; @@ -13,15 +14,13 @@ use Stancl\Tenancy\Database\TenantScope; use Illuminate\Database\Schema\Blueprint; use Stancl\Tenancy\Tests\Etc\ScopedComment; use Stancl\Tenancy\Events\TenancyInitialized; +use Stancl\Tenancy\Database\Concerns\RlsModel; use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Jobs\DeleteTenantsPostgresUser; use Illuminate\Database\Eloquent\Concerns\HasUuids; use Stancl\Tenancy\Jobs\CreatePostgresUserForTenant; use Stancl\Tenancy\Listeners\RevertToCentralContext; use Stancl\Tenancy\Bootstrappers\Integrations\PostgresRLSBootstrapper; -use Stancl\Tenancy\Database\Concerns\RlsModel; -use Stancl\Tenancy\Tenancy; -use Stancl\Tenancy\Tests\Etc\Post; beforeEach(function () { DB::purge($centralConnection = config('tenancy.database.central_connection')); @@ -29,10 +28,9 @@ beforeEach(function () { Event::listen(TenancyInitialized::class, BootstrapTenancy::class); Event::listen(TenancyEnded::class, RevertToCentralContext::class); - Tenancy::$modelDirectories = [__DIR__ . '/Etc']; - // Turn RLS scoping on - config(['tenancy.database.rls' => false]); + config(['tenancy.rls.enabled' => false]); + config(['tenancy.rls.model_directories' => [__DIR__ . '/Etc']]); config(['tenancy.bootstrappers' => [PostgresRLSBootstrapper::class]]); config(['database.connections.' . $centralConnection => config('database.connections.pgsql')]); config(['tenancy.models.tenant_key_column' => 'tenant_id']); @@ -142,7 +140,7 @@ test('correct rls policies get created', function () { test('global scope is not applied when using rls', function () { // By default, TenantScope is added to models using BelongsToTenant - // If config('tenancy.database.rls') is false (which it is by default) + // If config('tenancy.rls.enabled') is false (which it is by default) expect(Post::hasGlobalScope(TenantScope::class))->toBeTrue(); // Clear booted models to forget the global scope and see if it gets applied during the boot @@ -153,7 +151,7 @@ test('global scope is not applied when using rls', function () { // The model shouldn't have the global scope expect(RlsPost::hasGlobalScope(TenantScope::class))->toBeFalse(); - config(['tenancy.database.rls' => true]); + config(['tenancy.rls.enabled' => true]); Post::clearBootedModels(); Post::bootBelongsToTenant(); @@ -236,7 +234,6 @@ trait UsesUuidAsPrimaryKey }); } } - /** * Post model that implements the RlsModel interface. */