mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 01:14:03 +00:00
Use reflection instead of try/catch, use arrays instead of collections
This commit is contained in:
parent
8d13ce7e2f
commit
eddb95c014
3 changed files with 23 additions and 21 deletions
|
|
@ -23,7 +23,9 @@ class CreateRLSPoliciesForTenantTables extends Command
|
||||||
public function handle(): int
|
public function handle(): int
|
||||||
{
|
{
|
||||||
DB::transaction(function () {
|
DB::transaction(function () {
|
||||||
tenancy()->getTenantModels()->each(fn (Model $model) => $this->useRlsOnModel($model));
|
foreach (tenancy()->getTenantModels() as $model) {
|
||||||
|
$this->useRlsOnModel($model);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return Command::SUCCESS;
|
return Command::SUCCESS;
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,9 @@ declare(strict_types=1);
|
||||||
namespace Stancl\Tenancy\Database\Concerns;
|
namespace Stancl\Tenancy\Database\Concerns;
|
||||||
|
|
||||||
use Closure;
|
use Closure;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use ReflectionClass;
|
||||||
use Illuminate\Support\Collection;
|
|
||||||
use Symfony\Component\Finder\Finder;
|
use Symfony\Component\Finder\Finder;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Symfony\Component\Finder\SplFileInfo;
|
use Symfony\Component\Finder\SplFileInfo;
|
||||||
|
|
||||||
trait DealsWithModels
|
trait DealsWithModels
|
||||||
|
|
@ -17,7 +17,7 @@ trait DealsWithModels
|
||||||
/**
|
/**
|
||||||
* Discover all models in the directories configured in 'tenancy.rls.model_directories'.
|
* Discover all models in the directories configured in 'tenancy.rls.model_directories'.
|
||||||
*/
|
*/
|
||||||
public static function getModels(): Collection
|
public static function getModels(): array
|
||||||
{
|
{
|
||||||
if (static::$modelDiscoveryOverride) {
|
if (static::$modelDiscoveryOverride) {
|
||||||
return (static::$modelDiscoveryOverride)();
|
return (static::$modelDiscoveryOverride)();
|
||||||
|
|
@ -25,30 +25,34 @@ trait DealsWithModels
|
||||||
|
|
||||||
$modelFiles = Finder::create()->files()->name('*.php')->in(config('tenancy.rls.model_directories'));
|
$modelFiles = Finder::create()->files()->name('*.php')->in(config('tenancy.rls.model_directories'));
|
||||||
|
|
||||||
$classes = collect($modelFiles)->map(function (SplFileInfo $file) {
|
return array_filter(array_map(function (SplFileInfo $file) {
|
||||||
$fileContents = str($file->getContents());
|
$fileContents = str($file->getContents());
|
||||||
$class = $fileContents->after('class ')->before("\n")->explode(' ')->first();
|
$class = $fileContents->after('class ')->before("\n")->explode(' ')->first();
|
||||||
|
|
||||||
if ($fileContents->contains('namespace ')) {
|
if ($fileContents->contains('namespace ')) {
|
||||||
try {
|
$class = $fileContents->after('namespace ')->before(';')->toString() . '\\' . $class;
|
||||||
return new ($fileContents->after('namespace ')->before(';')->toString() . '\\' . $class);
|
$reflection = new ReflectionClass($class);
|
||||||
} catch (\Throwable $th) {
|
|
||||||
// Skip non-instantiable classes – we only care about models, and those are instantiable
|
// Skip non-instantiable classes – we only care about models, and those are instantiable
|
||||||
|
if ($reflection->getConstructor()?->getNumberOfRequiredParameters() === 0) {
|
||||||
|
$object = new $class;
|
||||||
|
|
||||||
|
if ($object instanceof Model) {
|
||||||
|
return $object;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
})->filter();
|
}, iterator_to_array($modelFiles)));
|
||||||
|
|
||||||
return $classes->filter(fn ($class) => $class instanceof Model);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filter all models retrieved by static::getModels() to get only the models that belong to tenants.
|
* Filter all models retrieved by static::getModels() to get only the models that belong to tenants.
|
||||||
*/
|
*/
|
||||||
public static function getTenantModels(): Collection
|
public static function getTenantModels(): array
|
||||||
{
|
{
|
||||||
return static::getModels()->filter(fn (Model $model) => tenancy()->modelBelongsToTenant($model) || tenancy()->modelBelongsToTenantIndirectly($model));
|
return array_filter(static::getModels(), fn (Model $model) => tenancy()->modelBelongsToTenant($model) || tenancy()->modelBelongsToTenantIndirectly($model));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function modelBelongsToTenant(Model $model): bool
|
public static function modelBelongsToTenant(Model $model): bool
|
||||||
|
|
|
||||||
|
|
@ -113,7 +113,7 @@ test('postgres user can get deleted using the job', function() {
|
||||||
test('correct rls policies get created', function () {
|
test('correct rls policies get created', function () {
|
||||||
$tenantModels = tenancy()->getTenantModels();
|
$tenantModels = tenancy()->getTenantModels();
|
||||||
$modelTables = collect($tenantModels)->map(fn (Model $model) => $model->getTable());
|
$modelTables = collect($tenantModels)->map(fn (Model $model) => $model->getTable());
|
||||||
$getRlsPolicies = fn () => array_map(fn ($policy) => $policy->policyname, DB::select('select * from pg_policies'));
|
$getRlsPolicies = fn () => DB::select('select * from pg_policies');
|
||||||
$getRlsTables = fn () => $modelTables->map(fn ($table) => DB::select('select relname, relrowsecurity, relforcerowsecurity from pg_class WHERE oid = ' . "'$table'::regclass"))->collapse();
|
$getRlsTables = fn () => $modelTables->map(fn ($table) => DB::select('select relname, relrowsecurity, relforcerowsecurity from pg_class WHERE oid = ' . "'$table'::regclass"))->collapse();
|
||||||
|
|
||||||
// Drop all existing policies to check if the command creates policies for multiple tables
|
// Drop all existing policies to check if the command creates policies for multiple tables
|
||||||
|
|
@ -128,9 +128,7 @@ test('correct rls policies get created', function () {
|
||||||
// Check if all tables with policies are RLS protected (even the ones not directly related to the tenant)
|
// Check if all tables with policies are RLS protected (even the ones not directly related to the tenant)
|
||||||
// Models related to tenant through some model must use the BelongsToPrimaryModel trait
|
// Models related to tenant through some model must use the BelongsToPrimaryModel trait
|
||||||
// For the command to create the policy correctly for the model's table
|
// For the command to create the policy correctly for the model's table
|
||||||
expect($getRlsPolicies())
|
expect($getRlsPolicies())->toHaveCount(count($tenantModels)); // 2
|
||||||
->toContain(...$tenantModels->map(fn (Model $model) => $model->getTable() . '_rls_policy', ))
|
|
||||||
->toHaveCount(count($tenantModels)); // 2
|
|
||||||
expect($getRlsTables())->toHaveCount(count($tenantModels)); // 2
|
expect($getRlsTables())->toHaveCount(count($tenantModels)); // 2
|
||||||
|
|
||||||
foreach ($getRlsTables() as $table) {
|
foreach ($getRlsTables() as $table) {
|
||||||
|
|
@ -235,9 +233,7 @@ test('model discovery gets the models correctly', function() {
|
||||||
// Check that the Post and ScopedComment models are found in the directory
|
// Check that the Post and ScopedComment models are found in the directory
|
||||||
$expectedModels = [Post::class, ScopedComment::class];
|
$expectedModels = [Post::class, ScopedComment::class];
|
||||||
|
|
||||||
$foundModels = tenancy()->getModels()->where(function (Model $model) use ($expectedModels) {
|
$foundModels = array_filter(tenancy()->getModels(), fn (Model $model) => in_array($model::class, $expectedModels));
|
||||||
return in_array($model::class, $expectedModels);
|
|
||||||
});
|
|
||||||
|
|
||||||
expect($foundModels)->toHaveCount(count($expectedModels));
|
expect($foundModels)->toHaveCount(count($expectedModels));
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue