1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 07:54:03 +00:00
tenancy/src/Commands/CreateRLSPoliciesForTenantTables.php
2023-02-13 14:32:50 +00:00

34 lines
1 KiB
PHP

<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
class CreateRLSPoliciesForTenantTables extends Command
{
protected $signature = 'tenants:create-rls-policies';
public function handle(): int
{
foreach ($this->getTenantTables() as $table) {
DB::statement("DROP POLICY IF EXISTS {$table}_rls_policy ON {$table};");
DB::statement("CREATE POLICY {$table}_rls_policy ON {$table} USING (tenant_id::TEXT = current_user);");
DB::statement("ALTER TABLE {$table} FORCE ROW LEVEL SECURITY");
$this->components->info("Created RLS policy for table '$table'");
}
return Command::SUCCESS;
}
protected function getTenantTables(): array
{
return array_map(function (string $migration) {
return str($migration)->after('create_')->before('_table')->toString();
}, File::files('./database/migrations/tenant'));
}
}