Skip to content

Commit a96e4b4

Browse files
committed
Fix tenant resolution to check if value is instance of tenant model
1 parent 9e1db16 commit a96e4b4

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

src/Http/Middleware/SetTenant.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ class SetTenant
1111
/**
1212
* Handle an incoming request.
1313
*
14-
* @param \Illuminate\Http\Request $request
15-
* @param \Closure $next
16-
* @param string|null $guard
14+
* @param \Illuminate\Http\Request $request
15+
* @param string|null $guard
1716
* @return mixed
1817
*/
1918
public function handle($request, Closure $next, $guard = null)
@@ -27,7 +26,7 @@ public function handle($request, Closure $next, $guard = null)
2726

2827
$options = tenancy()->config()->getOption('routes.middleware.set_tenant');
2928

30-
list('in_tenant' => $inTenant, 'in_header' => $inHeader) = $options;
29+
['in_tenant' => $inTenant, 'in_header' => $inHeader] = $options;
3130

3231
$subdomainSafeRoutes = tenancy()->config()->getOption('routes.subdomain_safe_routes');
3332

@@ -41,7 +40,7 @@ public function handle($request, Closure $next, $guard = null)
4140

4241
$tenant = tenancy()->resolveTenant($value, $subdomainSafeCheck);
4342

44-
if (!$tenant) {
43+
if (! $tenant) {
4544
return config('tenancy.redirect.abort') ? abort(404) : redirect(config('tenancy.redirect.fallback_url'));
4645
}
4746

@@ -63,13 +62,10 @@ public function handle($request, Closure $next, $guard = null)
6362
/**
6463
* Check if current user is authorized to access tenant.
6564
*
66-
* @param $request
67-
* @param $inTenant
68-
* @param $tenant
6965
* @return bool
7066
*/
7167
protected function cannotAccessTenant($request, $inTenant, $tenant)
7268
{
73-
return $inTenant && !$tenant->users->contains($request->user());
69+
return $inTenant && ! $tenant->users->contains($request->user());
7470
}
7571
}

src/Tenancy.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,18 @@ public function subdomainSafeCheck()
123123
/**
124124
* Resolve the tenant.
125125
*
126-
* @param int|string $value
126+
* @param int|string|\Illuminate\Database\Eloquent\Model $value
127127
* @param bool $ignoreSubdomainCheck
128128
* @return \Illuminate\Database\Eloquent\Model|null
129129
*/
130130
public function resolveTenant($value, $ignoreSubdomainCheck = false)
131131
{
132132
$model = $this->tenantModel();
133133

134+
if ($value instanceof $model) {
135+
return $value;
136+
}
137+
134138
if (tenancy()->config()->getOption('routes.subdomain') && $ignoreSubdomainCheck == false) {
135139
$key = tenancy()->config()->getOption('routes.subdomain_key', 'domain');
136140
} else {

0 commit comments

Comments
 (0)