mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 00:34:04 +00:00
Make Postgres tenant DB password default to central
This commit is contained in:
parent
90dc8d50be
commit
5a1beb1f3b
3 changed files with 28 additions and 19 deletions
|
|
@ -31,7 +31,7 @@
|
||||||
"league/flysystem-aws-s3-v3": "^3.12.2",
|
"league/flysystem-aws-s3-v3": "^3.12.2",
|
||||||
"doctrine/dbal": "^3.6.0",
|
"doctrine/dbal": "^3.6.0",
|
||||||
"spatie/valuestore": "^1.2.5",
|
"spatie/valuestore": "^1.2.5",
|
||||||
"pestphp/pest": "^1.21",
|
"pestphp/pest": "^2.25",
|
||||||
"nunomaduro/larastan": "^2.4",
|
"nunomaduro/larastan": "^2.4",
|
||||||
"spatie/invade": "^1.1"
|
"spatie/invade": "^1.1"
|
||||||
},
|
},
|
||||||
|
|
@ -60,16 +60,31 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"docker-up": "PHP_VERSION=8.2 docker-compose up -d",
|
"docker-up": [
|
||||||
"docker-down": "PHP_VERSION=8.2 docker-compose down",
|
"@putenv PHP_VERSION=8.2",
|
||||||
"docker-rebuild": "PHP_VERSION=8.2 docker-compose up -d --no-deps --build",
|
"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",
|
"docker-m1": "ln -s docker-compose-m1.override.yml docker-compose.override.yml",
|
||||||
"coverage": "open coverage/phpunit/html/index.html",
|
"coverage": "open coverage/phpunit/html/index.html",
|
||||||
"phpstan": "vendor/bin/phpstan",
|
"phpstan": "vendor/bin/phpstan",
|
||||||
"phpstan-pro": "vendor/bin/phpstan --pro",
|
"phpstan-pro": "vendor/bin/phpstan --pro",
|
||||||
"cs": "php-cs-fixer fix --config=.php-cs-fixer.php",
|
"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"
|
|
||||||
},
|
},
|
||||||
"minimum-stability": "dev",
|
"minimum-stability": "dev",
|
||||||
"prefer-stable": true,
|
"prefer-stable": true,
|
||||||
|
|
|
||||||
|
|
@ -23,11 +23,6 @@ class PostgresRLSBootstrapper implements TenancyBootstrapper
|
||||||
{
|
{
|
||||||
protected array $originalCentralConnectionConfig;
|
protected array $originalCentralConnectionConfig;
|
||||||
|
|
||||||
/**
|
|
||||||
* Must not return an empty string.
|
|
||||||
*/
|
|
||||||
public static string|Closure $defaultPassword = 'password';
|
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
protected Repository $config,
|
protected Repository $config,
|
||||||
protected DatabaseManager $database,
|
protected DatabaseManager $database,
|
||||||
|
|
@ -38,13 +33,14 @@ class PostgresRLSBootstrapper implements TenancyBootstrapper
|
||||||
public function bootstrap(Tenant $tenant): void
|
public function bootstrap(Tenant $tenant): void
|
||||||
{
|
{
|
||||||
$centralConnection = $this->config->get('tenancy.database.central_connection');
|
$centralConnection = $this->config->get('tenancy.database.central_connection');
|
||||||
|
$centralConnectionPassword = $this->config->get("database.connections.$centralConnection.password");
|
||||||
|
|
||||||
$this->database->purge($centralConnection);
|
$this->database->purge($centralConnection);
|
||||||
|
|
||||||
/** @var TenantWithDatabase $tenant */
|
/** @var TenantWithDatabase $tenant */
|
||||||
$this->config->set([
|
$this->config->set([
|
||||||
'database.connections.' . $centralConnection . '.username' => $tenant->database()->getUsername() ?? $tenant->getTenantKey(),
|
'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);
|
$this->config->set('database.connections.' . $centralConnection, $this->originalCentralConnectionConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getDefaultPassword(): string
|
|
||||||
{
|
|
||||||
return is_string(static::$defaultPassword) ? static::$defaultPassword : (static::$defaultPassword)();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,8 +35,11 @@ class CreatePostgresUserForTenant implements ShouldQueue
|
||||||
*/
|
*/
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
|
$centralConnection = config('tenancy.database.central_connection');
|
||||||
|
$centralConnectionPassword = config("database.connections.$centralConnection.password");
|
||||||
|
|
||||||
$name = $this->tenant->database()->getUsername() ?? $this->tenant->getTenantKey();
|
$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
|
// Create the user only if it doesn't already exist
|
||||||
if (! count(DB::select('SELECT usename FROM pg_user WHERE usename = $1', [$name])) > 0) {
|
if (! count(DB::select('SELECT usename FROM pg_user WHERE usename = $1', [$name])) > 0) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue