From 0fa52e502027743d9181d03a5031f88ac0dc6476 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Tue, 27 Jun 2023 12:18:44 +0200 Subject: [PATCH] Update password defaulting logic --- .../Integrations/PostgresRLSBootstrapper.php | 19 +++++++++++++++---- src/Jobs/CreatePostgresUserForTenant.php | 3 ++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/Bootstrappers/Integrations/PostgresRLSBootstrapper.php b/src/Bootstrappers/Integrations/PostgresRLSBootstrapper.php index 6f66b1d4..2d242bd8 100644 --- a/src/Bootstrappers/Integrations/PostgresRLSBootstrapper.php +++ b/src/Bootstrappers/Integrations/PostgresRLSBootstrapper.php @@ -4,10 +4,11 @@ declare(strict_types=1); namespace Stancl\Tenancy\Bootstrappers\Integrations; -use Illuminate\Contracts\Config\Repository; -use Illuminate\Database\DatabaseManager; -use Stancl\Tenancy\Contracts\TenancyBootstrapper; +use Closure; use Stancl\Tenancy\Contracts\Tenant; +use Illuminate\Database\DatabaseManager; +use Illuminate\Contracts\Config\Repository; +use Stancl\Tenancy\Contracts\TenancyBootstrapper; use Stancl\Tenancy\Database\Contracts\TenantWithDatabase; /** @@ -23,6 +24,11 @@ class PostgresRLSBootstrapper implements TenancyBootstrapper protected array $originalCentralConnectionConfig; protected array $originalPostgresConfig; + /** + * Must not return an empty string. + */ + public static string|Closure $defaultPassword = 'password'; + public function __construct( protected Repository $config, protected DatabaseManager $database, @@ -37,7 +43,7 @@ class PostgresRLSBootstrapper implements TenancyBootstrapper /** @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() ?? 'password'); + $this->config->set('database.connections.pgsql.password', $tenant->database()->getPassword() ?? $this->getDefaultPassword()); $this->config->set( 'database.connections.' . $this->config->get('tenancy.database.central_connection'), @@ -54,4 +60,9 @@ class PostgresRLSBootstrapper implements TenancyBootstrapper $this->config->set('database.connections.' . $centralConnection, $this->originalCentralConnectionConfig); $this->config->set('database.connections.pgsql', $this->originalPostgresConfig); } + + public static function getDefaultPassword(): string + { + return is_string(static::$defaultPassword) ? static::$defaultPassword : (static::$defaultPassword)(); + } } diff --git a/src/Jobs/CreatePostgresUserForTenant.php b/src/Jobs/CreatePostgresUserForTenant.php index 29102a43..683354db 100644 --- a/src/Jobs/CreatePostgresUserForTenant.php +++ b/src/Jobs/CreatePostgresUserForTenant.php @@ -11,6 +11,7 @@ use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\DB; +use Stancl\Tenancy\Bootstrappers\Integrations\PostgresRLSBootstrapper; use Stancl\Tenancy\Database\Contracts\TenantWithDatabase; class CreatePostgresUserForTenant implements ShouldQueue @@ -35,7 +36,7 @@ class CreatePostgresUserForTenant implements ShouldQueue public function handle() { $name = $this->tenant->database()->getUsername() ?? $this->tenant->getTenantKey(); - $password = $this->tenant->database()->getPassword() ?? ''; + $password = $this->tenant->database()->getPassword() ?? PostgresRLSBootstrapper::getDefaultPassword(); // Create the user only if it doesn't already exist if (! count(DB::select("SELECT usename FROM pg_user WHERE usename = '$name';")) > 0) {