diff --git a/ProcessMaker/Observers/SettingObserver.php b/ProcessMaker/Observers/SettingObserver.php index b9d87f7b2d..6c5898809a 100644 --- a/ProcessMaker/Observers/SettingObserver.php +++ b/ProcessMaker/Observers/SettingObserver.php @@ -86,4 +86,35 @@ public function deleted(Setting $setting): void $key = $settingCache->createKey(['key' => $setting->key]); $settingCache->invalidate(['key' => $key]); } + + /** + * Handle the setting "updated" event. + * + * @param Setting $setting + * @return void + */ + public function updated(Setting $setting): void + { + $this->updateConfigurationCache($setting); + } + + /** + * Updates the configuration file with the new value of the setting and then cache the updated configuration. + * + * @param Setting setting + * + * @return void + */ + private function updateConfigurationCache(Setting $setting): void + { + if (app()->environment() === 'testing') { + return; + } + + if (app()->configurationIsCached() && $setting->config !== config([$setting->key])) { + config([$setting->key => $setting->config]); + + \Artisan::call('config:cache'); + } + } } diff --git a/tests/Feature/Api/SettingAuthTest.php b/tests/Feature/Api/SettingAuthTest.php index 4736f8d240..f1dca709d9 100644 --- a/tests/Feature/Api/SettingAuthTest.php +++ b/tests/Feature/Api/SettingAuthTest.php @@ -5,6 +5,7 @@ use Illuminate\Foundation\Testing\RefreshDatabase; use ProcessMaker\Models\ProcessCategory; use ProcessMaker\Models\ScriptExecutor; +use ProcessMaker\Models\Setting; use ProcessMaker\Package\Auth\Database\Seeds\AtlassianSeeder; use ProcessMaker\Package\Auth\Database\Seeds\Auth0Seeder; use ProcessMaker\Package\Auth\Database\Seeds\AuthSeeder; @@ -278,4 +279,36 @@ public function testUpdateSsoSettings() $this->assertDatabaseCount('security_logs', 4); } + + public function testConfigCacheUpdatedAfterSettingEdit() + { + $setting = Setting::factory()->create([ + 'name' => 'Allow Standard Login', + 'key' => 'standard-login.enabled', + 'config' => true, + 'group' => 'SSO', + 'format' => 'boolean', + ]); + + $this->assertDatabaseHas('settings', ['key' => $setting->key, 'config' => $setting->config]); + + $this->assertTrue(config('standard-login.enabled')); + + $response = $this->apiCall('GET', route('api.settings.index', ['group' => 'SSO', 'order_by' => 'name', 'order_direction' => 'ASC'])); + $this->assertCount(1, $response['data']); + $standardLogin = $response['data'][0]; + $this->assertEquals('Allow Standard Login', $standardLogin['name']); + $this->assertTrue($standardLogin['config']); + + // Update setting config + $data = array_merge($standardLogin, ['config' => false]); + $response = $this->apiCall('PUT', route('api.settings.update', ['setting' => $standardLogin['id']]), $data); + // Verify the status + $response->assertStatus(204); + // Verify variables were updated + $this->assertDatabaseHas('settings', ['id' => $standardLogin['id'], 'config' => false]); + + // Check if the config cache was updated + $this->assertFalse(config('standard-login.enabled')); + } }