mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 11:44:04 +00:00
Merge branch '2.x' into laravel-7-support
This commit is contained in:
commit
febca87dfc
5 changed files with 135 additions and 4 deletions
|
|
@ -16,7 +16,7 @@ return [
|
||||||
'tenants' => 'tenants',
|
'tenants' => 'tenants',
|
||||||
'domains' => 'domains',
|
'domains' => 'domains',
|
||||||
],
|
],
|
||||||
'cache_store' => false, // What store should be used to cache tenant resolution. Set to false to disable cache, null to use default store, or a string with a specific cache store name.
|
'cache_store' => null, // What store should be used to cache tenant resolution. Set to null to disable cache or a string with a specific cache store name.
|
||||||
'cache_ttl' => 3600, // seconds
|
'cache_ttl' => 3600, // seconds
|
||||||
],
|
],
|
||||||
'redis' => [
|
'redis' => [
|
||||||
|
|
|
||||||
70
src/Middleware/InitializeTenancyByRequestData.php
Normal file
70
src/Middleware/InitializeTenancyByRequestData.php
Normal file
|
|
@ -0,0 +1,70 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Middleware;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
|
||||||
|
|
||||||
|
class InitializeTenancyByRequestData
|
||||||
|
{
|
||||||
|
/** @var string|null */
|
||||||
|
protected $header;
|
||||||
|
|
||||||
|
/** @var string|null */
|
||||||
|
protected $queryParameter;
|
||||||
|
|
||||||
|
/** @var callable */
|
||||||
|
protected $onFail;
|
||||||
|
|
||||||
|
public function __construct(?string $header = 'X-Tenant', ?string $queryParameter = 'tenant', callable $onFail = null)
|
||||||
|
{
|
||||||
|
$this->header = $header;
|
||||||
|
$this->queryParameter = $queryParameter;
|
||||||
|
$this->onFail = $onFail ?? function ($e) {
|
||||||
|
throw $e;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle an incoming request.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @param \Closure $next
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function handle($request, Closure $next)
|
||||||
|
{
|
||||||
|
if ($request->method() !== 'OPTIONS') {
|
||||||
|
try {
|
||||||
|
$this->initializeTenancy($request);
|
||||||
|
} catch (TenantCouldNotBeIdentifiedException $e) {
|
||||||
|
($this->onFail)($e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $next($request);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function initializeTenancy(Request $request)
|
||||||
|
{
|
||||||
|
if (tenancy()->initialized) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$tenant = null;
|
||||||
|
if ($this->header && $request->hasHeader($this->header)) {
|
||||||
|
$tenant = $request->header($this->header);
|
||||||
|
} elseif ($this->queryParameter && $request->has($this->queryParameter)) {
|
||||||
|
$tenant = $request->get($this->queryParameter);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $tenant) {
|
||||||
|
throw new TenantCouldNotBeIdentifiedException($request->getHost());
|
||||||
|
}
|
||||||
|
|
||||||
|
tenancy()->initialize(tenancy()->find($tenant));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -251,7 +251,6 @@ class DatabaseStorageDriver implements StorageDriver, CanDeleteKeys, CanFindByAn
|
||||||
|
|
||||||
public function usesCache(): bool
|
public function usesCache(): bool
|
||||||
{
|
{
|
||||||
// null is also truthy here
|
return $this->app['config']['tenancy.storage_drivers.db.cache_store'] !== null;
|
||||||
return $this->app['config']['tenancy.storage_drivers.db.cache_store'] !== false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ class CachedResolverTest extends TestCase
|
||||||
$this->markTestSkipped('This test is only relevant for the DB storage driver.');
|
$this->markTestSkipped('This test is only relevant for the DB storage driver.');
|
||||||
}
|
}
|
||||||
|
|
||||||
config(['tenancy.storage_drivers.db.cache_store' => null]); // default driver
|
config(['tenancy.storage_drivers.db.cache_store' => config('cache.default')]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
|
|
|
||||||
62
tests/RequestDataIdentificationTest.php
Normal file
62
tests/RequestDataIdentificationTest.php
Normal file
|
|
@ -0,0 +1,62 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Tests;
|
||||||
|
|
||||||
|
use Illuminate\Support\Facades\Route;
|
||||||
|
use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData;
|
||||||
|
use Stancl\Tenancy\Tenant;
|
||||||
|
|
||||||
|
class RequestDataIdentificationTest extends TestCase
|
||||||
|
{
|
||||||
|
public $autoCreateTenant = false;
|
||||||
|
public $autoInitTenancy = false;
|
||||||
|
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
config([
|
||||||
|
'tenancy.exempt_domains' => [
|
||||||
|
'localhost',
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
Route::middleware(InitializeTenancyByRequestData::class)->get('/test', function () {
|
||||||
|
return 'Tenant id: ' . tenant('id');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function header_identification_works()
|
||||||
|
{
|
||||||
|
$this->app->bind(InitializeTenancyByRequestData::class, function () {
|
||||||
|
return new InitializeTenancyByRequestData('X-Tenant');
|
||||||
|
});
|
||||||
|
$tenant = Tenant::new()->save();
|
||||||
|
$tenant2 = Tenant::new()->save();
|
||||||
|
|
||||||
|
$this
|
||||||
|
->withoutExceptionHandling()
|
||||||
|
->get('test', [
|
||||||
|
'X-Tenant' => $tenant->id,
|
||||||
|
])
|
||||||
|
->assertSee($tenant->id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function query_parameter_identification_works()
|
||||||
|
{
|
||||||
|
$this->app->bind(InitializeTenancyByRequestData::class, function () {
|
||||||
|
return new InitializeTenancyByRequestData(null, 'tenant');
|
||||||
|
});
|
||||||
|
$tenant = Tenant::new()->save();
|
||||||
|
$tenant2 = Tenant::new()->save();
|
||||||
|
|
||||||
|
$this
|
||||||
|
->withoutExceptionHandling()
|
||||||
|
->get('test?tenant=' . $tenant->id)
|
||||||
|
->assertSee($tenant->id);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue