mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-04 19:24:02 +00:00
Get RLS models from a temporary tenant
This commit is contained in:
parent
7c7680baf6
commit
983e4c1f84
1 changed files with 29 additions and 4 deletions
|
|
@ -16,16 +16,26 @@ class CreateRLSPoliciesForTenantTables extends Command
|
|||
|
||||
public function handle(): int
|
||||
{
|
||||
foreach (config('tenancy.models.rls') as $modelClass) {
|
||||
/** @var Model $model */
|
||||
$model = new $modelClass;
|
||||
|
||||
foreach ($this->rlsModels() as $model) {
|
||||
DB::transaction(fn () => $this->makeModelUseRls($model));
|
||||
}
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
protected function rlsModels(): array
|
||||
{
|
||||
tenancy()->initialize($tenant = tenancy()->model()::create());
|
||||
|
||||
$rlsTables = array_map(fn ($table) => $table->tablename, Schema::getAllTables());
|
||||
|
||||
tenancy()->end();
|
||||
|
||||
$tenant->delete();
|
||||
|
||||
return array_filter(array_map(fn ($table) => $this->getModelFromTable($table), $rlsTables));
|
||||
}
|
||||
|
||||
protected function makeModelUseRls(Model $model): void
|
||||
{
|
||||
$table = $model->getTable();
|
||||
|
|
@ -82,4 +92,19 @@ class CreateRLSPoliciesForTenantTables extends Command
|
|||
DB::statement("ALTER TABLE {$table} ENABLE ROW LEVEL SECURITY");
|
||||
DB::statement("ALTER TABLE {$table} FORCE ROW LEVEL SECURITY");
|
||||
}
|
||||
|
||||
protected function getModelFromTable(string $table): Model|null
|
||||
{
|
||||
foreach(get_declared_classes() as $class) {
|
||||
if(is_subclass_of($class, Model::class)) {
|
||||
$model = new $class;
|
||||
|
||||
if ($model->getTable() === $table) {
|
||||
return $model;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue