Skip to content

Commit d3ee0a4

Browse files
committed
Update tenant create
1 parent 5565385 commit d3ee0a4

4 files changed

Lines changed: 28 additions & 18 deletions

File tree

‎.github/workflows/deploy-pm4.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ on:
99
jobs:
1010
run:
1111
name: Run PM4-workflow
12-
uses: processmaker/.github/.github/workflows/deploy-pm4.yml@main
12+
uses: processmaker/.github/.github/workflows/deploy-pm4.yml@multitenancy
1313
secrets: inherit

‎ProcessMaker/Console/Commands/TenantsCreate.php‎

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace ProcessMaker\Console\Commands;
44

55
use Illuminate\Console\Command;
6+
use Illuminate\Encryption\Encrypter;
67
use Illuminate\Support\Env;
78
use Illuminate\Support\Facades\Artisan;
9+
use Illuminate\Support\Facades\Crypt;
810
use Illuminate\Support\Facades\DB;
911
use Illuminate\Support\Facades\File;
1012
use ProcessMaker\Multitenancy\Tenant;
@@ -54,19 +56,22 @@ public function handle()
5456
return;
5557
}
5658

59+
$key = 'base64:' . base64_encode(
60+
Encrypter::generateKey(config('app.cipher'))
61+
);
62+
$key = Crypt::encryptString($key);
63+
5764
// insert into the tenants table
58-
$tenant = Tenant::updateOrCreate([
65+
$tenant = Tenant::create([
5966
'domain' => $domain,
60-
], [
6167
'name' => $this->option('name'),
6268
'database' => $this->option('database'),
6369
'username' => $this->option('username', null),
6470
'password' => $this->option('password', null),
65-
'config' => ['app.url' => $this->option('url')],
71+
'config' => ['app.url' => $this->option('url'), 'app.key' => $key],
6672
]);
6773

6874
// Setup storage
69-
7075
$tenantStoragePath = base_path('storage/tenant_' . $tenant->id);
7176
$needsStorage = true;
7277

@@ -112,20 +117,18 @@ public function handle()
112117
}
113118

114119
// Setup database
115-
116-
$tenantDbExists = DB::select("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '{$this->option('database')}'");
120+
// DB::select("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '{$this->option('database')}'");
121+
DB::statement("CREATE DATABASE `{$this->option('database')}`");
117122

118123
// Create the database
119-
if (!$tenantDbExists) {
120-
DB::statement("CREATE DATABASE `{$this->option('database')}`");
121-
$this->tenantArtisan('migrate --seed --force', $tenant->id);
124+
// SKIPPING - migrate/upgrade should be done on its own
125+
// if (!$tenantDbExists) {
126+
// DB::statement("CREATE DATABASE `{$this->option('database')}`");
127+
// $this->tenantArtisan('migrate --seed --force', $tenant->id);
122128

123-
// Add passport keys
124-
$this->tenantArtisan('passport:keys', $tenant->id);
125-
126-
// Call the upgrade commands
127-
$this->tenantArtisan('upgrade', $tenant->id);
128-
}
129+
// // Call the upgrade commands
130+
// $this->tenantArtisan('upgrade', $tenant->id);
131+
// }
129132

130133
// Crate the config cache subfolder
131134
//

‎ProcessMaker/Multitenancy/SwitchTenant.php‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ProcessMaker\Multitenancy;
44

55
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Support\Facades\Crypt;
67
use Illuminate\Support\Facades\DB;
78
use PDO;
89
use Spatie\Multitenancy\Concerns\UsesMultitenancyConfig;
@@ -65,7 +66,13 @@ public function makeCurrent(IsTenant $tenant): void
6566
config($newConfig);
6667

6768
// Set config from the entry in the tenants table
68-
config($tenant->config);
69+
$config = $tenant->config;
70+
if (isset($config['app.key'])) {
71+
// Decrypt using the landlord APP_KEY in the .env file.
72+
// All encryption after this will use the tenant's key.
73+
$config['app.key'] = Crypt::decryptString($config['app.key']);
74+
}
75+
config($config);
6976
}
7077

7178
/**

‎config/database.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
'driver' => env('DB_DRIVER', 'mysql'),
5555
'host' => env('DB_HOSTNAME', 'localhost'),
5656
'port' => env('DB_PORT'),
57-
'database' => env('LANDLORD_DB_DATABASE', 'landlord'),
57+
'database' => env('DB_DATABASE', 'landlord'),
5858
'username' => env('DB_USERNAME'),
5959
'password' => env('DB_PASSWORD'),
6060
'unix_socket' => env('DB_SOCKET', ''),

0 commit comments

Comments
 (0)