mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 14:14:03 +00:00
Update password defaulting logic
This commit is contained in:
parent
339f8e5778
commit
0fa52e5020
2 changed files with 17 additions and 5 deletions
|
|
@ -4,10 +4,11 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Stancl\Tenancy\Bootstrappers\Integrations;
|
namespace Stancl\Tenancy\Bootstrappers\Integrations;
|
||||||
|
|
||||||
use Illuminate\Contracts\Config\Repository;
|
use Closure;
|
||||||
use Illuminate\Database\DatabaseManager;
|
|
||||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
|
||||||
use Stancl\Tenancy\Contracts\Tenant;
|
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;
|
use Stancl\Tenancy\Database\Contracts\TenantWithDatabase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -23,6 +24,11 @@ class PostgresRLSBootstrapper implements TenancyBootstrapper
|
||||||
protected array $originalCentralConnectionConfig;
|
protected array $originalCentralConnectionConfig;
|
||||||
protected array $originalPostgresConfig;
|
protected array $originalPostgresConfig;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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,
|
||||||
|
|
@ -37,7 +43,7 @@ class PostgresRLSBootstrapper implements TenancyBootstrapper
|
||||||
|
|
||||||
/** @var TenantWithDatabase $tenant */
|
/** @var TenantWithDatabase $tenant */
|
||||||
$this->config->set('database.connections.pgsql.username', $tenant->database()->getUsername() ?? $tenant->getTenantKey());
|
$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(
|
$this->config->set(
|
||||||
'database.connections.' . $this->config->get('tenancy.database.central_connection'),
|
'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.' . $centralConnection, $this->originalCentralConnectionConfig);
|
||||||
$this->config->set('database.connections.pgsql', $this->originalPostgresConfig);
|
$this->config->set('database.connections.pgsql', $this->originalPostgresConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function getDefaultPassword(): string
|
||||||
|
{
|
||||||
|
return is_string(static::$defaultPassword) ? static::$defaultPassword : (static::$defaultPassword)();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ use Illuminate\Foundation\Bus\Dispatchable;
|
||||||
use Illuminate\Queue\InteractsWithQueue;
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
use Illuminate\Queue\SerializesModels;
|
use Illuminate\Queue\SerializesModels;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Stancl\Tenancy\Bootstrappers\Integrations\PostgresRLSBootstrapper;
|
||||||
use Stancl\Tenancy\Database\Contracts\TenantWithDatabase;
|
use Stancl\Tenancy\Database\Contracts\TenantWithDatabase;
|
||||||
|
|
||||||
class CreatePostgresUserForTenant implements ShouldQueue
|
class CreatePostgresUserForTenant implements ShouldQueue
|
||||||
|
|
@ -35,7 +36,7 @@ class CreatePostgresUserForTenant implements ShouldQueue
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
$name = $this->tenant->database()->getUsername() ?? $this->tenant->getTenantKey();
|
$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
|
// Create the user only if it doesn't already exist
|
||||||
if (! count(DB::select("SELECT usename FROM pg_user WHERE usename = '$name';")) > 0) {
|
if (! count(DB::select("SELECT usename FROM pg_user WHERE usename = '$name';")) > 0) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue