mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 15:34:04 +00:00
Test if policies get created correctly using action
This commit is contained in:
parent
338d6f1151
commit
562d507482
2 changed files with 87 additions and 4 deletions
71
src/Actions/CreateRLSPoliciesForTables.php
Normal file
71
src/Actions/CreateRLSPoliciesForTables.php
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Actions;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Stancl\Tenancy\Database\Concerns\BelongsToPrimaryModel;
|
||||
|
||||
class CreateRLSPoliciesForTables
|
||||
{
|
||||
// protected $signature = 'tenants:create-rls-policies';
|
||||
|
||||
public static function handle()
|
||||
{
|
||||
$tenantModels = static::getTenantModels();
|
||||
$tenantKey = config('tenancy.models.tenant_key_column');
|
||||
|
||||
foreach ($tenantModels as $model) {
|
||||
$table = $model->getTable();
|
||||
|
||||
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))) {
|
||||
$parentName = $model->getRelationshipToPrimaryModel();
|
||||
$parentKey = $model->$parentName()->getForeignKeyName();
|
||||
$parentModel = $model->$parentName()->make();
|
||||
$parentTable = str($parentModel->getTable())->toString();
|
||||
|
||||
DB::statement("CREATE POLICY {$table}_rls_policy ON {$table} USING (
|
||||
{$parentKey} IN (
|
||||
SELECT id
|
||||
FROM {$parentTable}
|
||||
WHERE ({$tenantKey}::TEXT = (
|
||||
SELECT {$tenantKey}
|
||||
FROM {$parentTable}
|
||||
WHERE id = {$parentKey}
|
||||
))
|
||||
)
|
||||
)");
|
||||
|
||||
DB::statement("ALTER TABLE {$table} FORCE ROW LEVEL SECURITY");
|
||||
|
||||
return Command::SUCCESS;
|
||||
} else {
|
||||
$modelName = $model::class;
|
||||
// $this->components->info("Table '$table' is not related to tenant. Make sure $modelName uses the BelongsToPrimaryModel trait.");
|
||||
|
||||
return Command::FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
DB::statement("CREATE POLICY {$table}_rls_policy ON {$table} USING ({$tenantKey}::TEXT = current_user);");
|
||||
|
||||
DB::statement("ALTER TABLE {$table} FORCE ROW LEVEL SECURITY");
|
||||
|
||||
// $this->components->info("Created RLS policy for table '$table'");
|
||||
}
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
public static function getTenantModels(): array
|
||||
{
|
||||
return array_map(fn (string $modelName) => (new $modelName), config('tenancy.models.rls'));
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue