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

Update password defaulting logic

This commit is contained in:
lukinovec 2023-06-27 12:18:44 +02:00
parent 339f8e5778
commit 0fa52e5020
2 changed files with 17 additions and 5 deletions

View file

@ -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)();
}
}

View file

@ -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) {