From 5469358e954d0c18bd580828acbc46737244a7c3 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Mon, 13 Feb 2023 15:32:13 +0100 Subject: [PATCH] Add command for creating RLS policies for tenant tables --- .../CreateRLSPoliciesForTenantTables.php | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/Commands/CreateRLSPoliciesForTenantTables.php diff --git a/src/Commands/CreateRLSPoliciesForTenantTables.php b/src/Commands/CreateRLSPoliciesForTenantTables.php new file mode 100644 index 00000000..4e3d5950 --- /dev/null +++ b/src/Commands/CreateRLSPoliciesForTenantTables.php @@ -0,0 +1,33 @@ +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')); + } +}