mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 16:24:04 +00:00
Merge branch '2.x' of github.com:stancl/tenancy into 2.x
This commit is contained in:
commit
0515b0b5b5
6 changed files with 114 additions and 6 deletions
58
src/Features/TenantConfig.php
Normal file
58
src/Features/TenantConfig.php
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Features;
|
||||
|
||||
use Illuminate\Foundation\Application;
|
||||
use Stancl\Tenancy\Contracts\Feature;
|
||||
use Stancl\Tenancy\Tenant;
|
||||
use Stancl\Tenancy\TenantManager;
|
||||
|
||||
class TenantConfig implements Feature
|
||||
{
|
||||
/** @var Application */
|
||||
protected $app;
|
||||
|
||||
/** @var array */
|
||||
public $originalConfig = [];
|
||||
|
||||
public function __construct(Application $app)
|
||||
{
|
||||
$this->app = $app;
|
||||
|
||||
foreach ($this->getStorageToConfigMap() as $configKey) {
|
||||
$this->originalConfig[$configKey] = $this->app['config'][$configKey];
|
||||
}
|
||||
}
|
||||
|
||||
public function bootstrap(TenantManager $tenantManager): void
|
||||
{
|
||||
$tenantManager->eventListener('bootstrapped', function (TenantManager $manager) {
|
||||
$this->setTenantConfig($manager->getTenant());
|
||||
});
|
||||
|
||||
$tenantManager->eventListener('ended', function () {
|
||||
$this->unsetTenantConfig();
|
||||
});
|
||||
}
|
||||
|
||||
public function setTenantConfig(Tenant $tenant): void
|
||||
{
|
||||
foreach ($this->getStorageToConfigMap() as $storageKey => $configKey) {
|
||||
$this->app['config'][$configKey] = $tenant->get($storageKey);
|
||||
}
|
||||
}
|
||||
|
||||
public function unsetTenantConfig(): void
|
||||
{
|
||||
foreach ($this->getStorageToConfigMap() as $configKey) {
|
||||
$this->app['config'][$configKey] = $this->originalConfig[$configKey];
|
||||
}
|
||||
}
|
||||
|
||||
public function getStorageToConfigMap(): array
|
||||
{
|
||||
return $this->app['config']['tenancy.storage_to_config_map'] ?? [];
|
||||
}
|
||||
}
|
||||
|
|
@ -23,16 +23,16 @@ class PreventAccessFromTenantDomains
|
|||
{
|
||||
// If the domain is not in exempt domains, it's a tenant domain.
|
||||
// Tenant domains can't have routes without tenancy middleware.
|
||||
$is_an_exempt_domain = in_array($request->getHost(), config('tenancy.exempt_domains'));
|
||||
$is_a_tenant_domain = ! $is_an_exempt_domain;
|
||||
$isExemptDomain = in_array($request->getHost(), config('tenancy.exempt_domains'));
|
||||
$isTenantDomain = ! $isExemptDomain;
|
||||
|
||||
$is_a_tenant_route = in_array('tenancy', $request->route()->middleware());
|
||||
$isTenantRoute = in_array('tenancy', $request->route()->middleware());
|
||||
|
||||
if ($is_a_tenant_domain && ! $is_a_tenant_route) { // accessing web routes from tenant domains
|
||||
if ($isTenantDomain && ! $isTenantRoute) { // accessing web routes from tenant domains
|
||||
return redirect(config('tenancy.home_url'));
|
||||
}
|
||||
|
||||
if ($is_an_exempt_domain && $is_a_tenant_route) { // accessing tenant routes on web domains
|
||||
if ($isExemptDomain && $isTenantRoute) { // accessing tenant routes on web domains
|
||||
abort(404);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -80,7 +80,7 @@ class DatabaseStorageDriver implements StorageDriver
|
|||
public function createTenant(Tenant $tenant): void
|
||||
{
|
||||
$this->centralDatabase->transaction(function () use ($tenant) {
|
||||
Tenants::create(['id' => $tenant->id, 'data' => '{}'])->toArray();
|
||||
Tenants::create(['id' => $tenant->id, 'data' => json_encode($tenant->data)])->toArray();
|
||||
|
||||
$domainData = [];
|
||||
foreach ($tenant->domains as $domain) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue