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

Rename RLSModel to RlsModel, fix global scope test

This commit is contained in:
lukinovec 2023-06-07 13:55:43 +02:00
parent f84e3ffc02
commit fa43d8ae1d
3 changed files with 19 additions and 26 deletions

View file

@ -21,9 +21,9 @@ trait BelongsToTenant
public static function bootBelongsToTenant(): void public static function bootBelongsToTenant(): void
{ {
// If tenancy.database.rls is true or this model implements RLSModel // If tenancy.database.rls is true or this model implements RlsModel
// Scope queries using Postgres RLS instead of TenantScope // Scope queries using Postgres RLS instead of TenantScope
if (! (config('tenancy.database.rls') || in_array(RLSModel::class, class_implements(static::class)))) { if (! (config('tenancy.database.rls') || in_array(RlsModel::class, class_implements(static::class)))) {
static::addGlobalScope(new TenantScope); static::addGlobalScope(new TenantScope);
} }

View file

@ -12,6 +12,6 @@ namespace Stancl\Tenancy\Database\Concerns;
* *
* @see \Stancl\Tenancy\Database\Concerns\BelongsToTenant * @see \Stancl\Tenancy\Database\Concerns\BelongsToTenant
*/ */
interface RLSModel interface RlsModel
{ {
} }

View file

@ -19,7 +19,7 @@ use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Stancl\Tenancy\Jobs\CreatePostgresUserForTenant; use Stancl\Tenancy\Jobs\CreatePostgresUserForTenant;
use Stancl\Tenancy\Listeners\RevertToCentralContext; use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Bootstrappers\Integrations\PostgresRLSBootstrapper; use Stancl\Tenancy\Bootstrappers\Integrations\PostgresRLSBootstrapper;
use Stancl\Tenancy\Database\Concerns\RLSModel; use Stancl\Tenancy\Database\Concerns\RlsModel;
use Stancl\Tenancy\Tenancy; use Stancl\Tenancy\Tenancy;
use Stancl\Tenancy\Tests\Etc\Post; use Stancl\Tenancy\Tests\Etc\Post;
@ -143,14 +143,18 @@ test('global scope is not applied when using rls', function () {
// If config('tenancy.database.rls') is false (which it is by default) // If config('tenancy.database.rls') is false (which it is by default)
expect(Post::hasGlobalScope(TenantScope::class))->toBeTrue(); expect(Post::hasGlobalScope(TenantScope::class))->toBeTrue();
// RLSPost and RLSScopedComment implement the RLSModel interface // Clear booted models to forget the global scope and see if it gets applied during the boot
RlsPost::clearBootedModels();
RlsPost::bootBelongsToTenant();
// RlsPost implements the RlsModel interface
// The model shouldn't have the global scope // The model shouldn't have the global scope
expect(RLSPost::hasGlobalScope(TenantScope::class))->toBeFalse(); expect(RlsPost::hasGlobalScope(TenantScope::class))->toBeFalse();
config(['tenancy.database.rls' => true]); config(['tenancy.database.rls' => true]);
// Clear booted Post to forget the global scope and see if it gets applied during the boot
Post::clearBootedModels(); Post::clearBootedModels();
Post::bootBelongsToTenant();
expect(Post::hasGlobalScope(TenantScope::class))->toBeFalse(); expect(Post::hasGlobalScope(TenantScope::class))->toBeFalse();
}); });
@ -169,25 +173,25 @@ test('queries are correctly scoped using RLS', function() {
// Create posts and comments for both tenants // Create posts and comments for both tenants
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
$post1 = RLSPost::create(['text' => 'first post']); $post1 = RlsPost::create(['text' => 'first post']);
$post1Comment = $post1->scoped_comments()->create(['text' => 'first comment']); $post1Comment = $post1->scoped_comments()->create(['text' => 'first comment']);
tenancy()->end(); tenancy()->end();
tenancy()->initialize($secondTenant); tenancy()->initialize($secondTenant);
$post2 = RLSPost::create(['text' => 'second post']); $post2 = RlsPost::create(['text' => 'second post']);
$post2Comment = $post2->scoped_comments()->create(['text' => 'second comment']); $post2Comment = $post2->scoped_comments()->create(['text' => 'second comment']);
tenancy()->end(); tenancy()->end();
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
expect(RLSPost::all()->pluck('text')) expect(RlsPost::all()->pluck('text'))
->toContain($post1->text) ->toContain($post1->text)
->not()->toContain($post2->text); ->not()->toContain($post2->text);
expect(RLSScopedComment::all()->pluck('text')) expect(ScopedComment::all()->pluck('text'))
->toContain($post1Comment->text) ->toContain($post1Comment->text)
->not()->toContain($post2Comment->text); ->not()->toContain($post2Comment->text);
@ -195,8 +199,8 @@ test('queries are correctly scoped using RLS', function() {
tenancy()->initialize($secondTenant); tenancy()->initialize($secondTenant);
expect(RLSPost::all()->pluck('text'))->toContain($post2->text)->not()->toContain($post1->text); expect(RlsPost::all()->pluck('text'))->toContain($post2->text)->not()->toContain($post1->text);
expect(RLSScopedComment::all()->pluck('text'))->toContain($post2Comment->text)->not()->toContain($post1Comment->text); expect(ScopedComment::all()->pluck('text'))->toContain($post2Comment->text)->not()->toContain($post1Comment->text);
tenancy()->end(); tenancy()->end();
}); });
@ -220,23 +224,12 @@ trait UsesUuidAsPrimaryKey
} }
/** /**
* Post model that implements the RLSModel interface. * Post model that implements the RlsModel interface.
*/ */
class RLSPost extends Post implements RLSModel class RlsPost extends Post implements RlsModel
{ {
public function getForeignKey() public function getForeignKey()
{ {
return 'post_id'; return 'post_id';
} }
} }
/**
* ScopedComment model that implements the RLSModel interface.
*/
class RLSScopedComment extends ScopedComment implements RLSModel
{
public function getForeignKey()
{
return 'comment_id';
}
}