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

Session scoping & tenant() cleanup

This commit is contained in:
Samuel Štancl 2020-05-15 11:07:42 +02:00
parent c8f9a82745
commit 05d6383b99
6 changed files with 125 additions and 4 deletions

View file

@ -46,7 +46,7 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper
}
// Tenancy is already initialized for the tenant (e.g. dispatchNow was used)
if (tenancy()->initialized && tenant('id') === $tenantId) {
if (tenancy()->initialized && tenant()->getTenantKey() === $tenantId) {
return;
}
@ -87,7 +87,7 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper
return [];
}
$id = tenant('id');
$id = tenant()->getTenantKey();
return [
'tenant_id' => $id,

View file

@ -17,7 +17,7 @@ class CacheManager extends BaseCacheManager
*/
public function __call($method, $parameters)
{
$tags = [config('tenancy.cache.tag_base') . tenant('id')];
$tags = [config('tenancy.cache.tag_base') . tenant()->getTenantKey()];
if ($method === 'tags') {
if (count($parameters) !== 1) {

View file

@ -0,0 +1,13 @@
<?php
namespace Stancl\Tenancy\Exceptions;
use Exception;
class TenancyNotInitializedException extends Exception
{
public function __construct($message = "")
{
parent::__construct($message ?: 'Tenancy is not initialized.');
}
}

View file

@ -37,7 +37,7 @@ class UserImpersonation implements Feature
{
$token = $token instanceof ImpersonationToken ? $token : ImpersonationToken::findOrFail($token);
if (((string) $token->tenant_id) !== ((string) tenant('id'))) {
if (((string) $token->tenant_id) !== ((string) tenant()->getTenantKey())) {
abort(403);
}

View file

@ -0,0 +1,29 @@
<?php
namespace Stancl\Tenancy\Middleware;
use Closure;
use Illuminate\Http\Request;
use Stancl\Tenancy\Exceptions\TenancyNotInitializedException;
class ScopeSessions
{
public static $tenantIdKey = '_tenant_id';
public function handle(Request $request, Closure $next)
{
if (! tenancy()->initialized) {
throw new TenancyNotInitializedException('Tenancy needs to be initialized before the session scoping middleware is executed');
}
if (! $request->session()->has(static::$tenantIdKey)) {
$request->session()->put(static::$tenantIdKey, tenant()->getTenantKey());
} else {
if ($request->session()->get(static::$tenantIdKey) !== tenant()->getTenantKey()) {
abort(403);
}
}
return $next($request);
}
}