Skip to content

Commit c41c178

Browse files
committed
Handle storage and config for tenants
1 parent efc39f7 commit c41c178

7 files changed

Lines changed: 110 additions & 0 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/public/css
66
/storage/*.key
77
/storage/*.index
8+
/storage/tenant_*
89
/vendor
910
/.idea
1011
/.vscode

‎ProcessMaker/Http/Kernel.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Kernel extends HttpKernel
2626
Middleware\BrowserCache::class,
2727
ServerTimingMiddleware::class,
2828
Middleware\FileSizeCheck::class,
29+
Middleware\AddTenantHeaders::class,
2930
];
3031

3132
/**
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace ProcessMaker\Http\Middleware;
4+
5+
use Closure;
6+
use Illuminate\Http\Request;
7+
use Spatie\Multitenancy\Models\Tenant;
8+
9+
class AddTenantHeaders
10+
{
11+
/**
12+
* Handle an incoming request.
13+
*
14+
* @param Request $request
15+
* @param Closure $next
16+
* @return mixed
17+
*/
18+
public function handle(Request $request, Closure $next)
19+
{
20+
$response = $next($request);
21+
22+
$tenant = Tenant::current();
23+
24+
if ($tenant) {
25+
$response->headers->set('x-tenant-id', $tenant->id);
26+
$response->headers->set('x-tenant-name', $tenant->name);
27+
} else {
28+
$response->headers->set('x-tenant-id', 'none');
29+
$response->headers->set('x-tenant-name', 'No tenant');
30+
}
31+
32+
return $response;
33+
}
34+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace ProcessMaker\Multitenancy;
4+
5+
use Illuminate\Foundation\Application;
6+
use Illuminate\Foundation\Bootstrap\LoadConfiguration;
7+
use Spatie\Multitenancy\Contracts\IsTenant;
8+
use Spatie\Multitenancy\Tasks\SwitchTenantTask;
9+
10+
class SwitchTenant implements SwitchTenantTask
11+
{
12+
/**
13+
* Make the given tenant current.
14+
*
15+
* @param IsTenant $tenant
16+
* @return void
17+
*/
18+
public function makeCurrent(IsTenant $tenant): void
19+
{
20+
$app = app();
21+
22+
// Set the tenant-specific storage path
23+
$tenantStoragePath = base_path('storage/tenant_' . $tenant->id);
24+
25+
$app->setStoragePath($tenantStoragePath);
26+
27+
// Create the tenant storage directory if it doesn't exist
28+
29+
// TODO: Move these to somewhere else - should not be run on every request
30+
if (!file_exists($tenantStoragePath)) {
31+
mkdir($tenantStoragePath, 0755, true);
32+
}
33+
$tennantCacheFolder = base_path('bootstrap/cache/tenant_' . $tenant->id);
34+
if (!file_exists($tennantCacheFolder)) {
35+
mkdir($tennantCacheFolder, 0755, true);
36+
}
37+
38+
// Reload the config
39+
putenv('APP_CONFIG_CACHE=' . $tennantCacheFolder . '/config.php');
40+
(new LoadConfiguration())->bootstrap($app);
41+
42+
// Set the app.url config
43+
$app->config->set('app.url', $app->config->get('app.protocol') . '://' . $tenant->domain);
44+
45+
if ($tenant->config_overrides) {
46+
foreach ($tenant->config_overrides as $key => $value) {
47+
$app->config->set($key, $value);
48+
}
49+
}
50+
}
51+
52+
/**
53+
* Forget the current tenant.
54+
*
55+
* @return void
56+
*/
57+
public function forgetCurrent(): void
58+
{
59+
}
60+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace ProcessMaker\Multitenancy;
4+
5+
use Spatie\Multitenancy\Models\Tenant as SpatieTenant;
6+
7+
class Tenant extends SpatieTenant
8+
{
9+
protected $casts = [
10+
'config_overrides' => 'array',
11+
];
12+
}

‎config/app.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@
210210
'Theme' => Igaster\LaravelTheme\Facades\Theme::class,
211211
'WorkspaceManager' => ProcessMaker\Facades\WorkspaceManager::class,
212212
'SettingCache' => ProcessMaker\Cache\Settings\SettingCacheFacade::class,
213+
'Tenant' => ProcessMaker\Multitenancy\Tenant::class,
213214
])->toArray(),
214215

215216
'debug_blacklist' => [

‎database/migrations/landlord/2025_04_10_225123_create_landlord_tenants_table.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public function up()
1515
$table->string('name');
1616
$table->string('domain')->unique();
1717
$table->string('database')->unique();
18+
$table->json('config_overrides')->nullable();
1819
$table->timestamps();
1920
});
2021
}

0 commit comments

Comments
 (0)