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

Merge branch 'master' of github.com:archtechx/tenancy

This commit is contained in:
Samuel Štancl 2023-02-21 00:40:40 +01:00
commit 858249759f
39 changed files with 1263 additions and 84 deletions

View file

@ -15,7 +15,11 @@ jobs:
strategy: strategy:
matrix: matrix:
laravel: ['^9.0'] include:
- laravel: 9
php: "8.0"
- laravel: 10
php: "8.1"
steps: steps:
- name: Checkout - name: Checkout
@ -23,7 +27,7 @@ jobs:
- name: Install Composer dependencies - name: Install Composer dependencies
run: | run: |
composer require "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update composer require "laravel/framework:^${{ matrix.laravel }}.0" --no-interaction --no-update
composer update --prefer-dist --no-interaction composer update --prefer-dist --no-interaction
- name: Run tests - name: Run tests
run: ./vendor/bin/pest run: ./vendor/bin/pest

View file

@ -15,7 +15,7 @@ RUN apt-get update \
&& curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - \ && curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - \
&& curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list > /etc/apt/sources.list.d/mssql-release.list \ && curl https://packages.microsoft.com/config/ubuntu/20.04/prod.list > /etc/apt/sources.list.d/mssql-release.list \
&& apt-get update \ && apt-get update \
&& ACCEPT_EULA=Y apt-get install -y unixodbc-dev msodbcsql17 && ACCEPT_EULA=Y apt-get install -y unixodbc-dev=2.3.7 unixodbc=2.3.7 odbcinst1debian2=2.3.7 odbcinst=2.3.7 msodbcsql17
# set PHP version # set PHP version
RUN update-alternatives --set php /usr/bin/php$PHP_VERSION \ RUN update-alternatives --set php /usr/bin/php$PHP_VERSION \

View file

@ -4,14 +4,14 @@ declare(strict_types=1);
namespace App\Providers; namespace App\Providers;
use Stancl\Tenancy\Jobs;
use Stancl\Tenancy\Events;
use Stancl\Tenancy\Listeners;
use Stancl\Tenancy\Middleware;
use Stancl\JobPipeline\JobPipeline;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Stancl\JobPipeline\JobPipeline;
use Stancl\Tenancy\Events;
use Stancl\Tenancy\Jobs;
use Stancl\Tenancy\Listeners;
use Stancl\Tenancy\Middleware;
class TenancyServiceProvider extends ServiceProvider class TenancyServiceProvider extends ServiceProvider
{ {
@ -118,6 +118,21 @@ class TenancyServiceProvider extends ServiceProvider
]; ];
} }
protected function overrideUrlInTenantContext(): void
{
/**
* Example of CLI tenant URL root override:
*
* UrlTenancyBootstrapper::$rootUrlOverride = function (Tenant $tenant) {
* $baseUrl = url('/');
* $scheme = str($baseUrl)->before('://');
* $hostname = str($baseUrl)->after($scheme . '://');
*
* return $scheme . '://' . $tenant->getTenantKey() . '.' . $hostname;
*};
*/
}
public function register() public function register()
{ {
// //
@ -129,6 +144,7 @@ class TenancyServiceProvider extends ServiceProvider
$this->mapRoutes(); $this->mapRoutes();
$this->makeTenancyMiddlewareHighestPriority(); $this->makeTenancyMiddlewareHighestPriority();
$this->overrideUrlInTenantContext();
} }
protected function bootEvents() protected function bootEvents()

View file

@ -102,6 +102,7 @@ return [
Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper::class, Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper::class,
Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper::class, Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper::class,
Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper::class, Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper::class,
// Stancl\Tenancy\Bootstrappers\UrlTenancyBootstrapper::class,
// Stancl\Tenancy\Bootstrappers\SessionTenancyBootstrapper::class, // Stancl\Tenancy\Bootstrappers\SessionTenancyBootstrapper::class,
// Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper::class, // Queueing mail requires using QueueTenancyBootstrapper with $forceRefresh set to true // Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper::class, // Queueing mail requires using QueueTenancyBootstrapper with $forceRefresh set to true
// Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class, // Note: phpredis is needed // Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class, // Note: phpredis is needed
@ -257,7 +258,7 @@ return [
], ],
/** /**
* Redis tenancy config. Used by RedisTenancyBoostrapper. * Redis tenancy config. Used by RedisTenancyBootstrapper.
* *
* Note: You need phpredis to use Redis tenancy. * Note: You need phpredis to use Redis tenancy.
* *
@ -285,6 +286,7 @@ return [
// Stancl\Tenancy\Features\TelescopeTags::class, // Stancl\Tenancy\Features\TelescopeTags::class,
// Stancl\Tenancy\Features\TenantConfig::class, // https://tenancyforlaravel.com/docs/v3/features/tenant-config // Stancl\Tenancy\Features\TenantConfig::class, // https://tenancyforlaravel.com/docs/v3/features/tenant-config
// Stancl\Tenancy\Features\CrossDomainRedirect::class, // https://tenancyforlaravel.com/docs/v3/features/cross-domain-redirect // Stancl\Tenancy\Features\CrossDomainRedirect::class, // https://tenancyforlaravel.com/docs/v3/features/cross-domain-redirect
// Stancl\Tenancy\Features\ViteBundler::class,
], ],
/** /**

View file

@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('tenant_resources', function (Blueprint $table) {
$table->increments('id');
$table->string('tenant_id');
$table->string('resource_global_id');
$table->string('tenant_resources_type');
});
}
public function down(): void
{
Schema::dropIfExists('tenant_resources');
}
};

View file

@ -17,17 +17,19 @@
"require": { "require": {
"php": "^8.2", "php": "^8.2",
"ext-json": "*", "ext-json": "*",
"illuminate/support": "^9.38", "illuminate/support": "^9.38|^10.0",
"facade/ignition-contracts": "^1.0.2",
"spatie/ignition": "^1.4", "spatie/ignition": "^1.4",
"ramsey/uuid": "^4.0", "ramsey/uuid": "^4.7.3",
"stancl/jobpipeline": "^1.0", "stancl/jobpipeline": "^1.6.2",
"stancl/virtualcolumn": "^1.3" "stancl/virtualcolumn": "^1.3.1",
"spatie/invade": "^1.1"
}, },
"require-dev": { "require-dev": {
"laravel/framework": "^9.38", "laravel/framework": "^9.38|^10.0",
"orchestra/testbench": "^7.0", "orchestra/testbench": "^7.0|^8.0",
"league/flysystem-aws-s3-v3": "^3.0", "league/flysystem-aws-s3-v3": "^3.12.2",
"doctrine/dbal": "^2.10", "doctrine/dbal": "^3.6.0",
"spatie/valuestore": "^1.2.5", "spatie/valuestore": "^1.2.5",
"pestphp/pest": "^1.21", "pestphp/pest": "^1.21",
"nunomaduro/larastan": "^2.4", "nunomaduro/larastan": "^2.4",

View file

@ -40,10 +40,6 @@ parameters:
message: '#Illuminate\\Routing\\UrlGenerator#' message: '#Illuminate\\Routing\\UrlGenerator#'
paths: paths:
- src/Bootstrappers/FilesystemTenancyBootstrapper.php - src/Bootstrappers/FilesystemTenancyBootstrapper.php
-
message: '#select\(\) expects string, Illuminate\\Database\\Query\\Expression given#'
paths:
- src/Database/TenantDatabaseManagers/PermissionControlledMySQLDatabaseManager.php
- -
message: '#Trying to invoke Closure\|null but it might not be a callable#' message: '#Trying to invoke Closure\|null but it might not be a callable#'
paths: paths:

View file

@ -0,0 +1,95 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Bootstrappers;
use Illuminate\Broadcasting\BroadcastManager;
use Illuminate\Config\Repository;
use Illuminate\Contracts\Broadcasting\Broadcaster;
use Illuminate\Foundation\Application;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\TenancyBroadcastManager;
class BroadcastTenancyBootstrapper implements TenancyBootstrapper
{
/**
* Tenant properties to be mapped to config (similarly to the TenantConfig feature).
*
* For example:
* [
* 'config.key.name' => 'tenant_property',
* ]
*/
public static array $credentialsMap = [];
public static string|null $broadcaster = null;
protected array $originalConfig = [];
protected BroadcastManager|null $originalBroadcastManager = null;
protected Broadcaster|null $originalBroadcaster = null;
public static array $mapPresets = [
'pusher' => [
'broadcasting.connections.pusher.key' => 'pusher_key',
'broadcasting.connections.pusher.secret' => 'pusher_secret',
'broadcasting.connections.pusher.app_id' => 'pusher_app_id',
'broadcasting.connections.pusher.options.cluster' => 'pusher_cluster',
],
'ably' => [
'broadcasting.connections.ably.key' => 'ably_key',
'broadcasting.connections.ably.public' => 'ably_public',
],
];
public function __construct(
protected Repository $config,
protected Application $app
) {
static::$broadcaster ??= $config->get('broadcasting.default');
static::$credentialsMap = array_merge(static::$credentialsMap, static::$mapPresets[static::$broadcaster] ?? []);
}
public function bootstrap(Tenant $tenant): void
{
$this->originalBroadcastManager = $this->app->make(BroadcastManager::class);
$this->originalBroadcaster = $this->app->make(Broadcaster::class);
$this->setConfig($tenant);
// Make BroadcastManager resolve to a custom BroadcastManager which makes the broadcasters use the tenant credentials
$this->app->extend(BroadcastManager::class, function (BroadcastManager $broadcastManager) {
return new TenancyBroadcastManager($this->app);
});
}
public function revert(): void
{
// Change the BroadcastManager and Broadcaster singletons back to what they were before initializing tenancy
$this->app->singleton(BroadcastManager::class, fn (Application $app) => $this->originalBroadcastManager);
$this->app->singleton(Broadcaster::class, fn (Application $app) => $this->originalBroadcaster);
$this->unsetConfig();
}
protected function setConfig(Tenant $tenant): void
{
foreach (static::$credentialsMap as $configKey => $storageKey) {
$override = $tenant->$storageKey;
if (array_key_exists($storageKey, $tenant->getAttributes())) {
$this->originalConfig[$configKey] ??= $this->config->get($configKey);
$this->config->set($configKey, $override);
}
}
}
protected function unsetConfig(): void
{
foreach ($this->originalConfig as $key => $value) {
$this->config->set($key, $value);
}
}
}

View file

@ -25,7 +25,7 @@ class DatabaseTenancyBootstrapper implements TenancyBootstrapper
/** @var TenantWithDatabase $tenant */ /** @var TenantWithDatabase $tenant */
// Better debugging, but breaks cached lookup in prod // Better debugging, but breaks cached lookup in prod
if (app()->environment('local')) { if (app()->environment('local') || app()->environment('testing')) { // todo@docs mention this change in v4 upgrade guide https://github.com/archtechx/tenancy/pull/945#issuecomment-1268206149
$database = $tenant->database()->getName(); $database = $tenant->database()->getName();
if (! $tenant->database()->manager()->databaseExists($database)) { if (! $tenant->database()->manager()->databaseExists($database)) {
throw new TenantDatabaseDoesNotExistException($database); throw new TenantDatabaseDoesNotExistException($database);

View file

@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Bootstrappers;
use Closure;
use Illuminate\Config\Repository;
use Illuminate\Contracts\Routing\UrlGenerator;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant;
class UrlTenancyBootstrapper implements TenancyBootstrapper
{
public static Closure|null $rootUrlOverride = null;
protected string|null $originalRootUrl = null;
public function __construct(
protected UrlGenerator $urlGenerator,
protected Repository $config,
) {
}
public function bootstrap(Tenant $tenant): void
{
$this->originalRootUrl = $this->urlGenerator->to('/');
if (static::$rootUrlOverride) {
$newRootUrl = (static::$rootUrlOverride)($tenant);
$this->urlGenerator->forceRootUrl($newRootUrl);
$this->config->set('app.url', $newRootUrl);
}
}
public function revert(): void
{
$this->urlGenerator->forceRootUrl($this->originalRootUrl);
$this->config->set('app.url', $this->originalRootUrl);
}
}

View file

@ -7,9 +7,11 @@ namespace Stancl\Tenancy\Commands;
use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Console\Migrations\MigrateCommand; use Illuminate\Database\Console\Migrations\MigrateCommand;
use Illuminate\Database\Migrations\Migrator; use Illuminate\Database\Migrations\Migrator;
use Illuminate\Database\QueryException;
use Stancl\Tenancy\Concerns\DealsWithMigrations; use Stancl\Tenancy\Concerns\DealsWithMigrations;
use Stancl\Tenancy\Concerns\ExtendsLaravelCommand; use Stancl\Tenancy\Concerns\ExtendsLaravelCommand;
use Stancl\Tenancy\Concerns\HasTenantOptions; use Stancl\Tenancy\Concerns\HasTenantOptions;
use Stancl\Tenancy\Database\Exceptions\TenantDatabaseDoesNotExistException;
use Stancl\Tenancy\Events\DatabaseMigrated; use Stancl\Tenancy\Events\DatabaseMigrated;
use Stancl\Tenancy\Events\MigratingDatabase; use Stancl\Tenancy\Events\MigratingDatabase;
@ -28,6 +30,8 @@ class Migrate extends MigrateCommand
{ {
parent::__construct($migrator, $dispatcher); parent::__construct($migrator, $dispatcher);
$this->addOption('skip-failing');
$this->specifyParameters(); $this->specifyParameters();
} }
@ -43,16 +47,23 @@ class Migrate extends MigrateCommand
return 1; return 1;
} }
tenancy()->runForMultiple($this->getTenants(), function ($tenant) { foreach ($this->getTenants() as $tenant) {
$this->components->info("Tenant: {$tenant->getTenantKey()}"); try {
$tenant->run(function ($tenant) {
$this->line("Tenant: {$tenant->getTenantKey()}");
event(new MigratingDatabase($tenant)); event(new MigratingDatabase($tenant));
// Migrate
parent::handle();
// Migrate event(new DatabaseMigrated($tenant));
parent::handle(); });
} catch (TenantDatabaseDoesNotExistException|QueryException $th) {
event(new DatabaseMigrated($tenant)); if (! $this->option('skip-failing')) {
}); throw $th;
}
}
}
return 0; return 0;
} }

View file

@ -4,8 +4,10 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Database\Concerns; namespace Stancl\Tenancy\Database\Concerns;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Stancl\Tenancy\Contracts\Syncable; use Stancl\Tenancy\Contracts\Syncable;
use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator; use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
use Stancl\Tenancy\Database\Models\TenantMorphPivot;
use Stancl\Tenancy\Events\SyncedResourceSaved; use Stancl\Tenancy\Events\SyncedResourceSaved;
trait ResourceSyncing trait ResourceSyncing
@ -43,4 +45,10 @@ trait ResourceSyncing
{ {
return true; return true;
} }
public function tenants(): MorphToMany
{
return $this->morphToMany(config('tenancy.models.tenant'), 'tenant_resources', 'tenant_resources', 'resource_global_id', 'tenant_id', 'global_id')
->using(TenantMorphPivot::class);
}
} }

View file

@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Database\Concerns;
use Stancl\Tenancy\Contracts\Syncable;
trait TriggerSyncEvent
{
public static function booted(): void
{
static::saved(function (self $pivot) {
$parent = $pivot->pivotParent;
if ($parent instanceof Syncable && $parent->shouldSync()) {
$parent->triggerSyncEvent();
}
});
}
}

View file

