1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-04 21:14:02 +00:00

Update the Postgres RLS bootstrapper

This commit is contained in:
lukinovec 2023-06-27 12:38:21 +02:00
parent 72b708e4c7
commit a568aa3626

View file

@ -34,21 +34,20 @@ class PostgresRLSBootstrapper implements TenancyBootstrapper
protected DatabaseManager $database, protected DatabaseManager $database,
) { ) {
$this->originalCentralConnectionConfig = $config->get('database.connections.central'); $this->originalCentralConnectionConfig = $config->get('database.connections.central');
$this->originalPostgresConfig = $config->get('database.connections.pgsql');
} }
public function bootstrap(Tenant $tenant): void public function bootstrap(Tenant $tenant): void
{ {
$this->database->purge($this->config->get('tenancy.database.central_connection')); $centralConnection = $this->config->get('tenancy.database.central_connection');
$this->database->purge($centralConnection);
/** @var TenantWithDatabase $tenant */ /** @var TenantWithDatabase $tenant */
$this->config->set('database.connections.pgsql.username', $tenant->database()->getUsername() ?? $tenant->getTenantKey());
$this->config->set('database.connections.pgsql.password', $tenant->database()->getPassword() ?? $this->getDefaultPassword());
$this->config->set( $this->config->set([
'database.connections.' . $this->config->get('tenancy.database.central_connection'), 'database.connections.' . $centralConnection . '.username' => $tenant->database()->getUsername() ?? $tenant->getTenantKey(),
$this->config->get('database.connections.pgsql') 'database.connections.' . $centralConnection . '.password' => $tenant->database()->getPassword() ?? $this->getDefaultPassword(),
); ]);
} }
public function revert(): void public function revert(): void
@ -57,8 +56,7 @@ class PostgresRLSBootstrapper implements TenancyBootstrapper
$this->database->purge($centralConnection); $this->database->purge($centralConnection);
$this->config->set('database.connections.' . $centralConnection, $this->originalCentralConnectionConfig); $this->config->set('database.connections.' . $centralConnection, $this->originalCentralConnectionConfig);;
$this->config->set('database.connections.pgsql', $this->originalPostgresConfig);
} }
public static function getDefaultPassword(): string public static function getDefaultPassword(): string