mirror of
https://github.com/archtechx/tenancy.git
synced 2026-06-21 04:04:03 +00:00
Make hardening work with all db/schema managers
Previously, hardening only worked with databases, not with schemas. Also test that hardening works with all relevant db managers.
This commit is contained in:
parent
b743720c7c
commit
34d19e94e2
6 changed files with 76 additions and 20 deletions
|
|
@ -90,7 +90,9 @@ class DatabaseTenancyBootstrapper implements TenancyBootstrapper
|
|||
protected function verifyTenantCanUseDatabase(Tenant $tenant): void
|
||||
{
|
||||
/** @var \Stancl\Tenancy\Database\Models\Tenant&TenantWithDatabase $tenant */
|
||||
$tenantDbName = $tenant->database()->getName();
|
||||
|
||||
$tenantDbConfig = $tenant->database();
|
||||
$tenantDbName = $tenantDbConfig->getName();
|
||||
|
||||
// Check that no other tenant uses this tenant's database
|
||||
if ($tenant::where($tenant->getTenantKeyName(), '!=', $tenant->getTenantKey())
|
||||
|
|
@ -99,13 +101,14 @@ class DatabaseTenancyBootstrapper implements TenancyBootstrapper
|
|||
throw new RuntimeException('Tenant cannot use a database of another tenant.');
|
||||
}
|
||||
|
||||
$centralDbName = DB::connection(
|
||||
config('tenancy.database.central_connection', 'central')
|
||||
)->getDatabaseName();
|
||||
$manager = $tenantDbConfig->manager();
|
||||
|
||||
if (DB::getDatabaseName() === $centralDbName) {
|
||||
// Throw if the current database is central.
|
||||
// DB::getDatabaseName() is the current DB name, which should not be central at this point.
|
||||
$centralConnection = DB::connection(config('tenancy.database.central_connection', 'central'));
|
||||
$currentConnection = DB::connection();
|
||||
|
||||
// Throw if the current database/schema is central.
|
||||
// At this point the connection should be the tenant's, so it should not match central.
|
||||
if ($manager->getCurrentDatabaseName($currentConnection) === $manager->getCurrentDatabaseName($centralConnection)) {
|
||||
throw new RuntimeException('Tenant cannot use the central database.');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace Stancl\Tenancy\Database\Contracts;
|
||||
|
||||
use Illuminate\Database\Connection;
|
||||
|
||||
interface TenantDatabaseManager
|
||||
{
|
||||
/** Create a database. */
|
||||
|
|
@ -17,4 +19,12 @@ interface TenantDatabaseManager
|
|||
|
||||
/** Construct a DB connection config array. */
|
||||
public function makeConnectionConfig(array $baseConfig, string $databaseName): array;
|
||||
|
||||
/**
|
||||
* Get the schema/database name that the given connection points to.
|
||||
*
|
||||
* In database managers, this should return the *database* name of the passed connection,
|
||||
* while in schema managers, this should return the *schema* name of the passed connection.
|
||||
*/
|
||||
public function getCurrentDatabaseName(Connection $connection): string;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace Stancl\Tenancy\Database\TenantDatabaseManagers;
|
||||
|
||||
use Illuminate\Database\Connection;
|
||||
use Stancl\Tenancy\Database\Contracts\TenantWithDatabase;
|
||||
|
||||
class PostgreSQLSchemaManager extends TenantDatabaseManager
|
||||
|
|
@ -37,4 +38,10 @@ class PostgreSQLSchemaManager extends TenantDatabaseManager
|
|||
|
||||
return $baseConfig;
|
||||
}
|
||||
|
||||
public function getCurrentDatabaseName(Connection $connection): string
|
||||
{
|
||||
// Get the *schema* name (not the database name)
|
||||
return $connection->selectOne('SELECT current_schema() AS schema')->schema;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||
namespace Stancl\Tenancy\Database\TenantDatabaseManagers;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Database\Connection;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use InvalidArgumentException;
|
||||
use PDO;
|
||||
|
|
@ -149,6 +150,11 @@ class SQLiteDatabaseManager implements TenantDatabaseManager
|
|||
return $baseConfig;
|
||||
}
|
||||
|
||||
public function getCurrentDatabaseName(Connection $connection): string
|
||||
{
|
||||
return $connection->getDatabaseName();
|
||||
}
|
||||
|
||||
public function getPath(string $name): string
|
||||
{
|
||||
$this->validateDatabaseName($name);
|
||||
|
|
|
|||
|
|
@ -37,4 +37,9 @@ abstract class TenantDatabaseManager implements StatefulTenantDatabaseManager
|
|||
|
||||
return $baseConfig;
|
||||
}
|
||||
|
||||
public function getCurrentDatabaseName(Connection $connection): string
|
||||
{
|
||||
return $connection->getDatabaseName();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue