1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 02:14:03 +00:00

Merge branch 'master' into unware-feature

This commit is contained in:
lukinovec 2023-12-08 15:42:06 +01:00 committed by GitHub
commit 10babdd1f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 635 additions and 54 deletions

View file

@ -16,8 +16,6 @@ jobs:
strategy: strategy:
matrix: matrix:
include: include:
- laravel: "^9.0"
php: "8.0"
- laravel: "^10.0" - laravel: "^10.0"
php: "8.2" php: "8.2"

View file

@ -124,7 +124,7 @@ class TenancyServiceProvider extends ServiceProvider
* Example of CLI tenant URL root override: * Example of CLI tenant URL root override:
* *
* UrlTenancyBootstrapper::$rootUrlOverride = function (Tenant $tenant) { * UrlTenancyBootstrapper::$rootUrlOverride = function (Tenant $tenant) {
* $baseUrl = url('/'); * $baseUrl = env('APP_URL');
* $scheme = str($baseUrl)->before('://'); * $scheme = str($baseUrl)->before('://');
* $hostname = str($baseUrl)->after($scheme . '://'); * $hostname = str($baseUrl)->after($scheme . '://');
* *

View file

@ -2,6 +2,7 @@
declare(strict_types=1); declare(strict_types=1);
use Stancl\Tenancy\CacheManager;
use Stancl\Tenancy\Middleware; use Stancl\Tenancy\Middleware;
use Stancl\Tenancy\Resolvers; use Stancl\Tenancy\Resolvers;
@ -98,10 +99,10 @@ return [
*/ */
'bootstrappers' => [ 'bootstrappers' => [
Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper::class, Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper::class,
Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper::class,
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\PrefixCacheTenancyBootstrapper::class,
// Stancl\Tenancy\Bootstrappers\UrlTenancyBootstrapper::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
@ -178,7 +179,7 @@ return [
], ],
/** /**
* Cache tenancy config. Used by CacheTenancyBootstrapper. * Cache tenancy config. Used by the custom CacheManager and the PrefixCacheTenancyBootstrapper.
* *
* This works for all Cache facade calls, cache() helper * This works for all Cache facade calls, cache() helper
* calls and direct calls to injected cache stores. * calls and direct calls to injected cache stores.
@ -190,6 +191,7 @@ return [
*/ */
'cache' => [ 'cache' => [
'tag_base' => 'tenant', // This tag_base, followed by the tenant_id, will form a tag that will be applied on each cache call. 'tag_base' => 'tenant', // This tag_base, followed by the tenant_id, will form a tag that will be applied on each cache call.
'prefix_base' => 'tenant_', // This prefix_base, followed by the tenant_id, will form a cache prefix that will be used for every cache key.
], ],
/** /**

View file

@ -20,6 +20,7 @@ return new class extends Migration
$table->string('token', 128)->primary(); $table->string('token', 128)->primary();
$table->string(Tenancy::tenantKeyColumn()); $table->string(Tenancy::tenantKeyColumn());
$table->string('user_id'); $table->string('user_id');
$table->boolean('remember');
$table->string('auth_guard'); $table->string('auth_guard');
$table->string('redirect_url'); $table->string('redirect_url');
$table->timestamp('created_at'); $table->timestamp('created_at');

View file

@ -17,17 +17,17 @@
"require": { "require": {
"php": "^8.2", "php": "^8.2",
"ext-json": "*", "ext-json": "*",
"illuminate/support": "^9.38|^10.0", "illuminate/support": "^10.1",
"facade/ignition-contracts": "^1.0.2", "facade/ignition-contracts": "^1.0.2",
"spatie/ignition": "^1.4", "spatie/ignition": "^1.4",
"ramsey/uuid": "^4.7.3", "ramsey/uuid": "^4.7.3",
"stancl/jobpipeline": "^1.6.2", "stancl/jobpipeline": "2.0.0-rc1",
"stancl/virtualcolumn": "^1.3.1", "stancl/virtualcolumn": "^1.3.1",
"spatie/invade": "^1.1" "spatie/invade": "^1.1"
}, },
"require-dev": { "require-dev": {
"laravel/framework": "^9.38|^10.0", "laravel/framework": "^10.1",
"orchestra/testbench": "^7.0|^8.0", "orchestra/testbench": "^8.0",
"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",

3
doctum/.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
build/
cache/
vendor/

8
doctum/composer.json Normal file
View file

@ -0,0 +1,8 @@
{
"require": {
"code-lts/doctum": "v5.5.x-dev"
},
"scripts": {
"doctum": "./vendor/bin/doctum.php update doctum.php --output-format=github --force --ignore-parse-errors --no-ansi --no-progress -v"
}
}

30
doctum/doctum.php Normal file
View file

@ -0,0 +1,30 @@
<?php
require __DIR__.'/vendor/autoload.php';
use Doctum\Doctum;
use Symfony\Component\Finder\Finder;
use Doctum\Version\GitVersionCollection;
use Doctum\RemoteRepository\GitHubRemoteRepository;
$iterator = Finder::create()
->files()
->name('*.php')
->in($dir = __DIR__ . '/../src');
$versions = GitVersionCollection::create($dir)
->add('1.x', 'Tenancy 1.x')
->add('2.x', 'Tenancy 2.x')
->add('3.x', 'Tenancy 3.x')
->add('master', 'Tenancy Dev');
return new Doctum($iterator, [
'title' => 'Tenancy for Laravel API Documentation',
'versions' => $versions,
'build_dir' => __DIR__ . '/build/%version%',
'cache_dir' => __DIR__ . '/cache/%version%',
'default_opened_level' => 2,
'base_url' => 'https://api.tenancyforlaravel.com/',
'favicon' => 'https://tenancyforlaravel.com/favicon.ico',
'remote_repository' => new GitHubRemoteRepository('archtechx/tenancy', dirname($dir)),
]);

View file

@ -7,13 +7,20 @@ namespace Stancl\Tenancy\Bootstrappers;
use Illuminate\Cache\CacheManager; use Illuminate\Cache\CacheManager;
use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Stancl\Tenancy\CacheManager as TenantCacheManager;
use Stancl\Tenancy\Contracts\TenancyBootstrapper; use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant; use Stancl\Tenancy\Contracts\Tenant;
class CacheTenancyBootstrapper implements TenancyBootstrapper /**
* todo name.
*
* Separate tenant cache using tagging.
* This is the legacy approach. Some things, like dependency injection, won't work properly with this bootstrapper.
* PrefixCacheTenancyBootstrapper is the recommended bootstrapper for cache separation.
*/
class CacheTagsBootstrapper implements TenancyBootstrapper
{ {
protected ?CacheManager $originalCache = null; protected ?CacheManager $originalCache = null;
public static string $cacheManagerWithTags = \Stancl\Tenancy\CacheManager::class;
public function __construct( public function __construct(
protected Application $app protected Application $app
@ -24,9 +31,9 @@ class CacheTenancyBootstrapper implements TenancyBootstrapper
{ {
$this->resetFacadeCache(); $this->resetFacadeCache();
$this->originalCache = $this->originalCache ?? $this->app['cache']; $this->originalCache ??= $this->app['cache'];
$this->app->extend('cache', function () { $this->app->extend('cache', function () {
return new TenantCacheManager($this->app); return new static::$cacheManagerWithTags($this->app);
}); });
} }

View file

@ -0,0 +1,84 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Bootstrappers;
use Closure;
use Illuminate\Cache\CacheManager;
use Illuminate\Cache\Repository;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Illuminate\Support\Facades\Cache;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant;
class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper
{
protected string|null $originalPrefix = null;
public static array $tenantCacheStores = []; // E.g. ['redis']
public static Closure|null $prefixGenerator = null;
public function __construct(
protected ConfigRepository $config,
protected CacheManager $cacheManager,
) {
}
public function bootstrap(Tenant $tenant): void
{
$this->originalPrefix = $this->config->get('cache.prefix');
$prefix = $this->generatePrefix($tenant);
foreach (static::$tenantCacheStores as $store) {
$this->setCachePrefix($store, $prefix);
// Now that the store uses the passed prefix
// Set the configured prefix back to the default one
$this->config->set('cache.prefix', $this->originalPrefix);
}
}
public function revert(): void
{
foreach (static::$tenantCacheStores as $store) {
$this->setCachePrefix($store, $this->originalPrefix);
}
}
protected function setCachePrefix(string $driver, string|null $prefix): void
{
$this->config->set('cache.prefix', $prefix);
// Refresh driver's store to make the driver use the current prefix
$this->refreshStore($driver);
// It is needed when a call to the facade has been made before bootstrapping tenancy
// The facade has its own cache, separate from the container
Cache::clearResolvedInstances();
}
public function generatePrefix(Tenant $tenant): string
{
$defaultPrefix = $this->originalPrefix . $this->config->get('tenancy.cache.prefix_base') . $tenant->getTenantKey();
return static::$prefixGenerator ? (static::$prefixGenerator)($tenant) : $defaultPrefix;
}
public static function generatePrefixUsing(Closure $prefixGenerator): void
{
static::$prefixGenerator = $prefixGenerator;
}
/**
* Refresh cache driver's store.
*/
protected function refreshStore(string $driver): void
{
$newStore = $this->cacheManager->resolve($driver)->getStore();
/** @var Repository $repository */
$repository = $this->cacheManager->driver($driver);
$repository->setStore($newStore);
}
}

View file

@ -41,7 +41,7 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper
*/ */
public static function __constructStatic(Application $app): void public static function __constructStatic(Application $app): void
{ {
static::setUpJobListener($app->make(Dispatcher::class), $app->runningUnitTests()); static::setUpJobListener($app->make(Dispatcher::class));
} }
public function __construct(Repository $config, QueueManager $queue) public function __construct(Repository $config, QueueManager $queue)
@ -52,7 +52,7 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper
$this->setUpPayloadGenerator(); $this->setUpPayloadGenerator();
} }
protected static function setUpJobListener(Dispatcher $dispatcher, bool $runningTests): void protected static function setUpJobListener(Dispatcher $dispatcher): void
{ {
$previousTenant = null; $previousTenant = null;
@ -69,10 +69,8 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper
}); });
// If we're running tests, we make sure to clean up after any artisan('queue:work') calls // If we're running tests, we make sure to clean up after any artisan('queue:work') calls
$revertToPreviousState = function ($event) use (&$previousTenant, $runningTests) { $revertToPreviousState = function ($event) use (&$previousTenant) {
if ($runningTests) {
static::revertToPreviousState($event, $previousTenant); static::revertToPreviousState($event, $previousTenant);
}
}; };
$dispatcher->listen(JobProcessed::class, $revertToPreviousState); // artisan('queue:work') which succeeds $dispatcher->listen(JobProcessed::class, $revertToPreviousState); // artisan('queue:work') which succeeds

View file

@ -18,6 +18,7 @@ use Stancl\Tenancy\Exceptions\StatefulGuardRequiredException;
* @property string $user_id * @property string $user_id
* @property string $auth_guard * @property string $auth_guard
* @property string $redirect_url * @property string $redirect_url
* @property bool $remember
* @property Carbon $created_at * @property Carbon $created_at
*/ */
class ImpersonationToken extends Model class ImpersonationToken extends Model

View file

@ -6,10 +6,12 @@ namespace Stancl\Tenancy\Exceptions;
use Exception; use Exception;
// todo@v4 improve all exception messages
class ModelNotSyncMasterException extends Exception class ModelNotSyncMasterException extends Exception
{ {
public function __construct(string $class) public function __construct(string $class)
{ {
parent::__construct("Model of $class class is not an SyncMaster model. Make sure you're using the central model to make changes to synced resources when you're in the central context"); parent::__construct("Model of $class class is not a SyncMaster model. Make sure you're using the central model to make changes to synced resources when you're in the central context");
} }
} }

View file

@ -24,6 +24,7 @@ class UserImpersonation implements Feature
'user_id' => $userId, 'user_id' => $userId,
'redirect_url' => $redirectUrl, 'redirect_url' => $redirectUrl,
'auth_guard' => $authGuard, 'auth_guard' => $authGuard,
'remember' => $remember,
]); ]);
}); });
} }
@ -44,7 +45,7 @@ class UserImpersonation implements Feature
abort_unless($tokenTenantId === $currentTenantId, 403); abort_unless($tokenTenantId === $currentTenantId, 403);
Auth::guard($token->auth_guard)->loginUsingId($token->user_id); Auth::guard($token->auth_guard)->loginUsingId($token->user_id, $token->remember);
$token->delete(); $token->delete();

