1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 21:54:03 +00:00

[3.x] DB users (#382)

* Initial draft

* Apply fixes from StyleCI

* Use CI on master branch too

* Pass correct argument to queued DB creators/deleters

* Apply fixes from StyleCI

* Remove new interface from MySQLDBManager

* Make phpunit run

* Apply fixes from StyleCI

* Fix static property

* Default databaseName

* Use database transactions for creating users & granting permissions

* Apply fixes from StyleCI

* Get old tests to pass

* Apply fixes from StyleCI

* Add tests for PermissionControlledMySQLDatabaseManager

* Apply fixes from StyleCI

* Write test for extra config, fix bug with extra config

* Apply fixes from StyleCI
This commit is contained in:
Samuel Štancl 2020-05-03 18:12:27 +02:00 committed by GitHub
parent 60665517a0
commit 3bb2759fe2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
41 changed files with 756 additions and 286 deletions

View file

@ -8,18 +8,19 @@ use Closure;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\DatabaseManager as BaseDatabaseManager;
use Illuminate\Foundation\Application;
use Stancl\Tenancy\Contracts\Future\CanSetConnection;
use Stancl\Tenancy\Contracts\TenantCannotBeCreatedException;
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
use Stancl\Tenancy\Exceptions\DatabaseManagerNotRegisteredException;
use Stancl\Tenancy\Exceptions\TenantDatabaseAlreadyExistsException;
use Stancl\Tenancy\Jobs\QueuedTenantDatabaseCreator;
use Stancl\Tenancy\Jobs\QueuedTenantDatabaseDeleter;
/**
* @internal Class is subject to breaking changes in minor and patch versions.
*/
class DatabaseManager
{
/** @var string */
public $originalDefaultConnectionName;
public static $originalDefaultConnectionName;
/** @var Application */
protected $app;
@ -34,14 +35,11 @@ class DatabaseManager
{
$this->app = $app;
$this->database = $database;
$this->originalDefaultConnectionName = $app['config']['database.default'];
static::$originalDefaultConnectionName = $app['config']['database.default'];
}
/**
* Set the TenantManager instance, used to dispatch tenancy events.
*
* @param TenantManager $tenantManager
* @return self
*/
public function withTenantManager(TenantManager $tenantManager): self
{
@ -52,35 +50,28 @@ class DatabaseManager
/**
* Connect to a tenant's database.
*
* @param Tenant $tenant
* @return void
*/
public function connect(Tenant $tenant)
{
$this->createTenantConnection($tenant->getDatabaseName(), $tenant->getConnectionName());
$this->setDefaultConnection($tenant->getConnectionName());
$this->switchConnection($tenant->getConnectionName());
$this->createTenantConnection($tenant);
$this->setDefaultConnection('tenant');
$this->switchConnection('tenant');
}
/**
* Reconnect to the default non-tenant connection.
*
* @return void
*/
public function reconnect()
{
// Opposite order to connect() because we don't
// want to ever purge the central connection
$this->switchConnection($this->originalDefaultConnectionName);
$this->setDefaultConnection($this->originalDefaultConnectionName);
if ($this->tenancy->initialized) {
$this->database->purge('tenant');
}
$this->setDefaultConnection(static::$originalDefaultConnectionName);
$this->switchConnection(static::$originalDefaultConnectionName);
}
/**
* Change the default database connection config.
*
* @param string $connection
* @return void
*/
public function setDefaultConnection(string $connection)
{
@ -89,57 +80,17 @@ class DatabaseManager
/**
* Create the tenant database connection.
*
* @param string $databaseName
* @param string $connectionName
* @return void
*/
public function createTenantConnection($databaseName, $connectionName)
public function createTenantConnection(Tenant $tenant)
{
// Create the database connection.
$based_on = $this->getBaseConnection($connectionName);
$this->app['config']["database.connections.$connectionName"] = $this->app['config']['database.connections.' . $based_on];
// Change database name.
$databaseName = $this->getDriver($connectionName) === 'sqlite' ? database_path($databaseName) : $databaseName;
$separateBy = $this->separateBy($connectionName);
$this->app['config']["database.connections.$connectionName.$separateBy"] = $databaseName;
}
/**
* Get the name of the connection that $connectionName should be based on.
*
* @param string $connectionName
* @return string
*/
public function getBaseConnection(string $connectionName): string
{
return ($connectionName !== 'tenant' ? $connectionName : null) // 'tenant' is not a specific connection, it's the default
?? $this->app['config']['tenancy.database.based_on']
?? $this->originalDefaultConnectionName; // tenancy.database.based_on === null => use the default connection
}
/**
* Get the driver of a database connection.
*
* @param string $connectionName
* @return string|null
*/
public function getDriver(string $connectionName): ?string
{
return $this->app['config']["database.connections.$connectionName.driver"];
$this->app['config']['database.connections.tenant'] = $tenant->database()->connection();
}
/**
* Switch the application's connection.
*
* @param string $connection
* @return void
*/
public function switchConnection(string $connection)
{
$this->database->purge();
$this->database->reconnect($connection);
$this->database->setDefaultConnection($connection);
}
@ -147,15 +98,13 @@ class DatabaseManager
/**
* Check if a tenant can be created.
*
* @param Tenant $tenant
* @return void
* @throws TenantCannotBeCreatedException
* @throws DatabaseManagerNotRegisteredException
* @throws TenantDatabaseAlreadyExistsException
*/
public function ensureTenantCanBeCreated(Tenant $tenant): void
{
if ($this->getTenantDatabaseManager($tenant)->databaseExists($database = $tenant->getDatabaseName())) {
if ($tenant->database()->manager()->databaseExists($database = $tenant->database()->getName())) {
throw new TenantDatabaseAlreadyExistsException($database);
}
}
@ -170,101 +119,66 @@ class DatabaseManager
*/
public function createDatabase(Tenant $tenant, array $afterCreating = [])
{
$database = $tenant->getDatabaseName();
$manager = $this->getTenantDatabaseManager($tenant);
$afterCreating = array_merge(
$afterCreating,
$this->tenancy->event('database.creating', $database, $tenant)
$this->tenancy->event('database.creating', $tenant->database()->getName(), $tenant)
);
if ($this->app['config']['tenancy.queue_database_creation'] ?? false) {
$chain = [];
foreach ($afterCreating as $item) {
if (is_string($item) && class_exists($item)) {
$chain[] = new $item($tenant); // Classes are instantiated and given $tenant
} elseif ($item instanceof ShouldQueue) {
$chain[] = $item;
}
}
QueuedTenantDatabaseCreator::withChain($chain)->dispatch($manager, $database);
$this->createDatabaseAsynchronously($tenant, $afterCreating);
} else {
$manager->createDatabase($database);
foreach ($afterCreating as $item) {
if (is_object($item) && ! $item instanceof Closure) {
$item->handle($tenant);
} else {
$item($tenant);
}
$this->createDatabaseSynchronously($tenant, $afterCreating);
}
$this->tenancy->event('database.created', $tenant->database()->getName(), $tenant);
}
protected function createDatabaseAsynchronously(Tenant $tenant, array $afterCreating)
{
$chain = [];
foreach ($afterCreating as $item) {
if (is_string($item) && class_exists($item)) {
$chain[] = new $item($tenant); // Classes are instantiated and given $tenant
} elseif ($item instanceof ShouldQueue) {
$chain[] = $item;
}
}
$this->tenancy->event('database.created', $database, $tenant);
QueuedTenantDatabaseCreator::withChain($chain)->dispatch($tenant->database()->manager(), $tenant);
}
protected function createDatabaseSynchronously(Tenant $tenant, array $afterCreating)
{
$manager = $tenant->database()->manager();
$manager->createDatabase($tenant);
foreach ($afterCreating as $item) {
if (is_object($item) && ! $item instanceof Closure) {
$item->handle($tenant);
} else {
$item($tenant);
}
}
}
/**
* Delete a tenant's database.
*
* @param Tenant $tenant
* @return void
* @throws DatabaseManagerNotRegisteredException
*/
public function deleteDatabase(Tenant $tenant)
{
$database = $tenant->getDatabaseName();
$manager = $this->getTenantDatabaseManager($tenant);
$database = $tenant->database()->getName();
$manager = $tenant->database()->manager();
$this->tenancy->event('database.deleting', $database, $tenant);
if ($this->app['config']['tenancy.queue_database_deletion'] ?? false) {
QueuedTenantDatabaseDeleter::dispatch($manager, $database);
QueuedTenantDatabaseDeleter::dispatch($manager, $tenant);
} else {
$manager->deleteDatabase($database);
$manager->deleteDatabase($tenant);
}
$this->tenancy->event('database.deleted', $database, $tenant);
}
/**
* Get the TenantDatabaseManager for a tenant's database connection.
*
* @param Tenant $tenant
* @return TenantDatabaseManager
* @throws DatabaseManagerNotRegisteredException
*/
public function getTenantDatabaseManager(Tenant $tenant): TenantDatabaseManager
{
$driver = $this->getDriver($this->getBaseConnection($connectionName = $tenant->getConnectionName()));
$databaseManagers = $this->app['config']['tenancy.database_managers'];
if (! array_key_exists($driver, $databaseManagers)) {
throw new DatabaseManagerNotRegisteredException($driver);
}
$databaseManager = $this->app[$databaseManagers[$driver]];
if ($connectionName !== 'tenant' && $databaseManager instanceof CanSetConnection) {
$databaseManager->setConnection($connectionName);
}
return $databaseManager;
}
/**
* What key on the connection config should be used to separate tenants.
*
* @param string $connectionName
* @return string
*/
public function separateBy(string $connectionName): string
{
if ($this->getDriver($this->getBaseConnection($connectionName)) === 'pgsql'
&& $this->app['config']['tenancy.database.separate_by'] === 'schema') {
return 'schema';
}
return 'database';
}
}