From e6a0717c8454aef47bac2c93cc36b8f069b768d9 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Mon, 13 Feb 2023 15:30:16 +0100 Subject: [PATCH] Add jobs for creating/deleting Postgres roles --- src/Jobs/CreatePostgresRoleForTenant.php | 45 +++++++++++++++++++++++ src/Jobs/DeleteTenantsPostgresRole.php | 46 ++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 src/Jobs/CreatePostgresRoleForTenant.php create mode 100644 src/Jobs/DeleteTenantsPostgresRole.php diff --git a/src/Jobs/CreatePostgresRoleForTenant.php b/src/Jobs/CreatePostgresRoleForTenant.php new file mode 100644 index 00000000..0f3e9dcb --- /dev/null +++ b/src/Jobs/CreatePostgresRoleForTenant.php @@ -0,0 +1,45 @@ +tenant->getTenantKey(); + $password = $this->tenant->database()->getPassword() ?? 'password'; + + try { + DB::statement("CREATE ROLE \"$name\" LOGIN PASSWORD '$password';"); + } catch (QueryException $exception) { + // Skip creating Postgres role if it already exists + } + } +} diff --git a/src/Jobs/DeleteTenantsPostgresRole.php b/src/Jobs/DeleteTenantsPostgresRole.php new file mode 100644 index 00000000..15151c9a --- /dev/null +++ b/src/Jobs/DeleteTenantsPostgresRole.php @@ -0,0 +1,46 @@ +tenant->getTenantKey(); + + // Revoke all permissions of a role before dropping it + try { + DB::statement("DROP OWNED BY \"{$tenantKey}\";"); + DB::statement("DROP ROLE \"{$tenantKey}\";"); + } catch (QueryException $exception) { + // Skip dropping permissions if the role doesn't exist + } + } +}