View file

@ -28,18 +28,26 @@ use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Tests\Etc\TestingBroadcaster; 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\CacheTagsBootstrapper;
use Stancl\Tenancy\Bootstrappers\UrlTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\UrlTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper;
use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain; use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain;
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\BroadcastTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\BroadcastTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper;
beforeEach(function () { beforeEach(function () {
$this->mockConsoleOutput = false; $this->mockConsoleOutput = false;
config(['cache.default' => $cacheDriver = 'redis']);
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver];
// Reset static properties of classes used in this test file to their default values
BroadcastTenancyBootstrapper::$credentialsMap = [];
TenancyBroadcastManager::$tenantBroadcasters = ['pusher', 'ably'];
UrlTenancyBootstrapper::$rootUrlOverride = null;
Event::listen( Event::listen(
TenantCreated::class, TenantCreated::class,
JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) { JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
@ -51,6 +59,14 @@ beforeEach(function () {
Event::listen(TenancyEnded::class, RevertToCentralContext::class); Event::listen(TenancyEnded::class, RevertToCentralContext::class);
}); });
afterEach(function () {
// Reset static properties of classes used in this test file to their default values
UrlTenancyBootstrapper::$rootUrlOverride = null;
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [];
TenancyBroadcastManager::$tenantBroadcasters = ['pusher', 'ably'];
BroadcastTenancyBootstrapper::$credentialsMap = [];
});
test('database data is separated', function () { test('database data is separated', function () {
config(['tenancy.bootstrappers' => [ config(['tenancy.bootstrappers' => [
DatabaseTenancyBootstrapper::class, DatabaseTenancyBootstrapper::class,
@ -82,12 +98,9 @@ test('database data is separated', function () {
expect(DB::table('users')->first()->name)->toBe('Foo'); expect(DB::table('users')->first()->name)->toBe('Foo');
}); });
test('cache data is separated', function () { test('cache data is separated', function (string $bootstrapper) {
config([ config([
'tenancy.bootstrappers' => [ 'tenancy.bootstrappers' => [$bootstrapper],
CacheTenancyBootstrapper::class,
],
'cache.default' => 'redis',
]); ]);
$tenant1 = Tenant::create(); $tenant1 = Tenant::create();
@ -121,7 +134,10 @@ test('cache data is separated', function () {
// Asset central is still the same // Asset central is still the same
expect(Cache::get('foo'))->toBe('central'); expect(Cache::get('foo'))->toBe('central');
}); })->with([
CacheTagsBootstrapper::class,
PrefixCacheTenancyBootstrapper::class,
]);
test('redis data is separated', function () { test('redis data is separated', function () {
config(['tenancy.bootstrappers' => [ config(['tenancy.bootstrappers' => [

View file

@ -15,10 +15,15 @@ use Illuminate\Contracts\Broadcasting\Broadcaster as BroadcasterContract;
beforeEach(function () { beforeEach(function () {
withTenantDatabases(); withTenantDatabases();
TenancyBroadcastManager::$tenantBroadcasters = ['pusher', 'ably'];
Event::listen(TenancyInitialized::class, BootstrapTenancy::class); Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
Event::listen(TenancyEnded::class, RevertToCentralContext::class); Event::listen(TenancyEnded::class, RevertToCentralContext::class);
}); });
afterEach(function () {
TenancyBroadcastManager::$tenantBroadcasters = ['pusher', 'ably'];
});
test('bound broadcaster instance is the same before initializing tenancy and after ending it', function() { test('bound broadcaster instance is the same before initializing tenancy and after ending it', function() {
config(['broadcasting.default' => 'null']); config(['broadcasting.default' => 'null']);
TenancyBroadcastManager::$tenantBroadcasters[] = 'null'; TenancyBroadcastManager::$tenantBroadcasters[] = 'null';

View file

@ -2,16 +2,14 @@
declare(strict_types=1); declare(strict_types=1);
use Stancl\Tenancy\Tests\Etc\Tenant;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper;
use Stancl\Tenancy\Events\TenancyInitialized; use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Tests\Etc\Tenant; use Stancl\Tenancy\Bootstrappers\CacheTagsBootstrapper;
beforeEach(function () { beforeEach(function () {
config(['tenancy.bootstrappers' => [ config(['tenancy.bootstrappers' => [CacheTagsBootstrapper::class]]);
CacheTenancyBootstrapper::class,
]]);
Event::listen(TenancyInitialized::class, BootstrapTenancy::class); Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
}); });
@ -60,7 +58,7 @@ test('tags separate cache well enough', function () {
$tenant2 = Tenant::create(); $tenant2 = Tenant::create();
tenancy()->initialize($tenant2); tenancy()->initialize($tenant2);
pest()->assertNotSame('bar', cache()->get('foo')); expect(cache('foo'))->not()->toBe('bar');
cache()->put('foo', 'xyz', 1); cache()->put('foo', 'xyz', 1);
expect(cache()->get('foo'))->toBe('xyz'); expect(cache()->get('foo'))->toBe('xyz');
@ -76,7 +74,7 @@ test('invoking the cache helper works', function () {
$tenant2 = Tenant::create(); $tenant2 = Tenant::create();
tenancy()->initialize($tenant2); tenancy()->initialize($tenant2);
pest()->assertNotSame('bar', cache('foo')); expect(cache('foo'))->not()->toBe('bar');
cache(['foo' => 'xyz'], 1); cache(['foo' => 'xyz'], 1);
expect(cache('foo'))->toBe('xyz'); expect(cache('foo'))->toBe('xyz');

View file

@ -12,6 +12,8 @@ use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
use Stancl\Tenancy\Resolvers\DomainTenantResolver; use Stancl\Tenancy\Resolvers\DomainTenantResolver;
beforeEach(function () { beforeEach(function () {
InitializeTenancyByDomain::$onFail = null;
Route::group([ Route::group([
'middleware' => InitializeTenancyByDomain::class, 'middleware' => InitializeTenancyByDomain::class,
], function () { ], function () {
@ -23,6 +25,10 @@ beforeEach(function () {
config(['tenancy.models.tenant' => DomainTenant::class]); config(['tenancy.models.tenant' => DomainTenant::class]);
}); });
afterEach(function () {
InitializeTenancyByDomain::$onFail = null;
});
test('tenant can be identified using hostname', function () { test('tenant can be identified using hostname', function () {
$tenant = DomainTenant::create(); $tenant = DomainTenant::create();
@ -89,9 +95,6 @@ test('onfail logic can be customized', function () {
}); });
test('throw correct exception when onFail is null and universal routes are enabled', function () { test('throw correct exception when onFail is null and universal routes are enabled', function () {
// un-define onFail logic
InitializeTenancyByDomain::$onFail = null;
// Enable UniversalRoute feature // Enable UniversalRoute feature
Route::middlewareGroup('universal', []); Route::middlewareGroup('universal', []);

View file

@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Tests\Etc;
use Illuminate\Cache\Repository;
class CacheService
{
public function __construct(
protected Repository $cache
) {
}
public function handle(): void
{
if (tenancy()->initialized) {
$this->cache->put('key', tenant()->getTenantKey());
} else {
$this->cache->put('key', 'central-value');
}
}
}

View file

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Tests\Etc;
use Illuminate\Cache\CacheManager;
use Illuminate\Cache\Repository;
class SpecificCacheStoreService
{
public Repository $cache;
public function __construct(CacheManager $cacheManager, string $cacheStoreName)
{
$this->cache = $cacheManager->store($cacheStoreName);
}
public function handle(): void
{
if (tenancy()->initialized) {
$this->cache->put('key', tenant()->getTenantKey());
} else {
$this->cache->put('key', 'central-value');
}
}
}

View file

@ -2,25 +2,32 @@
declare(strict_types=1); declare(strict_types=1);
use Stancl\Tenancy\CacheManager;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper;
use Stancl\Tenancy\Events\TenancyEnded; use Stancl\Tenancy\Events\TenancyEnded;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Facades\GlobalCache; use Stancl\Tenancy\Facades\GlobalCache;
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\Tests\Etc\Tenant; use Stancl\Tenancy\Bootstrappers\CacheTagsBootstrapper;
use Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper;
beforeEach(function () { beforeEach(function () {
config(['tenancy.bootstrappers' => [ config(['cache.default' => $cacheDriver = 'redis']);
CacheTenancyBootstrapper::class, PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver];
]]);
Event::listen(TenancyInitialized::class, BootstrapTenancy::class); Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
Event::listen(TenancyEnded::class, RevertToCentralContext::class); Event::listen(TenancyEnded::class, RevertToCentralContext::class);
}); });
test('global cache manager stores data in global cache', function () { afterEach(function () {
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [];
});
test('global cache manager stores data in global cache', function (string $bootstrapper) {
config(['tenancy.bootstrappers' => [$bootstrapper]]);
expect(cache('foo'))->toBe(null); expect(cache('foo'))->toBe(null);
GlobalCache::put(['foo' => 'bar'], 1); GlobalCache::put(['foo' => 'bar'], 1);
expect(GlobalCache::get('foo'))->toBe('bar'); expect(GlobalCache::get('foo'))->toBe('bar');
@ -48,9 +55,14 @@ test('global cache manager stores data in global cache', function () {
tenancy()->initialize($tenant1); tenancy()->initialize($tenant1);
expect(cache('def'))->toBe('ghi'); expect(cache('def'))->toBe('ghi');
}); })->with([
CacheTagsBootstrapper::class,
PrefixCacheTenancyBootstrapper::class,
]);
test('the global_cache helper supports the same syntax as the cache helper', function (string $bootstrapper) {
config(['tenancy.bootstrappers' => [$bootstrapper]]);
test('the global_cache helper supports the same syntax as the cache helper', function () {
$tenant = Tenant::create(); $tenant = Tenant::create();
$tenant->enter(); $tenant->enter();
@ -63,4 +75,7 @@ test('the global_cache helper supports the same syntax as the cache helper', fun
expect(global_cache()->get('foo'))->toBe('baz'); expect(global_cache()->get('foo'))->toBe('baz');
expect(cache('foo'))->toBe(null); // tenant cache is not affected expect(cache('foo'))->toBe(null); // tenant cache is not affected
}); })->with([
CacheTagsBootstrapper::class,
PrefixCacheTenancyBootstrapper::class,
]);

View file

@ -12,11 +12,16 @@ use Stancl\Tenancy\Bootstrappers\MailTenancyBootstrapper;
beforeEach(function() { beforeEach(function() {
config(['mail.default' => 'smtp']); config(['mail.default' => 'smtp']);
MailTenancyBootstrapper::$credentialsMap = [];
Event::listen(TenancyInitialized::class, BootstrapTenancy::class); Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
Event::listen(TenancyEnded::class, RevertToCentralContext::class); Event::listen(TenancyEnded::class, RevertToCentralContext::class);
}); });
afterEach(function () {
MailTenancyBootstrapper::$credentialsMap = [];
});
// Initialize tenancy as $tenant and assert that the smtp mailer's transport has the correct password // Initialize tenancy as $tenant and assert that the smtp mailer's transport has the correct password
function assertMailerTransportUsesPassword(string|null $password) { function assertMailerTransportUsesPassword(string|null $password) {
$manager = app(MailManager::class); $manager = app(MailManager::class);

View file

@ -0,0 +1,324 @@
<?php
declare(strict_types=1);
use Illuminate\Cache\CacheManager;
use Illuminate\Support\Facades\Event;
use Stancl\Tenancy\Events\TenancyEnded;
use Stancl\Tenancy\Tests\Etc\CacheService;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Tests\Etc\SpecificCacheStoreService;
use Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper;
beforeEach(function () {
config([
'tenancy.bootstrappers' => [
PrefixCacheTenancyBootstrapper::class
],
'cache.default' => $cacheDriver = 'redis',
'cache.stores.' . $secondCacheDriver = 'redis2' => config('cache.stores.redis'),
]);
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$cacheDriver, $secondCacheDriver];
PrefixCacheTenancyBootstrapper::$prefixGenerator = null;
Event::listen(TenancyInitialized::class, BootstrapTenancy::class);
Event::listen(TenancyEnded::class, RevertToCentralContext::class);
});
afterEach(function () {
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [];
PrefixCacheTenancyBootstrapper::$prefixGenerator = null;
});
test('correct cache prefix is used in all contexts', function () {
$originalPrefix = config('cache.prefix');
$prefixBase = config('tenancy.cache.prefix_base');
$getDefaultPrefixForTenant = fn (Tenant $tenant) => $originalPrefix . $prefixBase . $tenant->getTenantKey();
$bootstrapper = app(PrefixCacheTenancyBootstrapper::class);
$expectCachePrefixToBe = function (string $prefix) {
expect($prefix . ':') // RedisStore suffixes prefix with ':'
->toBe(app('cache')->getPrefix())
->toBe(app('cache.store')->getPrefix())
->toBe(cache()->getPrefix())
->toBe(cache()->store('redis2')->getPrefix()); // Non-default cache stores specified in $tenantCacheStores are prefixed too
};
$expectCachePrefixToBe($originalPrefix);
$tenant1 = Tenant::create();
$tenant2 = Tenant::create();
tenancy()->initialize($tenant1);
cache()->set('key', 'tenantone-value');
$tenantOnePrefix = $getDefaultPrefixForTenant($tenant1);
$expectCachePrefixToBe($tenantOnePrefix);
expect($bootstrapper->generatePrefix($tenant1))->toBe($tenantOnePrefix);
tenancy()->initialize($tenant2);
cache()->set('key', 'tenanttwo-value');
$tenantTwoPrefix = $getDefaultPrefixForTenant($tenant2);
$expectCachePrefixToBe($tenantTwoPrefix);
expect($bootstrapper->generatePrefix($tenant2))->toBe($tenantTwoPrefix);
// Prefix gets reverted to default after ending tenancy
tenancy()->end();
$expectCachePrefixToBe($originalPrefix);
// Assert tenant's data is accessible using the prefix from the central context
config(['cache.prefix' => null]); // stop prefixing cache keys in central so we can provide prefix manually
app('cache')->forgetDriver(config('cache.default'));
expect(cache($tenantOnePrefix . ':key'))->toBe('tenantone-value');
expect(cache($tenantTwoPrefix . ':key'))->toBe('tenanttwo-value');
});
test('cache is persisted when reidentification is used', function () {
$tenant1 = Tenant::create();
$tenant2 = Tenant::create();
tenancy()->initialize($tenant1);
cache(['foo' => 'bar']);
expect(cache('foo'))->toBe('bar');
tenancy()->initialize($tenant2);
expect(cache('foo'))->toBeNull();
tenancy()->end();
tenancy()->initialize($tenant1);
expect(cache('foo'))->toBe('bar');
});
test('prefixing separates the cache', function () {
$tenant1 = Tenant::create();
tenancy()->initialize($tenant1);
cache()->put('foo', 'bar');
expect(cache()->get('foo'))->toBe('bar');
$tenant2 = Tenant::create();
tenancy()->initialize($tenant2);
expect(cache()->get('foo'))->toBeNull();
cache()->put('foo', 'xyz');
expect(cache()->get('foo'))->toBe('xyz');
tenancy()->initialize($tenant1);
expect(cache()->get('foo'))->toBe('bar');
});
test('central cache is persisted', function () {
cache()->put('key', 'central');
$tenant1 = Tenant::create();
tenancy()->initialize($tenant1);
expect(cache('key'))->toBeNull();
cache()->put('key', 'tenant');
expect(cache()->get('key'))->toBe('tenant');
tenancy()->end();
cache()->put('key2', 'central-two');
expect(cache()->get('key'))->toBe('central');
expect(cache()->get('key2'))->toBe('central-two');
tenancy()->initialize($tenant1);
expect(cache()->get('key'))->toBe('tenant');
expect(cache()->get('key2'))->toBeNull();
});
test('cache base prefix is customizable', function () {
config([
'tenancy.cache.prefix_base' => $prefixBase = 'custom_'
]);
$originalPrefix = config('cache.prefix');
$tenant1 = Tenant::create();
tenancy()->initialize($tenant1);
expect($originalPrefix . $prefixBase . $tenant1->getTenantKey() . ':')
->toBe(cache()->getPrefix())
->toBe(cache()->store('redis2')->getPrefix()) // Non-default store gets prefixed correctly too
->toBe(app('cache')->getPrefix())
->toBe(app('cache.store')->getPrefix());
});
test('cache is prefixed correctly when using a repository injected in a singleton', function () {
$this->app->singleton(CacheService::class);
expect(cache('key'))->toBeNull();
$this->app->make(CacheService::class)->handle();
expect(cache('key'))->toBe('central-value');
$tenant1 = Tenant::create();
$tenant2 = Tenant::create();
tenancy()->initialize($tenant1);
expect(cache('key'))->toBeNull();
$this->app->make(CacheService::class)->handle();
expect(cache('key'))->toBe($tenant1->getTenantKey());
tenancy()->initialize($tenant2);
expect(cache('key'))->toBeNull();
$this->app->make(CacheService::class)->handle();
expect(cache('key'))->toBe($tenant2->getTenantKey());
tenancy()->end();
expect(cache('key'))->toBe('central-value');
});
test('specific central cache store can be used inside a service', function () {
// Make sure 'redis' (the default store) is the only prefixed store
PrefixCacheTenancyBootstrapper::$tenantCacheStores = ['redis'];
// Name of the non-default, central cache store that we'll use using cache()->store($cacheStore)
$cacheStore = 'redis2';
// Service uses the 'redis2' store which is central/not prefixed (not present in PrefixCacheTenancyBootstrapper::$tenantCacheStores)
// The service's handle() method sets the value of the cache key 'key' to the current tenant key
// Or to 'central-value' if tenancy isn't initialized
$this->app->singleton(SpecificCacheStoreService::class, function() use ($cacheStore) {
return new SpecificCacheStoreService($this->app->make(CacheManager::class), $cacheStore);
});
$this->app->make(SpecificCacheStoreService::class)->handle();
expect(cache()->store($cacheStore)->get('key'))->toBe('central-value');
$tenant1 = Tenant::create();
$tenant2 = Tenant::create();
tenancy()->initialize($tenant1);
// The store isn't prefixed, so the cache isn't separated the values persist from one context to another
// Also assert that the value of 'key' is set correctly inside SpecificCacheStoreService according to the current context
expect(cache()->store($cacheStore)->get('key'))->toBe('central-value');
$this->app->make(SpecificCacheStoreService::class)->handle();
expect(cache()->store($cacheStore)->get('key'))->toBe($tenant1->getTenantKey());
tenancy()->initialize($tenant2);
expect(cache()->store($cacheStore)->get('key'))->toBe($tenant1->getTenantKey());
$this->app->make(SpecificCacheStoreService::class)->handle();
expect(cache()->store($cacheStore)->get('key'))->toBe($tenant2->getTenantKey());
tenancy()->end();
// We last executed handle() in tenant2's context, so the value should persist as tenant2's id
expect(cache()->store($cacheStore)->get('key'))->toBe($tenant2->getTenantKey());
});
test('only the stores specified in tenantCacheStores get prefixed', function () {
// Make sure the currently used store ('redis') is the only store in $tenantCacheStores
PrefixCacheTenancyBootstrapper::$tenantCacheStores = [$prefixedStore = 'redis'];
$centralValue = 'central-value';
$assertStoreIsNotPrefixed = function (string $unprefixedStore) use ($prefixedStore, $centralValue) {
// Switch to the unprefixed store
config(['cache.default' => $unprefixedStore]);
expect(cache('key'))->toBe($centralValue);
// Switch back to the prefixed store
config(['cache.default' => $prefixedStore]);
};
$this->app->singleton(CacheService::class);
$this->app->make(CacheService::class)->handle();
expect(cache('key'))->toBe($centralValue);
$tenant1 = Tenant::create();
$tenant2 = Tenant::create();
tenancy()->initialize($tenant1);
expect(cache('key'))->toBeNull();
$this->app->make(CacheService::class)->handle();
expect(cache('key'))->toBe($tenant1->getTenantKey());
$assertStoreIsNotPrefixed('redis2');
tenancy()->initialize($tenant2);
expect(cache('key'))->toBeNull();
$this->app->make(CacheService::class)->handle();
expect(cache('key'))->toBe($tenant2->getTenantKey());
$assertStoreIsNotPrefixed('redis2');
tenancy()->end();
expect(cache('key'))->toBe($centralValue);
$this->app->make(CacheService::class)->handle();
expect(cache('key'))->toBe($centralValue);
});
test('non default stores get prefixed too when specified in tenantCacheStores', function () {
// In beforeEach, we set $tenantCacheStores to ['redis', 'redis2']
// Make 'redis' the default cache driver
config(['cache.default' => 'redis']);
$tenant = Tenant::create();
$defaultPrefix = cache()->store()->getPrefix();
$bootstrapper = app(PrefixCacheTenancyBootstrapper::class);
// The prefix is the same for both drivers in the central context
expect(cache()->store('redis')->getPrefix())->toBe($defaultPrefix);
expect(cache()->store('redis2')->getPrefix())->toBe($defaultPrefix);
tenancy()->initialize($tenant);
// We didn't add a prefix generator for our 'redis2' driver, so we expect the prefix to be generated using the 'default' generator
expect($bootstrapper->generatePrefix($tenant) . ':')
->toBe(cache()->getPrefix())
->toBe(cache()->store('redis2')->getPrefix()); // Non-default store
tenancy()->end();
});
test('cache store prefix generation can be customized', function() {
// Use custom prefix generator
PrefixCacheTenancyBootstrapper::generatePrefixUsing($customPrefixGenerator = function (Tenant $tenant) {
return 'redis_tenant_cache_' . $tenant->getTenantKey();
});
expect(PrefixCacheTenancyBootstrapper::$prefixGenerator)->toBe($customPrefixGenerator);
expect(app(PrefixCacheTenancyBootstrapper::class)->generatePrefix($tenant = Tenant::create()))
->toBe($customPrefixGenerator($tenant));
tenancy()->initialize($tenant = Tenant::create());
// Expect the 'redis' store to use the prefix generated by the custom generator
expect($customPrefixGenerator($tenant) . ':')
->toBe(cache()->getPrefix())
->toBe(cache()->store('redis2')->getPrefix()) // Non-default cache stores specified in $tenantCacheStores are prefixed too
->toBe(app('cache')->getPrefix())
->toBe(app('cache.store')->getPrefix());
tenancy()->end();
});
test('stores get prefixed using the default way if no prefix generator is specified', function() {
$originalPrefix = config('cache.prefix');
$prefixBase = config('tenancy.cache.prefix_base');
$tenant = Tenant::create();
$defaultPrefix = $originalPrefix . $prefixBase . $tenant->getTenantKey();
// Don't specify a prefix generator
// Let the prefix get created using the default approach
tenancy()->initialize($tenant);
// All stores use the default way of generating the prefix when the prefix generator isn't specified
expect($defaultPrefix . ':')
->toBe(app(PrefixCacheTenancyBootstrapper::class)->generatePrefix($tenant) . ':')
->toBe(cache()->getPrefix()) // Get prefix of the default store ('redis')
->toBe(cache()->store('redis2')->getPrefix());
tenancy()->end();
});

View file

@ -110,6 +110,8 @@ test('tenancy is initialized inside queues', function (bool $shouldEndTenancy) {
pest()->artisan('queue:work --once'); pest()->artisan('queue:work --once');
expect(! tenancy()->initialized)->toBe($shouldEndTenancy);
expect(DB::connection('central')->table('failed_jobs')->count())->toBe(0); expect(DB::connection('central')->table('failed_jobs')->count())->toBe(0);
expect(pest()->valuestore->get('tenant_id'))->toBe('The current tenant id is: ' . $tenant->id); expect(pest()->valuestore->get('tenant_id'))->toBe('The current tenant id is: ' . $tenant->id);
@ -117,7 +119,7 @@ test('tenancy is initialized inside queues', function (bool $shouldEndTenancy) {
$tenant->run(function () use ($user) { $tenant->run(function () use ($user) {
expect($user->fresh()->name)->toBe('Bar'); expect($user->fresh()->name)->toBe('Bar');
}); });
})->with([true, false]);; })->with([true, false]);
test('tenancy is initialized when retrying jobs', function (bool $shouldEndTenancy) { test('tenancy is initialized when retrying jobs', function (bool $shouldEndTenancy) {
withFailedJobs(); withFailedJobs();
@ -144,12 +146,21 @@ test('tenancy is initialized when retrying jobs', function (bool $shouldEndTenan
pest()->artisan('queue:work --once'); pest()->artisan('queue:work --once');
expect(! tenancy()->initialized)->toBe($shouldEndTenancy);
expect(DB::connection('central')->table('failed_jobs')->count())->toBe(1); expect(DB::connection('central')->table('failed_jobs')->count())->toBe(1);
expect(pest()->valuestore->get('tenant_id'))->toBeNull(); // job failed expect(pest()->valuestore->get('tenant_id'))->toBeNull(); // job failed
pest()->artisan('queue:retry all'); pest()->artisan('queue:retry all');
if ($shouldEndTenancy) {
tenancy()->end();
}
pest()->artisan('queue:work --once'); pest()->artisan('queue:work --once');
expect(! tenancy()->initialized)->toBe($shouldEndTenancy);
expect(DB::connection('central')->table('failed_jobs')->count())->toBe(0); expect(DB::connection('central')->table('failed_jobs')->count())->toBe(0);
expect(pest()->valuestore->get('tenant_id'))->toBe('The current tenant id is: ' . $tenant->id); // job succeeded expect(pest()->valuestore->get('tenant_id'))->toBe('The current tenant id is: ' . $tenant->id); // job succeeded
@ -182,6 +193,22 @@ test('the tenant used by the job doesnt change when the current tenant changes',
expect(pest()->valuestore->get('tenant_id'))->toBe('The current tenant id is: acme'); expect(pest()->valuestore->get('tenant_id'))->toBe('The current tenant id is: acme');
}); });
test('tenant connections do not persist after tenant jobs get processed', function() {
withTenantDatabases();
$tenant = Tenant::create();
tenancy()->initialize($tenant);
dispatch(new TestJob(pest()->valuestore));
tenancy()->end();
pest()->artisan('queue:work --once');
expect(collect(DB::select('SHOW FULL PROCESSLIST'))->pluck('db'))->not()->toContain($tenant->database()->getName());
});
function createValueStore(): void function createValueStore(): void
{ {
$valueStorePath = __DIR__ . '/Etc/tmp/queuetest.json'; $valueStorePath = __DIR__ . '/Etc/tmp/queuetest.json';

View file

@ -9,6 +9,7 @@ use Dotenv\Dotenv;
use Stancl\Tenancy\Facades\Tenancy; use Stancl\Tenancy\Facades\Tenancy;
use Stancl\Tenancy\Tests\Etc\Tenant; use Stancl\Tenancy\Tests\Etc\Tenant;
use Illuminate\Support\Facades\Redis; use Illuminate\Support\Facades\Redis;
use Stancl\Tenancy\Bootstrappers\PrefixCacheTenancyBootstrapper;
use Illuminate\Foundation\Application; use Illuminate\Foundation\Application;
use Stancl\Tenancy\Facades\GlobalCache; use Stancl\Tenancy\Facades\GlobalCache;
use Stancl\Tenancy\TenancyServiceProvider; use Stancl\Tenancy\TenancyServiceProvider;
@ -118,6 +119,7 @@ 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(PrefixCacheTenancyBootstrapper::class); // todo (Samuel) use proper approach eg config for singleton registration
$app->singleton(BroadcastTenancyBootstrapper::class); $app->singleton(BroadcastTenancyBootstrapper::class);
$app->singleton(MailTenancyBootstrapper::class); $app->singleton(MailTenancyBootstrapper::class);
$app->singleton(UrlTenancyBootstrapper::class); $app->singleton(UrlTenancyBootstrapper::class);