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

Make Postgres tenant DB password default to central

This commit is contained in:
lukinovec 2023-11-22 18:07:58 +01:00
parent 90dc8d50be
commit 5a1beb1f3b
3 changed files with 28 additions and 19 deletions

View file

@ -31,7 +31,7 @@
"league/flysystem-aws-s3-v3": "^3.12.2",
"doctrine/dbal": "^3.6.0",
"spatie/valuestore": "^1.2.5",
"pestphp/pest": "^1.21",
"pestphp/pest": "^2.25",
"nunomaduro/larastan": "^2.4",
"spatie/invade": "^1.1"
},
@ -60,16 +60,31 @@
}
},
"scripts": {
"docker-up": "PHP_VERSION=8.2 docker-compose up -d",
"docker-down": "PHP_VERSION=8.2 docker-compose down",
"docker-rebuild": "PHP_VERSION=8.2 docker-compose up -d --no-deps --build",
"docker-up": [
"@putenv PHP_VERSION=8.2",
"docker-compose up -d"
],
"docker-down": [
"@putenv PHP_VERSION=8.2",
"docker-compose down"
],
"docker-rebuild": [
"@putenv PHP_VERSION=8.2",
"docker-compose up -d --no-deps --build"
],
"test": [
"@putenv PHP_VERSION=8.2",
"./test --no-coverage"
],
"test-full": [
"@putenv PHP_VERSION=8.2",
"./test"
],
"docker-m1": "ln -s docker-compose-m1.override.yml docker-compose.override.yml",
"coverage": "open coverage/phpunit/html/index.html",
"phpstan": "vendor/bin/phpstan",
"phpstan-pro": "vendor/bin/phpstan --pro",
"cs": "php-cs-fixer fix --config=.php-cs-fixer.php",
"test": "PHP_VERSION=8.2 ./test --no-coverage",
"test-full": "PHP_VERSION=8.2 ./test"
"cs": "php-cs-fixer fix --config=.php-cs-fixer.php"
},
"minimum-stability": "dev",
"prefer-stable": true,

View file

@ -23,11 +23,6 @@ class PostgresRLSBootstrapper implements TenancyBootstrapper
{
protected array $originalCentralConnectionConfig;
/**
* Must not return an empty string.
*/
public static string|Closure $defaultPassword = 'password';
public function __construct(
protected Repository $config,
protected DatabaseManager $database,
@ -38,13 +33,14 @@ class PostgresRLSBootstrapper implements TenancyBootstrapper
public function bootstrap(Tenant $tenant): void
{
$centralConnection = $this->config->get('tenancy.database.central_connection');
$centralConnectionPassword = $this->config->get("database.connections.$centralConnection.password");
$this->database->purge($centralConnection);
/** @var TenantWithDatabase $tenant */
$this->config->set([
'database.connections.' . $centralConnection . '.username' => $tenant->database()->getUsername() ?? $tenant->getTenantKey(),
'database.connections.' . $centralConnection . '.password' => $tenant->database()->getPassword() ?? $this->getDefaultPassword(),
'database.connections.' . $centralConnection . '.password' => $tenant->database()->getPassword() ?? $centralConnectionPassword,
]);
}
@ -56,9 +52,4 @@ class PostgresRLSBootstrapper implements TenancyBootstrapper
$this->config->set('database.connections.' . $centralConnection, $this->originalCentralConnectionConfig);
}
public static function getDefaultPassword(): string
{
return is_string(static::$defaultPassword) ? static::$defaultPassword : (static::$defaultPassword)();
}
}

View file

@ -35,8 +35,11 @@ class CreatePostgresUserForTenant implements ShouldQueue
*/
public function handle()
{
$centralConnection = config('tenancy.database.central_connection');
$centralConnectionPassword = config("database.connections.$centralConnection.password");
$name = $this->tenant->database()->getUsername() ?? $this->tenant->getTenantKey();
$password = $this->tenant->database()->getPassword() ?? PostgresRLSBootstrapper::getDefaultPassword();
$password = $this->tenant->database()->getPassword() ?? $centralConnectionPassword;
// Create the user only if it doesn't already exist
if (! count(DB::select('SELECT usename FROM pg_user WHERE usename = $1', [$name])) > 0) {