@ -87,7 +87,7 @@ class DatabaseConfig
{ {
$this->tenant->setInternal('db_name', $this->getName()); $this->tenant->setInternal('db_name', $this->getName());
if ($this->connectionDriverManager($this->getTemplateConnectionName()) instanceof Contracts\ManagesDatabaseUsers) { if ($this->connectionDriverManager($this->getTemplateConnectionDriver()) instanceof Contracts\ManagesDatabaseUsers) {
$this->tenant->setInternal('db_username', $this->getUsername() ?? (static::$usernameGenerator)($this->tenant)); $this->tenant->setInternal('db_username', $this->getUsername() ?? (static::$usernameGenerator)($this->tenant));
$this->tenant->setInternal('db_password', $this->getPassword() ?? (static::$passwordGenerator)($this->tenant)); $this->tenant->setInternal('db_password', $this->getPassword() ?? (static::$passwordGenerator)($this->tenant));
} }
@ -97,11 +97,29 @@ class DatabaseConfig
} }
} }
public function getTemplateConnectionName(): string public function getTemplateConnectionDriver(): string
{ {
return $this->tenant->getInternal('db_connection') return $this->getTemplateConnection()['driver'];
?? config('tenancy.database.template_tenant_connection') }
?? config('tenancy.database.central_connection');
public function getTemplateConnection(): array
{
if ($template = $this->tenant->getInternal('db_connection')) {
return config("database.connections.{$template}");
}
if ($template = config('tenancy.database.template_tenant_connection')) {
return is_array($template) ? array_merge($this->getCentralConnection(), $template) : config("database.connections.{$template}");
}
return $this->getCentralConnection();
}
protected function getCentralConnection(): array
{
$centralConnectionName = config('tenancy.database.central_connection');
return config("database.connections.{$centralConnectionName}");
} }
public function getTenantHostConnectionName(): string public function getTenantHostConnectionName(): string
@ -114,8 +132,7 @@ class DatabaseConfig
*/ */
public function connection(): array public function connection(): array
{ {
$template = $this->getTemplateConnectionName(); $templateConnection = $this->getTemplateConnection();
$templateConnection = config("database.connections.{$template}");
return $this->manager()->makeConnectionConfig( return $this->manager()->makeConnectionConfig(
array_merge($templateConnection, $this->tenantConfig()), array_merge($templateConnection, $this->tenantConfig()),
@ -129,10 +146,9 @@ class DatabaseConfig
public function hostConnection(): array public function hostConnection(): array
{ {
$config = $this->tenantConfig(); $config = $this->tenantConfig();
$template = $this->getTemplateConnectionName(); $templateConnection = $this->getTemplateConnection();
$templateConnection = config("database.connections.{$template}");
if ($this->connectionDriverManager($template) instanceof Contracts\ManagesDatabaseUsers) { if ($this->connectionDriverManager($this->getTemplateConnectionDriver()) instanceof Contracts\ManagesDatabaseUsers) {
// We're removing the username and password because user with these credentials is not created yet // We're removing the username and password because user with these credentials is not created yet
// If you need to provide username and password when using PermissionControlledMySQLDatabaseManager, // If you need to provide username and password when using PermissionControlledMySQLDatabaseManager,
// consider creating a new connection and use it as `tenancy_db_connection` tenant config key // consider creating a new connection and use it as `tenancy_db_connection` tenant config key
@ -196,7 +212,7 @@ class DatabaseConfig
$tenantHostConnectionName = $this->getTenantHostConnectionName(); $tenantHostConnectionName = $this->getTenantHostConnectionName();
config(["database.connections.{$tenantHostConnectionName}" => $this->hostConnection()]); config(["database.connections.{$tenantHostConnectionName}" => $this->hostConnection()]);
$manager = $this->connectionDriverManager($tenantHostConnectionName); $manager = $this->connectionDriverManager(config("database.connections.{$tenantHostConnectionName}.driver"));
if ($manager instanceof Contracts\StatefulTenantDatabaseManager) { if ($manager instanceof Contracts\StatefulTenantDatabaseManager) {
$manager->setConnection($tenantHostConnectionName); $manager->setConnection($tenantHostConnectionName);
@ -211,10 +227,8 @@ class DatabaseConfig
* *
* @throws DatabaseManagerNotRegisteredException * @throws DatabaseManagerNotRegisteredException
*/ */
protected function connectionDriverManager(string $connectionName): Contracts\TenantDatabaseManager protected function connectionDriverManager(string $driver): Contracts\TenantDatabaseManager
{ {
$driver = config("database.connections.{$connectionName}.driver");
$databaseManagers = config('tenancy.database.managers'); $databaseManagers = config('tenancy.database.managers');
if (! array_key_exists($driver, $databaseManagers)) { if (! array_key_exists($driver, $databaseManagers)) {

View file

@ -33,9 +33,8 @@ class ImpersonationToken extends Model
public $incrementing = false; public $incrementing = false;
protected $table = 'tenant_user_impersonation_tokens'; protected $table = 'tenant_user_impersonation_tokens';
protected $casts = [
protected $dates = [ 'created_at' => 'datetime',
'created_at',
]; ];
public static function booted(): void public static function booted(): void

View file

@ -32,6 +32,8 @@ class Tenant extends Model implements Contracts\Tenant
Concerns\InitializationHelpers, Concerns\InitializationHelpers,
Concerns\InvalidatesResolverCache; Concerns\InvalidatesResolverCache;
protected static $modelsShouldPreventAccessingMissingAttributes = false;
protected $table = 'tenants'; protected $table = 'tenants';
protected $primaryKey = 'id'; protected $primaryKey = 'id';
protected $guarded = []; protected $guarded = [];

View file

@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Database\Models;
use Illuminate\Database\Eloquent\Relations\MorphPivot;
use Stancl\Tenancy\Database\Concerns\TriggerSyncEvent;
class TenantMorphPivot extends MorphPivot
{
use TriggerSyncEvent;
}

View file

@ -5,18 +5,9 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Database\Models; namespace Stancl\Tenancy\Database\Models;
use Illuminate\Database\Eloquent\Relations\Pivot; use Illuminate\Database\Eloquent\Relations\Pivot;
use Stancl\Tenancy\Contracts\Syncable; use Stancl\Tenancy\Database\Concerns\TriggerSyncEvent;
class TenantPivot extends Pivot class TenantPivot extends Pivot
{ {
public static function booted(): void use TriggerSyncEvent;
{
static::saved(function (self $pivot) {
$parent = $pivot->pivotParent;
if ($parent instanceof Syncable && $parent->shouldSync()) {
$parent->triggerSyncEvent();
}
});
}
} }

View file

@ -41,7 +41,8 @@ class PermissionControlledMySQLDatabaseManager extends MySQLDatabaseManager impl
protected function isVersion8(): bool protected function isVersion8(): bool
{ {
$version = $this->database()->select($this->database()->raw('select version()'))[0]->{'version()'}; $versionSelect = (string) $this->database()->raw('select version()')->getValue($this->database()->getQueryGrammar());
$version = $this->database()->select($versionSelect)[0]->{'version()'};
return version_compare($version, '8.0.0') >= 0; return version_compare($version, '8.0.0') >= 0;
} }

View file

@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Features;
use Illuminate\Foundation\Application;
use Stancl\Tenancy\Contracts\Feature;
use Stancl\Tenancy\Tenancy;
use Stancl\Tenancy\Vite;
class ViteBundler implements Feature
{
/** @var Application */
protected $app;
public function __construct(Application $app)
{
$this->app = $app;
}
public function bootstrap(Tenancy $tenancy): void
{
$this->app->singleton(\Illuminate\Foundation\Vite::class, Vite::class);
}
}

View file

@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy; // todo new Overrides namespace?
use Illuminate\Broadcasting\Broadcasters\Broadcaster;
use Illuminate\Broadcasting\BroadcastManager;
use Illuminate\Contracts\Broadcasting\Broadcaster as BroadcasterContract;
use Illuminate\Contracts\Foundation\Application;
class TenancyBroadcastManager extends BroadcastManager
{
/**
* Names of broadcasters to always recreate using $this->resolve() (even when they're
* cached and available in the $broadcasters property).
*
* The reason for recreating the broadcasters is
* to make your app use the correct broadcaster credentials when tenancy is initialized.
*/
public static array $tenantBroadcasters = ['pusher', 'ably'];
/**
* Override the get method so that the broadcasters in $tenantBroadcasters
* always get freshly resolved even when they're cached and available in the $broadcasters property,
* and that the resolved broadcaster will override the BroadcasterContract::class singleton.
*
* If there's a cached broadcaster with the same name as $name,
* give its channels to the newly resolved bootstrapper.
*/
protected function get($name)
{
if (in_array($name, static::$tenantBroadcasters)) {
/** @var Broadcaster|null $originalBroadcaster */
$originalBroadcaster = $this->app->make(BroadcasterContract::class);
$newBroadcaster = $this->resolve($name);
// If there is a current broadcaster, give its channels to the newly resolved one
// Broadcasters only have to implement the Illuminate\Contracts\Broadcasting\Broadcaster contract
// Which doesn't require the channels property
// So passing the channels is only needed for Illuminate\Broadcasting\Broadcasters\Broadcaster instances
if ($originalBroadcaster instanceof Broadcaster && $newBroadcaster instanceof Broadcaster) {
$this->passChannelsFromOriginalBroadcaster($originalBroadcaster, $newBroadcaster);
}
$this->app->singleton(BroadcasterContract::class, fn (Application $app) => $newBroadcaster);
return $newBroadcaster;
}
return parent::get($name);
}
// Because, unlike the original broadcaster, the newly resolved broadcaster won't have the channels registered using routes/channels.php
// Using it for broadcasting won't work, unless we make it have the original broadcaster's channels
protected function passChannelsFromOriginalBroadcaster(Broadcaster $originalBroadcaster, Broadcaster $newBroadcaster): void
{
// invade() because channels can't be retrieved through any of the broadcaster's public methods
$originalBroadcaster = invade($originalBroadcaster);
foreach ($originalBroadcaster->channels as $channel => $callback) {
$newBroadcaster->channel($channel, $callback, $originalBroadcaster->retrieveChannelOptions($channel));
}
}
}

View file

@ -62,6 +62,7 @@ class TenancyServiceProvider extends ServiceProvider
$this->app->singleton(Commands\Rollback::class, function ($app) { $this->app->singleton(Commands\Rollback::class, function ($app) {
return new Commands\Rollback($app['migrator']); return new Commands\Rollback($app['migrator']);
}); });
$this->app->singleton(Commands\Seed::class, function ($app) { $this->app->singleton(Commands\Seed::class, function ($app) {
return new Commands\Seed($app['db']); return new Commands\Seed($app['db']);
}); });
@ -106,6 +107,10 @@ class TenancyServiceProvider extends ServiceProvider
__DIR__ . '/../assets/impersonation-migrations/' => database_path('migrations'), __DIR__ . '/../assets/impersonation-migrations/' => database_path('migrations'),
], 'impersonation-migrations'); ], 'impersonation-migrations');
$this->publishes([
__DIR__ . '/../assets/resource-syncing-migrations/' => database_path('migrations'),
], 'resource-syncing-migrations');
$this->publishes([ $this->publishes([
__DIR__ . '/../assets/tenant_routes.stub.php' => base_path('routes/tenant.php'), __DIR__ . '/../assets/tenant_routes.stub.php' => base_path('routes/tenant.php'),
], 'routes'); ], 'routes');

22
src/Vite.php Normal file
View file

@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy;
use Illuminate\Foundation\Vite as BaseVite;
class Vite extends BaseVite // todo move to a different directory in v4
{
/**
* Generate an asset path for the application.
*
* @param string $path
* @param bool|null $secure
* @return string
*/
protected function assetPath($path, $secure = null)
{
return global_asset($path);
}
}

View file

@ -50,6 +50,8 @@ test('context is switched when tenancy is reinitialized', function () {
}); });
test('central helper runs callbacks in the central state', function () { test('central helper runs callbacks in the central state', function () {
withTenantDatabases();
tenancy()->initialize($tenant = Tenant::create()); tenancy()->initialize($tenant = Tenant::create());
tenancy()->central(function () { tenancy()->central(function () {
@ -60,6 +62,8 @@ test('central helper runs callbacks in the central state', function () {
}); });
test('central helper returns the value from the callback', function () { test('central helper returns the value from the callback', function () {
withTenantDatabases();
tenancy()->initialize(Tenant::create()); tenancy()->initialize(Tenant::create());
pest()->assertSame('foo', tenancy()->central(function () { pest()->assertSame('foo', tenancy()->central(function () {
@ -68,6 +72,8 @@ test('central helper returns the value from the callback', function () {
}); });
test('central helper reverts back to tenant context', function () { test('central helper reverts back to tenant context', function () {
withTenantDatabases();
tenancy()->initialize($tenant = Tenant::create()); tenancy()->initialize($tenant = Tenant::create());
tenancy()->central(function () { tenancy()->central(function () {

View file

@ -23,6 +23,8 @@ beforeEach(function () {
}); });
test('batch repository is set to tenant connection and reverted', function () { test('batch repository is set to tenant connection and reverted', function () {
withTenantDatabases();
$tenant = Tenant::create(); $tenant = Tenant::create();
$tenant2 = Tenant::create(); $tenant2 = Tenant::create();

View file

@ -3,31 +3,38 @@
declare(strict_types=1); declare(strict_types=1);
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Illuminate\Mail\MailManager;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\URL;
use Stancl\JobPipeline\JobPipeline; use Stancl\JobPipeline\JobPipeline;
use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\File;
use Stancl\Tenancy\Tests\Etc\Tenant; use Stancl\Tenancy\Tests\Etc\Tenant;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Redis; use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Stancl\Tenancy\Events\TenancyEnded; use Stancl\Tenancy\Events\TenancyEnded;
use Stancl\Tenancy\Jobs\CreateDatabase; use Stancl\Tenancy\Jobs\CreateDatabase;
use Stancl\Tenancy\Events\TenantCreated; use Stancl\Tenancy\Events\TenantCreated;
use Stancl\Tenancy\Events\TenantDeleted; use Stancl\Tenancy\Events\TenantDeleted;
use Stancl\Tenancy\Events\DeletingTenant; use Stancl\Tenancy\Events\DeletingTenant;
use Stancl\Tenancy\TenancyBroadcastManager;
use Illuminate\Filesystem\FilesystemAdapter; use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Broadcasting\BroadcastManager;
use Stancl\Tenancy\Events\TenancyInitialized; use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Jobs\CreateStorageSymlinks; use Stancl\Tenancy\Jobs\CreateStorageSymlinks;
use Stancl\Tenancy\Jobs\RemoveStorageSymlinks; use Stancl\Tenancy\Jobs\RemoveStorageSymlinks;
use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Tests\Etc\TestingBroadcaster;
use Stancl\Tenancy\Listeners\DeleteTenantStorage; use Stancl\Tenancy\Listeners\DeleteTenantStorage;
use Stancl\Tenancy\Listeners\RevertToCentralContext; use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Bootstrappers\UrlTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper;
use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain;
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\BroadcastTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
beforeEach(function () { beforeEach(function () {
@ -328,6 +335,82 @@ test('local storage public urls are generated correctly', function() {
expect(File::isDirectory($tenantStoragePath))->toBeFalse(); expect(File::isDirectory($tenantStoragePath))->toBeFalse();
}); });
test('BroadcastTenancyBootstrapper binds TenancyBroadcastManager to BroadcastManager and reverts the binding when tenancy is ended', function() {
expect(app(BroadcastManager::class))->toBeInstanceOf(BroadcastManager::class);
tenancy()->initialize(Tenant::create());
expect(app(BroadcastManager::class))->toBeInstanceOf(TenancyBroadcastManager::class);
tenancy()->end();
expect(app(BroadcastManager::class))->toBeInstanceOf(BroadcastManager::class);
});
test('BroadcastTenancyBootstrapper maps tenant broadcaster credentials to config as specified in the $credentialsMap property and reverts the config after ending tenancy', function() {
config([
'broadcasting.connections.testing.driver' => 'testing',
'broadcasting.connections.testing.message' => $defaultMessage = 'default',
]);
BroadcastTenancyBootstrapper::$credentialsMap = [
'broadcasting.connections.testing.message' => 'testing_broadcaster_message',
];
$tenant = Tenant::create(['testing_broadcaster_message' => $tenantMessage = 'first testing']);
$tenant2 = Tenant::create(['testing_broadcaster_message' => $secondTenantMessage = 'second testing']);
tenancy()->initialize($tenant);
expect(array_key_exists('testing_broadcaster_message', tenant()->getAttributes()))->toBeTrue();
expect(config('broadcasting.connections.testing.message'))->toBe($tenantMessage);
tenancy()->initialize($tenant2);
expect(config('broadcasting.connections.testing.message'))->toBe($secondTenantMessage);
tenancy()->end();
expect(config('broadcasting.connections.testing.message'))->toBe($defaultMessage);
});
test('BroadcastTenancyBootstrapper makes the app use broadcasters with the correct credentials', function() {
config([
'broadcasting.default' => 'testing',
'broadcasting.connections.testing.driver' => 'testing',
'broadcasting.connections.testing.message' => $defaultMessage = 'default',
]);
TenancyBroadcastManager::$tenantBroadcasters[] = 'testing';
BroadcastTenancyBootstrapper::$credentialsMap = [
'broadcasting.connections.testing.message' => 'testing_broadcaster_message',
];
$registerTestingBroadcaster = fn() => app(BroadcastManager::class)->extend('testing', fn($app, $config) => new TestingBroadcaster($config['message']));
$registerTestingBroadcaster();
expect(invade(app(BroadcastManager::class)->driver())->message)->toBe($defaultMessage);
$tenant = Tenant::create(['testing_broadcaster_message' => $tenantMessage = 'first testing']);
$tenant2 = Tenant::create(['testing_broadcaster_message' => $secondTenantMessage = 'second testing']);
tenancy()->initialize($tenant);
$registerTestingBroadcaster();
expect(invade(app(BroadcastManager::class)->driver())->message)->toBe($tenantMessage);
tenancy()->initialize($tenant2);
$registerTestingBroadcaster();
expect(invade(app(BroadcastManager::class)->driver())->message)->toBe($secondTenantMessage);
tenancy()->end();
$registerTestingBroadcaster();
expect(invade(app(BroadcastManager::class)->driver())->message)->toBe($defaultMessage);
});
test('MailTenancyBootstrapper maps tenant mail credentials to config as specified in the $credentialsMap property and makes the mailer use tenant credentials', function() { test('MailTenancyBootstrapper maps tenant mail credentials to config as specified in the $credentialsMap property and makes the mailer use tenant credentials', function() {
MailTenancyBootstrapper::$credentialsMap = [ MailTenancyBootstrapper::$credentialsMap = [
'mail.mailers.smtp.username' => 'smtp_username', 'mail.mailers.smtp.username' => 'smtp_username',
@ -380,3 +463,48 @@ function getDiskPrefix(string $disk): string
return $prefix; return $prefix;
} }
test('url bootstrapper overrides the root url when tenancy gets initialized and reverts the url to the central one after tenancy ends', function() {
config(['tenancy.bootstrappers.url' => UrlTenancyBootstrapper::class]);
Route::group([
'middleware' => InitializeTenancyBySubdomain::class,
], function () {
Route::get('/', function () {
return true;
})->name('home');
});
$baseUrl = url(route('home'));
config(['app.url' => $baseUrl]);
$rootUrlOverride = function (Tenant $tenant) use ($baseUrl) {
$scheme = str($baseUrl)->before('://');
$hostname = str($baseUrl)->after($scheme . '://');
return $scheme . '://' . $tenant->getTenantKey() . '.' . $hostname;
};
UrlTenancyBootstrapper::$rootUrlOverride = $rootUrlOverride;
$tenant = Tenant::create();
$tenantUrl = $rootUrlOverride($tenant);
expect($tenantUrl)->not()->toBe($baseUrl);
expect(url(route('home')))->toBe($baseUrl);
expect(URL::to('/'))->toBe($baseUrl);
expect(config('app.url'))->toBe($baseUrl);
tenancy()->initialize($tenant);
expect(url(route('home')))->toBe($tenantUrl);
expect(URL::to('/'))->toBe($tenantUrl);
expect(config('app.url'))->toBe($tenantUrl);
tenancy()->end();
expect(url(route('home')))->toBe($baseUrl);
expect(URL::to('/'))->toBe($baseUrl);
expect(config('app.url'))->toBe($baseUrl);
});

View file

@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Events\TenancyEnded;
use Illuminate\Support\Facades\Broadcast;
use Stancl\Tenancy\TenancyBroadcastManager;
use Illuminate\Broadcasting\BroadcastManager;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Tests\Etc\TestingBroadcaster;
use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Illuminate\Contracts\Broadcasting\Broadcaster as BroadcasterContract;
beforeEach(function() {
withTenantDatabases();
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
});
test('bound broadcaster instance is the same before initializing tenancy and after ending it', function() {
config(['broadcasting.default' => 'null']);
TenancyBroadcastManager::$tenantBroadcasters[] = 'null';
$originalBroadcaster = app(BroadcasterContract::class);
tenancy()->initialize(Tenant::create());
// TenancyBroadcastManager binds new broadcaster
$tenantBroadcaster = app(BroadcastManager::class)->driver();
expect($tenantBroadcaster)->not()->toBe($originalBroadcaster);
tenancy()->end();
expect($originalBroadcaster)->toBe(app(BroadcasterContract::class));
});
test('new broadcasters get the channels from the previously bound broadcaster', function() {
config([
'broadcasting.default' => $driver = 'testing',
'broadcasting.connections.testing.driver' => $driver,
]);
TenancyBroadcastManager::$tenantBroadcasters[] = $driver;
$registerTestingBroadcaster = fn() => app(BroadcastManager::class)->extend('testing', fn($app, $config) => new TestingBroadcaster('testing'));
$getCurrentChannels = fn() => array_keys(invade(app(BroadcastManager::class)->driver())->channels);
$registerTestingBroadcaster();
Broadcast::channel($channel = 'testing-channel', fn() => true);
expect($channel)->toBeIn($getCurrentChannels());
tenancy()->initialize(Tenant::create());
$registerTestingBroadcaster();
expect($channel)->toBeIn($getCurrentChannels());
tenancy()->end();
$registerTestingBroadcaster();
expect($channel)->toBeIn($getCurrentChannels());
});

View file

@ -18,11 +18,13 @@ use Stancl\Tenancy\Events\TenantCreated;
use Stancl\Tenancy\Events\TenantDeleted; use Stancl\Tenancy\Events\TenantDeleted;
use Stancl\Tenancy\Tests\Etc\TestSeeder; use Stancl\Tenancy\Tests\Etc\TestSeeder;
use Stancl\Tenancy\Events\DeletingTenant; use Stancl\Tenancy\Events\DeletingTenant;
use Stancl\Tenancy\Events\DatabaseMigrated;
use Stancl\Tenancy\Tests\Etc\ExampleSeeder; use Stancl\Tenancy\Tests\Etc\ExampleSeeder;
use Stancl\Tenancy\Events\TenancyInitialized; use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Listeners\RevertToCentralContext; use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
use Stancl\Tenancy\Database\Exceptions\TenantDatabaseDoesNotExistException;
beforeEach(function () { beforeEach(function () {
if (file_exists($schemaPath = 'tests/Etc/tenant-schema-test.dump')) { if (file_exists($schemaPath = 'tests/Etc/tenant-schema-test.dump')) {
@ -109,6 +111,46 @@ test('migrate command loads schema state', function () {
expect(Schema::hasTable('users'))->toBeTrue(); expect(Schema::hasTable('users'))->toBeTrue();
}); });
test('migrate command only throws exceptions if skip-failing is not passed', function() {
Tenant::create();
$tenantWithoutDatabase = Tenant::create();
$databaseToDrop = $tenantWithoutDatabase->run(fn() => DB::connection()->getDatabaseName());
DB::statement("DROP DATABASE `$databaseToDrop`");
Tenant::create();
expect(fn() => pest()->artisan('tenants:migrate --schema-path="tests/Etc/tenant-schema.dump"'))->toThrow(TenantDatabaseDoesNotExistException::class);
expect(fn() => pest()->artisan('tenants:migrate --schema-path="tests/Etc/tenant-schema.dump" --skip-failing'))->not()->toThrow(TenantDatabaseDoesNotExistException::class);
});
test('migrate command does not stop after the first failure if skip-failing is passed', function() {
$tenants = collect([
Tenant::create(),
$tenantWithoutDatabase = Tenant::create(),
Tenant::create(),
]);
$migratedTenants = 0;
Event::listen(DatabaseMigrated::class, function() use (&$migratedTenants) {
$migratedTenants++;
});
$databaseToDrop = $tenantWithoutDatabase->run(fn() => DB::connection()->getDatabaseName());
DB::statement("DROP DATABASE `$databaseToDrop`");
Artisan::call('tenants:migrate', [
'--schema-path' => '"tests/Etc/tenant-schema.dump"',
'--skip-failing' => true,
'--tenants' => $tenants->pluck('id')->toArray(),
]);
expect($migratedTenants)->toBe(2);
});
test('dump command works', function () { test('dump command works', function () {
$tenant = Tenant::create(); $tenant = Tenant::create();
$schemaPath = 'tests/Etc/tenant-schema-test.dump'; $schemaPath = 'tests/Etc/tenant-schema-test.dump';

View file

@ -0,0 +1,25 @@
<?php
namespace Stancl\Tenancy\Tests\Etc;
use Illuminate\Broadcasting\Broadcasters\Broadcaster;
class TestingBroadcaster extends Broadcaster {
public function __construct(
public string $message
) {}
public function auth($request)
{
return true;
}
public function validAuthenticationResponse($request, $result)
{
return true;
}
public function broadcast(array $channels, $event, array $payload = [])
{
}
}

View file

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class TestCreateCompaniesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('companies', function (Blueprint $table) {
$table->increments('id');
$table->string('global_id')->unique();
$table->string('name');
$table->string('email');
});
}
public function down()
{
Schema::dropIfExists('companies');
}
}

View file

@ -79,7 +79,7 @@ test('ing events can be used to cancel db creation', function () {
}); });
$tenant = Tenant::create(); $tenant = Tenant::create();
dispatch_now(new CreateDatabase($tenant)); dispatch_sync(new CreateDatabase($tenant));
pest()->assertFalse($tenant->database()->manager()->databaseExists( pest()->assertFalse($tenant->database()->manager()->databaseExists(
$tenant->database()->getName() $tenant->database()->getName()
@ -171,12 +171,13 @@ test('database is not migrated if creation is disabled', function () {
})->toListener() })->toListener()
); );
Tenant::create([ $tenant = Tenant::create([
'tenancy_create_database' => false, 'tenancy_create_database' => false,
'tenancy_db_name' => 'already_created', 'tenancy_db_name' => 'already_created',
]); ]);
expect(pest()->hasFailed())->toBeFalse(); // assert test didn't fail
$this->assertTrue($tenant->exists());
}); });
class FooListener extends QueueableListener class FooListener extends QueueableListener

View file

@ -0,0 +1,29 @@
<?php
declare(strict_types=1);
use Illuminate\Foundation\Vite;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Stancl\Tenancy\Vite as StanclVite;
use Stancl\Tenancy\Features\ViteBundler;
test('vite helper uses our custom class', function() {
$vite = app(Vite::class);
expect($vite)->toBeInstanceOf(Vite::class);
expect($vite)->not()->toBeInstanceOf(StanclVite::class);
config([
'tenancy.features' => [ViteBundler::class],
]);
$tenant = Tenant::create();
tenancy()->initialize($tenant);
app()->forgetInstance(Vite::class);
$vite = app(Vite::class);
expect($vite)->toBeInstanceOf(StanclVite::class);
});

View file

@ -27,6 +27,8 @@ function assertMailerTransportUsesPassword(string|null $password) {
}; };
test('mailer transport uses the correct credentials', function() { test('mailer transport uses the correct credentials', function() {
withTenantDatabases();
config(['mail.default' => 'smtp', 'mail.mailers.smtp.password' => $defaultPassword = 'DEFAULT']); config(['mail.default' => 'smtp', 'mail.mailers.smtp.password' => $defaultPassword = 'DEFAULT']);
MailTenancyBootstrapper::$credentialsMap = ['mail.mailers.smtp.password' => 'smtp_password']; MailTenancyBootstrapper::$credentialsMap = ['mail.mailers.smtp.password' => 'smtp_password'];
@ -52,6 +54,8 @@ test('mailer transport uses the correct credentials', function() {
test('initializing and ending tenancy binds a fresh MailManager instance without cached mailers', function() { test('initializing and ending tenancy binds a fresh MailManager instance without cached mailers', function() {
withTenantDatabases();
$mailers = fn() => invade(app(MailManager::class))->mailers; $mailers = fn() => invade(app(MailManager::class))->mailers;
app(MailManager::class)->mailer('smtp'); app(MailManager::class)->mailer('smtp');

View file

@ -1,6 +1,10 @@
<?php <?php
use Stancl\Tenancy\Tests\TestCase; use Stancl\Tenancy\Tests\TestCase;
use Stancl\JobPipeline\JobPipeline;
use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Jobs\CreateDatabase;
use Stancl\Tenancy\Events\TenantCreated;
uses(TestCase::class)->in(__DIR__); uses(TestCase::class)->in(__DIR__);
@ -8,3 +12,10 @@ function pest(): TestCase
{ {
return Pest\TestSuite::getInstance()->test; return Pest\TestSuite::getInstance()->test;
} }
function withTenantDatabases()
{
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
return $event->tenant;
})->toListener());
}

View file

@ -3,23 +3,23 @@
declare(strict_types=1); declare(strict_types=1);
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Spatie\Valuestore\Valuestore; use Spatie\Valuestore\Valuestore;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Stancl\Tenancy\Tests\Etc\User; use Stancl\Tenancy\Tests\Etc\User;
use Stancl\JobPipeline\JobPipeline; use Stancl\JobPipeline\JobPipeline;
use Stancl\Tenancy\Tests\Etc\Tenant; use Stancl\Tenancy\Tests\Etc\Tenant;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;
use Stancl\Tenancy\Events\TenancyEnded; use Stancl\Tenancy\Events\TenancyEnded;
use Stancl\Tenancy\Jobs\CreateDatabase; use Stancl\Tenancy\Jobs\CreateDatabase;
use Illuminate\Queue\InteractsWithQueue;
use Stancl\Tenancy\Events\TenantCreated; use Stancl\Tenancy\Events\TenantCreated;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Queue\Events\JobProcessed; use Illuminate\Queue\Events\JobProcessed;
use Illuminate\Queue\Events\JobProcessing; use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Stancl\Tenancy\Events\TenancyInitialized; use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Listeners\RevertToCentralContext; use Stancl\Tenancy\Listeners\RevertToCentralContext;
@ -48,6 +48,8 @@ afterEach(function () {
}); });
test('tenant id is passed to tenant queues', function () { test('tenant id is passed to tenant queues', function () {
withTenantDatabases();
config(['queue.default' => 'sync']); config(['queue.default' => 'sync']);
$tenant = Tenant::create(); $tenant = Tenant::create();
@ -64,6 +66,8 @@ test('tenant id is passed to tenant queues', function () {
}); });
test('tenant id is not passed to central queues', function () { test('tenant id is not passed to central queues', function () {
withTenantDatabases();
$tenant = Tenant::create(); $tenant = Tenant::create();
tenancy()->initialize($tenant); tenancy()->initialize($tenant);
@ -156,6 +160,8 @@ test('tenancy is initialized when retrying jobs', function (bool $shouldEndTenan
})->with([true, false]); })->with([true, false]);
test('the tenant used by the job doesnt change when the current tenant changes', function () { test('the tenant used by the job doesnt change when the current tenant changes', function () {
withTenantDatabases();
$tenant1 = Tenant::create([ $tenant1 = Tenant::create([
'id' => 'acme', 'id' => 'acme',
]); ]);
@ -217,13 +223,6 @@ function withUsers()
}); });
} }
function withTenantDatabases()
{
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
return $event->tenant;
})->toListener());
}
class TestJob implements ShouldQueue class TestJob implements ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

View file

@ -832,7 +832,7 @@ function migrateUsersTableForTenants(): void
// Tenant model used for resource syncing setup // Tenant model used for resource syncing setup
class ResourceTenant extends Tenant class ResourceTenant extends Tenant
{ {
public function users() public function users(): BelongsToMany
{ {
return $this->belongsToMany(CentralUser::class, 'tenant_users', 'tenant_id', 'global_user_id', 'id', 'global_id') return $this->belongsToMany(CentralUser::class, 'tenant_users', 'tenant_id', 'global_user_id', 'id', 'global_id')
->using(TenantPivot::class); ->using(TenantPivot::class);

View file

@ -0,0 +1,398 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Str;
use Stancl\JobPipeline\JobPipeline;
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
use Stancl\Tenancy\Contracts\Syncable;
use Stancl\Tenancy\Contracts\SyncMaster;
use Stancl\Tenancy\Database\Concerns\CentralConnection;
use Stancl\Tenancy\Database\Concerns\ResourceSyncing;
use Stancl\Tenancy\Database\DatabaseConfig;
use Stancl\Tenancy\Database\Models\TenantMorphPivot;
use Stancl\Tenancy\Database\Models\TenantPivot;
use Stancl\Tenancy\Events\SyncedResourceSaved;
use Stancl\Tenancy\Events\TenancyEnded;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Events\TenantCreated;
use Stancl\Tenancy\Jobs\CreateDatabase;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Listeners\UpdateSyncedResource;
use Stancl\Tenancy\Tests\Etc\Tenant;
beforeEach(function () {
config([
'tenancy.bootstrappers' => [
DatabaseTenancyBootstrapper::class,
],
'tenancy.models.tenant' => ResourceTenantUsingPolymorphic::class,
]);
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
return $event->tenant;
})->toListener());
DatabaseConfig::generateDatabaseNamesUsing(function () {
return 'db' . Str::random(16);
});
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
UpdateSyncedResource::$shouldQueue = false; // Global state cleanup
Event::listen(SyncedResourceSaved::class, UpdateSyncedResource::class);
// Run migrations on central connection
pest()->artisan('migrate', [
'--path' => [
__DIR__ . '/../assets/resource-syncing-migrations',
__DIR__ . '/Etc/synced_resource_migrations/users',
__DIR__ . '/Etc/synced_resource_migrations/companies',
],
'--realpath' => true,
])->assertExitCode(0);
});
test('resource syncing works using a single pivot table for multiple models when syncing from central to tenant', function () {
$tenant1 = ResourceTenantUsingPolymorphic::create(['id' => 't1']);
migrateUsersTableForTenants();
$centralUser = CentralUserUsingPolymorphic::create([
'global_id' => 'acme',
'name' => 'John Doe',
'email' => 'john@localhost',
'password' => 'password',
'role' => 'commenter',
]);
$tenant1->run(function () {
expect(TenantUserUsingPolymorphic::all())->toHaveCount(0);
});
$centralUser->tenants()->attach('t1');
// Assert `tenants` are accessible
expect($centralUser->tenants->pluck('id')->toArray())->toBe(['t1']);
// Users are accessible from tenant
expect($tenant1->users()->pluck('email')->toArray())->toBe(['john@localhost']);
// Assert User resource is synced
$tenant1->run(function () use ($centralUser) {
$tenantUser = TenantUserUsingPolymorphic::first()->toArray();
$centralUser = $centralUser->withoutRelations()->toArray();
unset($centralUser['id'], $tenantUser['id']);
expect($tenantUser)->toBe($centralUser);
});
$tenant2 = ResourceTenantUsingPolymorphic::create(['id' => 't2']);
migrateCompaniesTableForTenants();
$centralCompany = CentralCompanyUsingPolymorphic::create([
'global_id' => 'acme',
'name' => 'ArchTech',
'email' => 'archtech@localhost',
]);
$tenant2->run(function () {
expect(TenantCompanyUsingPolymorphic::all())->toHaveCount(0);
});
$centralCompany->tenants()->attach('t2');
// Assert `tenants` are accessible
expect($centralCompany->tenants->pluck('id')->toArray())->toBe(['t2']);
// Companies are accessible from tenant
expect($tenant2->companies()->pluck('email')->toArray())->toBe(['archtech@localhost']);
// Assert Company resource is synced
$tenant2->run(function () use ($centralCompany) {
$tenantCompany = TenantCompanyUsingPolymorphic::first()->toArray();
$centralCompany = $centralCompany->withoutRelations()->toArray();
unset($centralCompany['id'], $tenantCompany['id']);
expect($tenantCompany)->toBe($centralCompany);
});
});
test('resource syncing works using a single pivot table for multiple models when syncing from tenant to central', function () {
$tenant1 = ResourceTenantUsingPolymorphic::create(['id' => 't1']);
migrateUsersTableForTenants();
tenancy()->initialize($tenant1);
$tenantUser = TenantUserUsingPolymorphic::create([
'name' => 'John Doe',
'email' => 'john@localhost',
'password' => 'password',
'role' => 'commenter',
]);
tenancy()->end();
// Assert User resource is synced
$centralUser = CentralUserUsingPolymorphic::first();
// Assert `tenants` are accessible
expect($centralUser->tenants->pluck('id')->toArray())->toBe(['t1']);
// Users are accessible from tenant
expect($tenant1->users()->pluck('email')->toArray())->toBe(['john@localhost']);
$centralUser = $centralUser->withoutRelations()->toArray();
$tenantUser = $tenantUser->toArray();
unset($centralUser['id'], $tenantUser['id']);
// array keys use a different order here
expect($tenantUser)->toEqualCanonicalizing($centralUser);
$tenant2 = ResourceTenantUsingPolymorphic::create(['id' => 't2']);
migrateCompaniesTableForTenants();
tenancy()->initialize($tenant2);
$tenantCompany = TenantCompanyUsingPolymorphic::create([
'global_id' => 'acme',
'name' => 'tenant comp',
'email' => 'company@localhost',
]);
tenancy()->end();
// Assert Company resource is synced
$centralCompany = CentralCompanyUsingPolymorphic::first();
// Assert `tenants` are accessible
expect($centralCompany->tenants->pluck('id')->toArray())->toBe(['t2']);
// Companies are accessible from tenant
expect($tenant2->companies()->pluck('email')->toArray())->toBe(['company@localhost']);
$centralCompany = $centralCompany->withoutRelations()->toArray();
$tenantCompany = $tenantCompany->toArray();
unset($centralCompany['id'], $tenantCompany['id']);
expect($tenantCompany)->toBe($centralCompany);
});
test('right resources are accessible from the tenant', function () {
$tenant1 = ResourceTenantUsingPolymorphic::create(['id' => 't1']);
$tenant2 = ResourceTenantUsingPolymorphic::create(['id' => 't2']);
migrateUsersTableForTenants();
$user1 = CentralUserUsingPolymorphic::create([
'global_id' => 'user1',
'name' => 'user1',
'email' => 'user1@localhost',
'password' => 'password',
'role' => 'commenter',
]);
$user2 = CentralUserUsingPolymorphic::create([
'global_id' => 'user2',
'name' => 'user2',
'email' => 'user2@localhost',
'password' => 'password',
'role' => 'commenter',
]);
$user3 = CentralUserUsingPolymorphic::create([
'global_id' => 'user3',
'name' => 'user3',
'email' => 'user3@localhost',
'password' => 'password',
'role' => 'commenter',
]);
$user1->tenants()->attach('t1');
$user2->tenants()->attach('t1');
$user3->tenants()->attach('t2');
expect($tenant1->users()->pluck('email')->toArray())->toBe([$user1->email, $user2->email]);
expect($tenant2->users()->pluck('email')->toArray())->toBe([$user3->email]);
});
function migrateCompaniesTableForTenants(): void
{
pest()->artisan('tenants:migrate', [
'--path' => __DIR__ . '/Etc/synced_resource_migrations/companies',
'--realpath' => true,
])->assertExitCode(0);
}
// Tenant model used for resource syncing setup
class ResourceTenantUsingPolymorphic extends Tenant
{
public function users(): MorphToMany
{
return $this->morphedByMany(CentralUserUsingPolymorphic::class, 'tenant_resources', 'tenant_resources', 'tenant_id', 'resource_global_id', 'id', 'global_id')
->using(TenantMorphPivot::class);
}
public function companies(): MorphToMany
{
return $this->morphedByMany(CentralCompanyUsingPolymorphic::class, 'tenant_resources', 'tenant_resources', 'tenant_id', 'resource_global_id', 'id', 'global_id')
->using(TenantMorphPivot::class);
}
}
class CentralUserUsingPolymorphic extends Model implements SyncMaster
{
use ResourceSyncing, CentralConnection;
protected $guarded = [];
public $timestamps = false;
public $table = 'users';
public function getTenantModelName(): string
{
return TenantUserUsingPolymorphic::class;
}
public function getGlobalIdentifierKey(): string|int
{
return $this->getAttribute($this->getGlobalIdentifierKeyName());
}
public function getGlobalIdentifierKeyName(): string
{
return 'global_id';
}
public function getCentralModelName(): string
{
return static::class;
}
public function getSyncedAttributeNames(): array
{
return [
'global_id',
'name',
'password',
'email',
];
}
}
class TenantUserUsingPolymorphic extends Model implements Syncable
{
use ResourceSyncing;
protected $table = 'users';
protected $guarded = [];
public $timestamps = false;
public function getGlobalIdentifierKey(): string|int
{
return $this->getAttribute($this->getGlobalIdentifierKeyName());
}
public function getGlobalIdentifierKeyName(): string
{
return 'global_id';
}
public function getCentralModelName(): string
{
return CentralUserUsingPolymorphic::class;
}
public function getSyncedAttributeNames(): array
{
return [
'global_id',
'name',
'password',
'email',
];
}
}
class CentralCompanyUsingPolymorphic extends Model implements SyncMaster
{
use ResourceSyncing, CentralConnection;
protected $guarded = [];
public $timestamps = false;
public $table = 'companies';
public function getTenantModelName(): string
{
return TenantCompanyUsingPolymorphic::class;
}
public function getGlobalIdentifierKey(): string|int
{
return $this->getAttribute($this->getGlobalIdentifierKeyName());
}
public function getGlobalIdentifierKeyName(): string
{
return 'global_id';
}
public function getCentralModelName(): string
{
return static::class;
}
public function getSyncedAttributeNames(): array
{
return [
'global_id',
'name',
'email',
];
}
}
class TenantCompanyUsingPolymorphic extends Model implements Syncable
{
use ResourceSyncing;
protected $table = 'companies';
protected $guarded = [];
public $timestamps = false;
public function getGlobalIdentifierKey(): string|int
{
return $this->getAttribute($this->getGlobalIdentifierKeyName());
}
public function getGlobalIdentifierKeyName(): string
{
return 'global_id';
}
public function getCentralModelName(): string
{
return CentralCompanyUsingPolymorphic::class;
}
public function getSyncedAttributeNames(): array
{
return [
'global_id',
'name',
'email',
];
}
}

View file

@ -390,6 +390,81 @@ test('path used by sqlite manager can be customized', function () {
expect(file_exists($customPath . '/' . $name))->toBeTrue(); expect(file_exists($customPath . '/' . $name))->toBeTrue();
}); });
test('the tenant connection template can be specified either by name or as a connection array', function () {
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
return $event->tenant;
})->toListener());
config([
'tenancy.database.managers.mysql' => MySQLDatabaseManager::class,
'tenancy.database.template_tenant_connection' => 'mysql',
]);
$name = 'foo' . Str::random(8);
$tenant = Tenant::create([
'tenancy_db_name' => $name,
]);
/** @var MySQLDatabaseManager $manager */
$manager = $tenant->database()->manager();
expect($manager->databaseExists($name))->toBeTrue();
expect($manager->database()->getConfig('host'))->toBe('mysql');
config([
'tenancy.database.template_tenant_connection' => [
'driver' => 'mysql',
'url' => null,
'host' => 'mysql2',
'port' => '3306',
'database' => 'main',
'username' => 'root',
'password' => 'password',
'unix_socket' => '',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => [],
],
]);
$tenant = Tenant::create([
'tenancy_db_name' => $name,
]);
/** @var MySQLDatabaseManager $manager */
$manager = $tenant->database()->manager();
expect($manager->databaseExists($name))->toBeTrue(); // tenant connection works
expect($manager->database()->getConfig('host'))->toBe('mysql2');
});
test('partial tenant connection templates get merged into the central connection template', function () {
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
return $event->tenant;
})->toListener());
config([
'database.connections.central.url' => 'example.com',
'tenancy.database.template_tenant_connection' => [
'url' => null,
'host' => 'mysql2',
],
]);
$name = 'foo' . Str::random(8);
$tenant = Tenant::create([
'tenancy_db_name' => $name,
]);
/** @var MySQLDatabaseManager $manager */
$manager = $tenant->database()->manager();
expect($manager->databaseExists($name))->toBeTrue(); // tenant connection works
expect($manager->database()->getConfig('host'))->toBe('mysql2');
expect($manager->database()->getConfig('url'))->toBeNull();
});
// Datasets // Datasets
dataset('database_managers', [ dataset('database_managers', [
['mysql', MySQLDatabaseManager::class], ['mysql', MySQLDatabaseManager::class],

View file

@ -4,16 +4,17 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Tests; namespace Stancl\Tenancy\Tests;
use Dotenv\Dotenv;
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Redis;
use PDO; use PDO;
use Stancl\Tenancy\Bootstrappers\BatchTenancyBootstrapper; use Dotenv\Dotenv;
use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper;
use Stancl\Tenancy\Facades\GlobalCache;
use Stancl\Tenancy\Facades\Tenancy; use Stancl\Tenancy\Facades\Tenancy;
use Stancl\Tenancy\TenancyServiceProvider;
use Stancl\Tenancy\Tests\Etc\Tenant; use Stancl\Tenancy\Tests\Etc\Tenant;
use Illuminate\Support\Facades\Redis;
use Illuminate\Foundation\Application;
use Stancl\Tenancy\Facades\GlobalCache;
use Stancl\Tenancy\TenancyServiceProvider;
use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\BroadcastTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\UrlTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper;
abstract class TestCase extends \Orchestra\Testbench\TestCase abstract class TestCase extends \Orchestra\Testbench\TestCase
@ -105,7 +106,9 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
'--force' => true, '--force' => true,
], ],
'tenancy.bootstrappers.redis' => RedisTenancyBootstrapper::class, // todo1 change this to []? two tests in TenantDatabaseManagerTest are failing with that 'tenancy.bootstrappers.redis' => RedisTenancyBootstrapper::class, // todo1 change this to []? two tests in TenantDatabaseManagerTest are failing with that
'tenancy.bootstrappers.broadcast' => BroadcastTenancyBootstrapper::class, // todo1 change this to []? two tests in TenantDatabaseManagerTest are failing with that
'tenancy.bootstrappers.mail' => MailTenancyBootstrapper::class, 'tenancy.bootstrappers.mail' => MailTenancyBootstrapper::class,
'tenancy.bootstrappers.url' => UrlTenancyBootstrapper::class,
'queue.connections.central' => [ 'queue.connections.central' => [
'driver' => 'sync', 'driver' => 'sync',
'central' => true, 'central' => true,
@ -115,7 +118,9 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
]); ]);
$app->singleton(RedisTenancyBootstrapper::class); // todo (Samuel) use proper approach eg config for singleton registration $app->singleton(RedisTenancyBootstrapper::class); // todo (Samuel) use proper approach eg config for singleton registration
$app->singleton(BroadcastTenancyBootstrapper::class);
$app->singleton(MailTenancyBootstrapper::class); $app->singleton(MailTenancyBootstrapper::class);
$app->singleton(UrlTenancyBootstrapper::class);
} }
protected function getPackageProviders($app) protected function getPackageProviders($app)