mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 10:34:04 +00:00
Reduce nesting, rename methods
This commit is contained in:
parent
526002c7b2
commit
ae568e1227
1 changed files with 27 additions and 29 deletions
|
|
@ -16,41 +16,39 @@ class CreateRLSPoliciesForTenantTables extends Command
|
||||||
|
|
||||||
public function handle(): int
|
public function handle(): int
|
||||||
{
|
{
|
||||||
$tenantModels = $this->getTenantModels();
|
foreach (config('tenancy.models.rls') as $modelClass) {
|
||||||
$tenantKey = tenancy()->tenantKeyColumn();
|
$this->makeModelUseRls((new $modelClass));
|
||||||
|
|
||||||
foreach ($tenantModels as $model) {
|
|
||||||
$table = $model->getTable();
|
|
||||||
|
|
||||||
DB::transaction(fn () => DB::statement("DROP POLICY IF EXISTS {$table}_rls_policy ON {$table}"));
|
|
||||||
|
|
||||||
if (! Schema::hasColumn($table, $tenantKey)) {
|
|
||||||
// Table is not directly related to tenant
|
|
||||||
if (in_array(BelongsToPrimaryModel::class, class_uses_recursive($model::class))) {
|
|
||||||
$this->makeModelUseRls($model);
|
|
||||||
} else {
|
|
||||||
$modelName = $model::class;
|
|
||||||
|
|
||||||
$this->components->info("Table '$table' is not related to tenant. Make sure $modelName uses the BelongsToPrimaryModel trait.");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
DB::transaction(fn () => DB::statement("CREATE POLICY {$table}_rls_policy ON {$table} USING ({$tenantKey}::TEXT = current_user);"));
|
|
||||||
|
|
||||||
$this->makeTableUseRls($table);
|
|
||||||
|
|
||||||
$this->components->info("Created RLS policy for table '$table'");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Command::SUCCESS;
|
return Command::SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTenantModels(): array
|
protected function makeModelUseRls(Model $model): void
|
||||||
{
|
{
|
||||||
return array_map(fn (string $modelName) => (new $modelName), config('tenancy.models.rls'));
|
$table = $model->getTable();
|
||||||
|
$tenantKey = tenancy()->tenantKeyColumn();
|
||||||
|
|
||||||
|
DB::transaction(fn () => DB::statement("DROP POLICY IF EXISTS {$table}_rls_policy ON {$table}"));
|
||||||
|
|
||||||
|
if (! Schema::hasColumn($table, $tenantKey)) {
|
||||||
|
// Table is not directly related to tenant
|
||||||
|
if (in_array(BelongsToPrimaryModel::class, class_uses_recursive($model::class))) {
|
||||||
|
$this->makeSecondaryModelUseRls($model);
|
||||||
|
} else {
|
||||||
|
$modelName = $model::class;
|
||||||
|
|
||||||
|
$this->components->info("Table '$table' is not related to tenant. Make sure $modelName uses the BelongsToPrimaryModel trait.");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
DB::transaction(fn () => DB::statement("CREATE POLICY {$table}_rls_policy ON {$table} USING ({$tenantKey}::TEXT = current_user);"));
|
||||||
|
|
||||||
|
$this->enableRls($table);
|
||||||
|
|
||||||
|
$this->components->info("Created RLS policy for table '$table'");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function makeModelUseRls(Model $model): void
|
protected function makeSecondaryModelUseRls(Model $model): void
|
||||||
{
|
{
|
||||||
$table = $model->getTable();
|
$table = $model->getTable();
|
||||||
$tenantKey = tenancy()->tenantKeyColumn();
|
$tenantKey = tenancy()->tenantKeyColumn();
|
||||||
|
|
@ -72,10 +70,10 @@ class CreateRLSPoliciesForTenantTables extends Command
|
||||||
)
|
)
|
||||||
)"));
|
)"));
|
||||||
|
|
||||||
$this->makeTableUseRls($table);
|
$this->enableRls($table);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function makeTableUseRls(string $table): void
|
protected function enableRls(string $table): void
|
||||||
{
|
{
|
||||||
DB::transaction(function () use ($table) {
|
DB::transaction(function () use ($table) {
|
||||||
DB::statement("ALTER TABLE {$table} ENABLE ROW LEVEL SECURITY");
|
DB::statement("ALTER TABLE {$table} ENABLE ROW LEVEL SECURITY");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue