Skip to content

FOUR-24651: Enable and disable custom packages / enterprise packages per tenant #8333

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: poc/FOUR-22888-Multitenancy
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions ProcessMaker/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,31 @@
*/
class Application extends IlluminateApplication
{
/**
* Get the path to the cached packages.php file.
*
* @return string
*/
public function getCachedPackagesPath()
{
// Get tenant domain name from domain
if (!app()->runningInConsole()) {
$domain = request()->getHost();
if (str_contains($domain, '.')) {
$tenantDomain = explode('.', $domain)[0];
} else {
$tenantDomain = $domain;
}
// check if there is a storage_path('tenant_' . $tenantDomain . '/framework/cache/packages.php')
if ($tenantDomain && file_exists(storage_path('tenant_' . $tenantDomain . '/framework/cache/packages.php'))) {
return storage_path('tenant_' . $tenantDomain . '/framework/cache/packages.php');
}
}

// if not, return the default path
return $this->normalizeCachePath('APP_PACKAGES_CACHE', 'cache/packages.php');
}

/**
* Sets the timezone for the application and for php with the specified timezone.
*
Expand Down
3 changes: 2 additions & 1 deletion ProcessMaker/Cache/CacheManagerBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public function getPrefix(string $connection): string
}

$tenant = app()->bound('currentTenant') ? app('currentTenant') : null;
$tenantId = $tenant ? $tenant->id : null;
$tenantDomain = $tenant ? $tenant->domain : null;
$tenantId = $tenantDomain ? explode('.', $tenantDomain)[0] : null;

if ($tenantId) {
if (strpos($prefix, self::TENANT_PREFIX) === false) {
Expand Down
10 changes: 6 additions & 4 deletions ProcessMaker/Console/Commands/TenantStorageLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public function handle()
return 1;
}

$this->createStorageLink($tenant->id);
$tenantDomain = explode('.', $tenant->domain)[0];

$this->createStorageLink($tenantDomain);
}

/**
Expand All @@ -43,13 +45,13 @@ public function handle()
* @param int $tenantId
* @return void
*/
protected function createStorageLink($tenantId)
protected function createStorageLink($tenantDomain)
{
$this->info('Creating storage link for tenant ' . $tenantId . '...');
$this->info('Creating storage link for tenant ' . $tenantDomain . '...');

// Define paths
$tenantStoragePath = storage_path('/app/public');
$publicPath = public_path('storage/tenant_' . $tenantId);
$publicPath = public_path('storage/tenant_' . $tenantDomain);

// Create tenant storage directory if it doesn't exist
if (!File::exists($tenantStoragePath)) {
Expand Down
3 changes: 2 additions & 1 deletion ProcessMaker/Console/Commands/TenantsCreate.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public function handle()
]);

// Setup storage
$tenantStoragePath = base_path('storage/tenant_' . $tenant->id);
$tenantName = explode('.', $tenant->domain)[0];
$tenantStoragePath = base_path('storage/tenant_' . $tenantName);
if (!File::isDirectory($tenantStoragePath)) {
mkdir($tenantStoragePath, 0755, true);
}
Expand Down
10 changes: 6 additions & 4 deletions ProcessMaker/Console/Commands/TenantsDisable.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TenantsDisable extends Command
*
* @var string
*/
protected $signature = 'tenants:disable {--default-tenant-id=}';
protected $signature = 'tenants:disable {--default-tenant-domain=}';

/**
* The console command description.
Expand All @@ -34,11 +34,13 @@ public function handle()
$domain = parse_url(Env::get('APP_URL'), PHP_URL_HOST);

$tenant = null;
if ($this->option('default-tenant-id')) {
$tenantId = $this->option('default-tenant-id');
if ($this->option('default-tenant-domain')) {
$tenantDomain = $this->option('default-tenant-domain');
$tenantId = explode('.', $tenantDomain)[0];
} else {
$tenant = Tenant::where('domain', $domain)->firstOrFail();
$tenantId = $tenant->id;
$tenantDomain = explode('.', $tenant->domain)[0];
$tenantId = $tenantDomain;
}

$this->recursiveDelete(base_path('storage/framework'));
Expand Down
10 changes: 6 additions & 4 deletions ProcessMaker/Http/Controllers/StorageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public function serve(Request $request, string $path): BinaryFileResponse
$tenantId = $tenant->id;

// Handle tenant path
$path = $this->handleTenantPath($path, $tenantId);
$tenantDomain = explode('.', $tenant->domain)[0];

$path = $this->handleTenantPath($path, $tenantDomain);
}

// Get storage disk
Expand Down Expand Up @@ -98,13 +100,13 @@ private function validateAndSanitizePath(string $path): string
*
* @return string
*/
private function handleTenantPath(string $path, int $tenantId): string
private function handleTenantPath(string $path, string $tenantDomain): string
{
if (preg_match('/^tenant_\d+\//', $path)) {
return str_replace('tenant_' . $tenantId . '/', '', $path);
return str_replace('tenant_' . $tenantDomain . '/', '', $path);
}

return 'tenant_' . $tenantId . '/' . $path;
return 'tenant_' . $tenantDomain . '/' . $path;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions ProcessMaker/Multitenancy/SwitchTenant.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public function makeCurrent(IsTenant $tenant): void
$this->setTenantDatabaseConnection($tenant);

// Set the tenant-specific storage path
$tenantStoragePath = base_path('storage/tenant_' . $tenant->id);
$tenantDomain = explode('.', $tenant->domain)[0];
$tenantStoragePath = base_path('storage/tenant_' . $tenantDomain);

$app = app();
$app->setStoragePath($tenantStoragePath);

// Create the tenant storage directory if it doesn't exist
// TODO: Move these to somewhere else - should not be run on every request
if (!file_exists($tenantStoragePath)) {
Expand Down
78 changes: 78 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"processmaker/docker-executor-php": "1.4.0",
"processmaker/laravel-i18next": "dev-master",
"processmaker/nayra": "1.12.1",
"processmaker/package-dynamic-ui": "^1.28",
"processmaker/pmql": "1.13.1",
"promphp/prometheus_client_php": "^2.12",
"psr/http-message": "^1.1",
Expand Down Expand Up @@ -215,7 +216,84 @@
{
"type": "vcs",
"url": "https://github.com/ProcessMaker/SocialiteProviders"
},
{
"type": "path",
"url": "/Users/rodrigoquelca/dev/packages"
},
{
"type": "path",
"url": "/Users/rodrigoquelca/dev/package-collections"
},
{
"type": "path",
"url": "/Users/rodrigoquelca/dev/package-data-sources"
},
{
"type": "path",
"url": "/Users/rodrigoquelca/dev/package-savedsearch"
},
{
"type": "path",
"url": "/Users/rodrigoquelca/dev/package-dynamic-ui"
},
{
"type": "path",
"url": "/Users/rodrigoquelca/dev/package-decision-engine"
},
{
"type": "path",
"url": "/Users/rodrigoquelca/dev/package-webentry"
},
{
"type": "path",
"url": "/Users/rodrigoquelca/dev/package-files"
},
{
"type": "path",
"url": "/Users/rodrigoquelca/dev/package-analytics-reporting"
},
{
"type": "path",
"url": "/Users/rodrigoquelca/dev/package-ab-testing"
},
{
"type": "path",
"url": "/Users/rodrigoquelca/dev/connector-send-email"
},
{
"type": "path",
"url": "/Users/rodrigoquelca/dev/package-testing"
},
{
"type": "path",
"url": "/Users/rodrigoquelca/dev/package-versions"
},
{
"type": "path",
"url": "/Users/rodrigoquelca/dev/package-files"
},
{
"type": "path",
"url": "/Users/rodrigoquelca/dev/package-auth"
},
{
"type": "path",
"url": "/Users/rodrigoquelca/dev/package-translations"
},
{
"type": "path",
"url": "/Users/rodrigoquelca/dev/package-advanced-user-manager"
},
{
"type": "path",
"url": "/Users/rodrigoquelca/dev/package-variable-finder"
},
{
"type": "path",
"url": "/Users/rodrigoquelca/dev/package-auth"
}

],
"config": {
"preferred-install": "dist",
Expand Down
Loading
Loading