Skip to content

Commit b9a8d76

Browse files
committed
Add functionality for custom user defined tenant middleware groups
1 parent a96e4b4 commit b9a8d76

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

config/laravel-multi-tenancy.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,28 @@
261261
// tenant_config
262262
// theme_config
263263
],
264+
265+
/*
266+
*
267+
* Custom tenant middleware to use according to your requirements app wide.
268+
*
269+
* Use \Miracuthbert\Multitenancy\Http\Middleware\SetTenant::class as reference
270+
* when defining custom middleware to suit your needs.
271+
*
272+
* They provide the same function as 'tenant' middleware group (SetTenant::ckass)
273+
* with exception to checking whether the request user can access tenant.
274+
*
275+
* Suitable for use in 'guest' routes when you need to display 'tenant' data
276+
* for public; eg. blogs, cms, etc
277+
*
278+
* Note: 'tenant' middleware group will have top priority compared to values
279+
* registered below. Do not use them on the same routes.
280+
*/
281+
'groups' => [
282+
'tenant.guest' => [
283+
\Miracuthbert\Multitenancy\Http\Middleware\ResolveTenantAsGuest::class,
284+
],
285+
],
264286
],
265287

266288
/*
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Miracuthbert\Multitenancy\Http\Middleware;
4+
5+
use Closure;
6+
use Miracuthbert\Multitenancy\Events\TenantIdentified;
7+
use Miracuthbert\Multitenancy\TenantStore;
8+
9+
class ResolveTenantAsGuest
10+
{
11+
/**
12+
* Handle an incoming request.
13+
*
14+
* @param \Illuminate\Http\Request $request
15+
* @param \Closure $next
16+
* @param string|null $guard
17+
* @return mixed
18+
*/
19+
public function handle($request, Closure $next, $guard = null)
20+
{
21+
$subdomainKey = config('tenancy.routes.subdomain_request_key', 'tenant_domain');
22+
23+
// set default subdomain key if it exists
24+
\Illuminate\Support\Facades\URL::defaults([$subdomainKey => $request->{$subdomainKey}]);
25+
26+
$paramKey = tenancy()->config()->getOption('model.route_key');
27+
28+
$options = tenancy()->config()->getOption('routes.middleware.set_tenant');
29+
30+
list('in_tenant' => $inTenant, 'in_header' => $inHeader) = $options;
31+
32+
$subdomainSafeRoutes = tenancy()->config()->getOption('routes.subdomain_safe_routes');
33+
34+
$subdomainSafeCheck = empty($subdomainSafeRoutes) ? false : $request->routeIs($subdomainSafeRoutes);
35+
36+
if (tenancy()->config()->getOption('routes.subdomain') && empty($subdomainSafeCheck)) {
37+
$value = ($request->{$subdomainKey});
38+
} else {
39+
$value = $inHeader ? $request->header(TenantStore::TENANT_HEADER) : tenancy()->store()->getKey($request->{$paramKey});
40+
}
41+
42+
$tenant = tenancy()->resolveTenant($value, $subdomainSafeCheck);
43+
44+
if (!$tenant) {
45+
return config('tenancy.redirect.abort') ? abort(404) : redirect(config('tenancy.redirect.fallback_url'));
46+
}
47+
48+
event(new TenantIdentified($tenant));
49+
50+
$tenantKey = tenancy()->config()->getOption('model.key');
51+
52+
$request->headers->set(TenantStore::TENANT_HEADER, $tenant->{$tenantKey});
53+
54+
return $next($request);
55+
}
56+
}

src/LaravelMultiTenancyServiceProvider.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ protected function registerMiddleware()
216216
// add tenant middleware group
217217
$router->middlewareGroup('tenant', $tenantMiddleware);
218218

219+
$additionalMiddlewareGroups = config('tenancy.routes.middleware.groups', []);
220+
221+
foreach ($additionalMiddlewareGroups as $key => $middleware) {
222+
$router->middlewareGroup($key, $middleware);
223+
}
224+
219225
// get app's middleware priority list
220226
$mp = $router->middlewarePriority;
221227

@@ -226,7 +232,7 @@ protected function registerMiddleware()
226232
$collection = collect($mp);
227233

228234
// add tenant middleware to collection
229-
$collection->splice($index, 0, $tenantMiddleware)->all();
235+
$collection->splice($index, 0, array_merge($tenantMiddleware, Arr::flatten($additionalMiddlewareGroups)))->all();
230236

231237
// reassign the middleware priority with the new list
232238
$router->middlewarePriority = $collection->all();

0 commit comments

Comments
 (0)