forked from larabeans/tenanter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTenancy.php
190 lines (145 loc) · 5.06 KB
/
Tenancy.php
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
namespace App\Containers\Vendor\Tenanter;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Traits\Macroable;
use App\Containers\Vendor\Tenanter\Events;
use App\Containers\Vendor\Tenanter\Contracts\Host;
use App\Containers\Vendor\Tenanter\Contracts\Tenant;
use App\Containers\Vendor\Tenanter\Contracts\TenancyBootstrapper;
use App\Containers\Vendor\Tenanter\Exceptions\HostCouldNotBeIdentifiedById;
use App\Containers\Vendor\Tenanter\Exceptions\TenantCouldNotBeIdentifiedById;
class Tenancy
{
use Macroable;
/** @var Host|Model|null */
public $host;
/** @var Tenant|Model|null */
public $tenant;
/** @var Domain|Model|null */
public $domain;
/** @var bool */
public $initialized = false;
/** @var bool */
public $hostInitialized = false;
/** @var bool */
public $tenantInitialized = false;
/** @var callable|null */
public $getBootstrappersUsing = null;
/**
* Initializes the host.
* @param Host|int|string $host
* @return void
*/
public function initializeHost($host): void
{
if (! is_object($host)) {
$hostId = $host;
$host = $this->findHost($hostId);
if (! $host) {
throw new HostCouldNotBeIdentifiedById($hostId);
}
}
// host is same, as already initialized
if ($this->initialized && $this->hostInitialized && $this->host->getHostKey() === $host->getHostKey()) {
return;
}
// This will end tenancy with old tenant & will use to revert to host context
// if ($this->hostInitialized) {
// $this->end();
// }
$this->host = $host;
event(new Events\InitializingTenancy($this));
$this->initialized = true;
$this->hostInitialized = true;
event(new Events\TenancyInitialized($this));
}
/**
* Initializes the tenant.
* @param Tenant|int|string $tenant
* @return void
*/
public function initializeTenant($tenant): void
{
if (! is_object($tenant)) {
$tenantId = $tenant;
$tenant = $this->findTenant($tenantId);
if (! $tenant) {
throw new TenantCouldNotBeIdentifiedById($tenantId);
}
}
// tenant is same, as already initialized
if ($this->initialized && $this->tenantInitialized && $this->tenant->getTenantKey() === $tenant->getTenantKey()) {
return;
}
// This will end tenancy
// will use to revert to host context
if ($this->tenantInitialized) {
$this->end();
}
$this->tenant = $tenant;
event(new Events\InitializingTenancy($this));
$this->initialized = true;
$this->tenantInitialized = true;
event(new Events\TenancyInitialized($this));
}
public function end(): void
{
event(new Events\EndingTenancy($this));
if (! $this->initialized && ! $this->tenantInitialized) {
return;
}
$this->initialized = false;
$this->tenantInitialized = false;
event(new Events\TenancyEnded($this));
$this->tenant = null;
}
/** @return TenancyBootstrapper[] */
public function getBootstrappers(): array
{
// If no callback for getting bootstrappers is set, we just return all of them.
$resolve = $this->getBootstrappersUsing ?? function () {
return config('tenanter.bootstrappers');
};
// Here We instantiate the bootstrappers and return them.
return array_map('app', $resolve($this->tenant));
}
public function findTenant($id): ?Tenant
{
return $this->model('tenant')->where($this->model('tenant')->getTenantKeyName(), $id)->first();
}
public function findHost($id): ?Host
{
return $this->model('host')->where($this->model('host')->getHostKeyName(), $id)->first();
}
public function query($key): Builder
{
return $this->model($key)->query();
}
public function model($key)
{
$class = config('tenanter.models.' . $key);
return new $class;
}
public function validTable($table): bool
{
return ! in_array($table, config('tenanter.ignore_tables'));
}
public function isValidHostAdmin(): bool
{
return Auth::check() && Auth::user()->hasAdminRole() && $this->host && $this->host->getHostKey() === Auth::user()->tenant_id;
}
public function isValidHostUser(): bool
{
return Auth::check() && $this->host && $this->host->getHostKey() === Auth::user()->tenant_id;
}
public function isValidTenantAdmin(): bool
{
return Auth::check() && Auth::user()->hasRole('tenant-admin') && $this->tenant && $this->tenant->getTenantKey() === Auth::user()->tenant_id;
}
public function isValidTenantUser(): bool
{
return Auth::check() && $this->tenant && $this->tenant->getTenantKey() === Auth::user()->tenant_id;
}
}