1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-04 19:24:02 +00:00

Move RLS toggling and model directories config to tenancy.rls

This commit is contained in:
lukinovec 2023-06-15 10:27:18 +02:00
parent 6b19a26d2c
commit f859a5da06
4 changed files with 14 additions and 15 deletions

View file

@ -181,7 +181,11 @@ return [
*
* Requires Postgres with single-database tenancy.
*/
'rls' => false,
],
'rls' => [
'enabled' => false,
'model_directories' => ['app/Models'],
],
/**

View file

@ -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);
}

View file

@ -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());

View file

@ -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.
*/