mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 11:14:03 +00:00
Add request origin tenant resolver
This commit is contained in:
parent
27e9fb4a69
commit
8852a48373
5 changed files with 116 additions and 0 deletions
|
|
@ -14,6 +14,7 @@ trait InvalidatesResolverCache
|
|||
Resolvers\DomainTenantResolver::class,
|
||||
Resolvers\PathTenantResolver::class,
|
||||
Resolvers\RequestDataTenantResolver::class,
|
||||
Resolvers\RequestOriginTenantResolver::class
|
||||
];
|
||||
|
||||
public static function bootInvalidatesResolverCache()
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ trait InvalidatesTenantsResolverCache
|
|||
Resolvers\DomainTenantResolver::class,
|
||||
Resolvers\PathTenantResolver::class,
|
||||
Resolvers\RequestDataTenantResolver::class,
|
||||
Resolvers\RequestOriginTenantResolver::class
|
||||
];
|
||||
|
||||
public static function bootInvalidatesTenantsResolverCache()
|
||||
|
|
|
|||
|
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Exceptions;
|
||||
|
||||
use Facade\IgnitionContracts\BaseSolution;
|
||||
use Facade\IgnitionContracts\ProvidesSolution;
|
||||
use Facade\IgnitionContracts\Solution;
|
||||
use Stancl\Tenancy\Contracts\TenantCouldNotBeIdentifiedException;
|
||||
|
||||
class TenantCouldNotBeIdentifiedByRequestOriginException extends TenantCouldNotBeIdentifiedException implements ProvidesSolution
|
||||
{
|
||||
public function __construct($tenant_id)
|
||||
{
|
||||
parent::__construct("Tenant could not be identified by request origin with payload: $tenant_id");
|
||||
}
|
||||
|
||||
public function getSolution(): Solution
|
||||
{
|
||||
return BaseSolution::create('Tenant could not be identified with this request origin')
|
||||
->setSolutionDescription('The request needs to originate from the same domain as the tenant.');
|
||||
}
|
||||
}
|
||||
50
src/Middleware/InitializeTenancyByRequestOrigin.php
Normal file
50
src/Middleware/InitializeTenancyByRequestOrigin.php
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Stancl\Tenancy\Tenancy;
|
||||
use Stancl\Tenancy\Resolvers\RequestOriginTenantResolver;
|
||||
|
||||
class InitializeTenancyByRequestOrigin extends IdentificationMiddleware
|
||||
{
|
||||
/** @var callable|null */
|
||||
public static $onFail;
|
||||
|
||||
/** @var Tenancy */
|
||||
protected $tenancy;
|
||||
|
||||
/** @var TenantResolver */
|
||||
protected $resolver;
|
||||
|
||||
public function __construct(Tenancy $tenancy, RequestOriginTenantResolver $resolver)
|
||||
{
|
||||
$this->tenancy = $tenancy;
|
||||
$this->resolver = $resolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
return $this->initializeTenancy($request, $next, $this->getPayload($request));
|
||||
}
|
||||
|
||||
protected function getPayload(Request $request): ?string
|
||||
{
|
||||
$tenant = null;
|
||||
if ($request->hasHeader('origin')) {
|
||||
$tenant = optional(parse_url($request->headers->get('origin')))['host'];
|
||||
}
|
||||
|
||||
return $tenant;
|
||||
}
|
||||
}
|
||||
40
src/Resolvers/RequestOriginTenantResolver.php
Normal file
40
src/Resolvers/RequestOriginTenantResolver.php
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Resolvers;
|
||||
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedByRequestOriginException;
|
||||
|
||||
class RequestOriginTenantResolver extends Contracts\CachedTenantResolver
|
||||
{
|
||||
/** @var bool */
|
||||
public static $shouldCache = false;
|
||||
|
||||
/** @var int */
|
||||
public static $cacheTTL = 3600; // seconds
|
||||
|
||||
/** @var string|null */
|
||||
public static $cacheStore = null; // default
|
||||
|
||||
public function resolveWithoutCache(...$args): Tenant
|
||||
{
|
||||
$payload = $args[0];
|
||||
|
||||
$domain = config('tenancy.domain_model')::where('domain', $payload)->first();
|
||||
|
||||
if ($domain) {
|
||||
return $domain->tenant;
|
||||
}
|
||||
|
||||
throw new TenantCouldNotBeIdentifiedByRequestOriginException($payload);
|
||||
}
|
||||
|
||||
public function getArgsForTenant(Tenant $tenant): array
|
||||
{
|
||||
return [
|
||||
[$tenant->id],
|
||||
];
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue