forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwitchTenant.php
More file actions
159 lines (131 loc) · 5.32 KB
/
SwitchTenant.php
File metadata and controls
159 lines (131 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace ProcessMaker\Multitenancy;
use Illuminate\Broadcasting\BroadcastManager;
use Illuminate\Contracts\Routing\UrlGenerator;
use Illuminate\Support\Arr;
use Illuminate\Support\Env;
use Monolog\Handler\RotatingFileHandler;
use ProcessMaker\Application;
use ProcessMaker\Multitenancy\Broadcasting\TenantAwareBroadcastManager;
use Spatie\Multitenancy\Concerns\UsesMultitenancyConfig;
use Spatie\Multitenancy\Contracts\IsTenant;
use Spatie\Multitenancy\Tasks\SwitchTenantTask;
class SwitchTenant implements SwitchTenantTask
{
use UsesMultitenancyConfig;
public static $landlordValues = null;
/**
* Make the given tenant current.
*
* @param IsTenant $tenant
* @return void
*/
public function makeCurrent(IsTenant $tenant): void
{
$app = app();
\Log::debug('SwitchTenant: ' . $tenant->id, ['domain' => request()->getHost()]);
// Save the landlord values for later use
if (!self::$landlordValues) {
self::$landlordValues = $app->make('config')->all();
}
// Set the tenant's domain in the request headers. Used for things like the global url() helper.
request()->headers->set('host', $tenant->domain);
$this->overrideConfigs($app, $tenant);
// Extend BroadcastManager to our custom implementation that prefixes the channel names with the tenant id.
$app->extend(BroadcastManager::class, function ($manager, $app) use ($tenant) {
return new TenantAwareBroadcastManager($app, $tenant->id);
});
}
/**
* Forget the current tenant.
*
* @return void
*/
public function forgetCurrent(): void
{
$app = app();
$app->useStoragePath(base_path('storage'));
$this->setConfig('logging.channels.daily.path', storage_path('logs/processmaker.log'));
$app->make('log')->reset();
$app->forgetInstance('log');
$app->useLangPath(resource_path('lang'));
// app key / encrypter
$this->setConfig('app.key', $this->landlordConfig('app.key'));
$app->forgetInstance('encrypter');
}
private function landlordConfig($key)
{
return Arr::get(self::$landlordValues, $key);
}
private function setConfig($key, $value)
{
app()->make('config')->set($key, $value);
}
private function setEnvironmentVariable($key, $value)
{
putenv("$key=$value");
$_SERVER[$key] = $value;
$_ENV[$key] = $value;
}
private function overrideConfigs(Application $app, IsTenant $tenant)
{
$this->setEnvironmentVariable('APP_URL', $tenant->config['app.url']);
$this->setConfig('app.instance', $this->landlordConfig('app.instance') . '_' . $tenant->id);
$this->setConfig('app.url', $tenant->config['app.url']);
// Microservice callback url
if (!isset($tenant->config['script-runner-microservice.callback'])) {
$this->setConfig('script-runner-microservice.callback', str_replace(
$this->landlordConfig('app.url'),
$tenant->config['app.url'],
$this->landlordConfig('script-runner-microservice.callback')
));
}
// Filesystem roots
$landlordStoragePath = base_path('storage');
$newStoragePath = base_path('storage/tenant_' . $tenant->id);
foreach ($this->landlordConfig('filesystems.disks') as $disk => $config) {
if (isset($config['root'])) {
$this->setConfig('filesystems.disks.' . $disk . '.root', str_replace(
$landlordStoragePath,
$newStoragePath,
$config['root']
));
}
// URLs
if (isset($config['url'])) {
$this->setConfig('filesystems.disks.' . $disk . '.url', str_replace(
$this->landlordConfig('app.url'),
$tenant->config['app.url'],
$config['url']
));
}
}
$app->useStoragePath($newStoragePath);
// Lang path
$app->useLangPath(resource_path('lang/tenant_' . $tenant->id));
$this->setConfig('filesystems.disks.lang.root', lang_path());
// app key / encrypter
$landlordEncrypter = $app->make('encrypter');
$this->setConfig('app.key', $landlordEncrypter->decryptString($tenant->config['app.key']));
$app->forgetInstance('encrypter');
// Logging
$this->setConfig('logging.channels.daily.path', storage_path('logs/processmaker.log'));
$app->make('log')->reset();
$app->forgetInstance('log');
// url() helper
app(UrlGenerator::class)->useOrigin($tenant->config['app.url']);
// NOTE: Cache prefix and cache settings prefix are handled in PrefixCacheTask
if (!isset($tenant->config['app.docker_host_url'])) {
// There is no specific override in the tenant's config so set it to the app url
$this->setConfig('app.docker_host_url', $tenant->config['app.url']);
}
// Set config from the entry in the tenants table
$config = $tenant->config;
foreach ($config as $key => $value) {
if ($key === 'app.key' || $key === 'app.url') {
continue;
}
$this->setConfig($key, $value);
}
}
}