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

Merge branch '2.x' into use-seeder-params-by-seeder

This commit is contained in:
Samuel Štancl 2020-03-17 15:38:23 +01:00
commit 3cf0d7948b
6 changed files with 141 additions and 8 deletions

View file

@ -16,14 +16,16 @@ RUN apt-get update \
&& php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer \
&& mkdir /run/php
RUN apt-get install -y php7.2-redis
RUN apt-get install -y python3
RUN apt-get install -y php7.2-dev php-pear
RUN pecl install redis-4.3.0
RUN echo "extension=redis.so" > /etc/php/7.2/mods-available/redis.ini
RUN ln -sf /etc/php/7.2/mods-available/redis.ini /etc/php/7.2/fpm/conf.d/20-redis.ini
RUN ln -sf /etc/php/7.2/mods-available/redis.ini /etc/php/7.2/cli/conf.d/20-redis.ini
RUN pecl install xdebug
# RUN echo '' > /etc/php/7.2/cli/conf.d/20-xdebug.ini
# RUN echo 'zend_extension=/usr/lib/php/20170718/xdebug.so' >> /etc/php/7.2/cli/php.ini
RUN echo 'zend_extension=/usr/lib/php/20170718/xdebug.so' > /etc/php/7.2/cli/conf.d/20-xdebug.ini
RUN apt-get -y autoremove \

View file

@ -16,7 +16,7 @@ return [
'tenants' => 'tenants',
'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
],
'redis' => [

View 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));
}
}

View file

@ -251,7 +251,6 @@ class DatabaseStorageDriver implements StorageDriver, CanDeleteKeys, CanFindByAn
public function usesCache(): bool
{
// null is also truthy here
return $this->app['config']['tenancy.storage_drivers.db.cache_store'] !== false;
return $this->app['config']['tenancy.storage_drivers.db.cache_store'] !== null;
}
}

View file

@ -21,7 +21,7 @@ class CachedResolverTest extends TestCase
$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 */

View 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);
}
}