From d07958b10eb299d4c9ab5528d861972132890ecf Mon Sep 17 00:00:00 2001 From: Sven Date: Sun, 9 Feb 2025 07:55:29 +0100 Subject: [PATCH 1/9] Add settings to enable and disable API Add settings to force authentication for API --- .../2025_02_09_073310_add_api_settings.php | 25 +++++++++++++++ database/seeders/DatabaseSeeder.php | 2 ++ resources/lang/de/settings.php | 2 ++ resources/lang/de_AT/settings.php | 2 ++ resources/lang/de_CH/settings.php | 2 ++ resources/lang/en/settings.php | 2 ++ resources/lang/nl/settings.php | 2 ++ resources/lang/ph/settings.php | 2 ++ resources/lang/pt_BR/settings.php | 2 ++ resources/lang/zh_CN/settings.php | 5 +++ resources/lang/zh_TW/settings.php | 2 ++ src/CachetCoreServiceProvider.php | 31 ++++++++++++++----- src/Filament/Pages/Settings/ManageCachet.php | 13 ++++++++ src/Settings/AppSettings.php | 4 +++ 14 files changed, 88 insertions(+), 8 deletions(-) create mode 100644 database/migrations/2025_02_09_073310_add_api_settings.php diff --git a/database/migrations/2025_02_09_073310_add_api_settings.php b/database/migrations/2025_02_09_073310_add_api_settings.php new file mode 100644 index 00000000..65acb748 --- /dev/null +++ b/database/migrations/2025_02_09_073310_add_api_settings.php @@ -0,0 +1,25 @@ + $this->migrator->add('app.api_enabled', true)); + rescue(fn () => $this->migrator->add('app.api_protected', false)); + } + /** + * Run the migrations. + */ + public function down(): void + { + // Theme settings... + rescue(fn () => $this->migrator->deleteIfExists('app.api_enabled')); + rescue(fn () => $this->migrator->deleteIfExists('app.api_protected')); + } +}; diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 00e6cc2d..d7964d6d 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -222,6 +222,8 @@ public function run(): void $appSettings->major_outage_threshold = 25; $appSettings->recent_incidents_only = false; $appSettings->recent_incidents_days = 7; + $appSettings->api_enabled = true; + $appSettings->api_get_protected = false; $appSettings->save(); $customizationSettings = app(CustomizationSettings::class); diff --git a/resources/lang/de/settings.php b/resources/lang/de/settings.php index c30eb3d9..6b3f9bd1 100644 --- a/resources/lang/de/settings.php +++ b/resources/lang/de/settings.php @@ -18,6 +18,8 @@ 'only_show_disrupted_days' => 'Nur Tage mit Vorfällen anzeigen', 'recent_incidents_only' => 'Nur aktuelle Vorfälle anzeigen', 'recent_incidents_days' => 'Anzahl der Tage, an denen aktuelle Vorfälle angezeigt werden sollen', + 'api_enabled' => 'API aktiviert', + 'api_protected' => 'API erfordert Authentifizierung', ], ], 'manage_customization' => [ diff --git a/resources/lang/de_AT/settings.php b/resources/lang/de_AT/settings.php index c30eb3d9..6b3f9bd1 100644 --- a/resources/lang/de_AT/settings.php +++ b/resources/lang/de_AT/settings.php @@ -18,6 +18,8 @@ 'only_show_disrupted_days' => 'Nur Tage mit Vorfällen anzeigen', 'recent_incidents_only' => 'Nur aktuelle Vorfälle anzeigen', 'recent_incidents_days' => 'Anzahl der Tage, an denen aktuelle Vorfälle angezeigt werden sollen', + 'api_enabled' => 'API aktiviert', + 'api_protected' => 'API erfordert Authentifizierung', ], ], 'manage_customization' => [ diff --git a/resources/lang/de_CH/settings.php b/resources/lang/de_CH/settings.php index c30eb3d9..6b3f9bd1 100644 --- a/resources/lang/de_CH/settings.php +++ b/resources/lang/de_CH/settings.php @@ -18,6 +18,8 @@ 'only_show_disrupted_days' => 'Nur Tage mit Vorfällen anzeigen', 'recent_incidents_only' => 'Nur aktuelle Vorfälle anzeigen', 'recent_incidents_days' => 'Anzahl der Tage, an denen aktuelle Vorfälle angezeigt werden sollen', + 'api_enabled' => 'API aktiviert', + 'api_protected' => 'API erfordert Authentifizierung', ], ], 'manage_customization' => [ diff --git a/resources/lang/en/settings.php b/resources/lang/en/settings.php index 63b32d87..33c332b8 100644 --- a/resources/lang/en/settings.php +++ b/resources/lang/en/settings.php @@ -18,6 +18,8 @@ 'only_show_disrupted_days' => 'Only Show Disrupted Days', 'recent_incidents_only' => 'Show Recent Incidents Only', 'recent_incidents_days' => 'Number of Days to Show Recent Incidents', + 'api_enabled' => 'API Enabled', + 'api_protected' => 'API requires authentication', ], ], 'manage_customization' => [ diff --git a/resources/lang/nl/settings.php b/resources/lang/nl/settings.php index 03140c75..26af8249 100644 --- a/resources/lang/nl/settings.php +++ b/resources/lang/nl/settings.php @@ -18,6 +18,8 @@ 'only_show_disrupted_days' => 'Alleen dagen met incidenten weergeven', 'recent_incidents_only' => 'Toon alleen actuele incidenten', 'recent_incidents_days' => 'Aantal dagen waarop actuele incidenten moeten worden weergegeven', + 'api_enabled' => 'API geactiveerd', + 'api_protected' => 'API authenticatie vereist', ], ], 'manage_customization' => [ diff --git a/resources/lang/ph/settings.php b/resources/lang/ph/settings.php index 7b77da60..66cc3794 100644 --- a/resources/lang/ph/settings.php +++ b/resources/lang/ph/settings.php @@ -18,6 +18,8 @@ 'only_show_disrupted_days' => 'Ipakita lamang ang Mga Araw ng Pagka-aberya', 'recent_incidents_only' => 'Ipakita lamang ang Mga Kamakailang Insidente', 'recent_incidents_days' => 'Bilang ng Araw para Ipakita ang Mga Kamakailang Insidente', + 'api_enabled' => 'Api Pinagana', + 'api_protected' => 'Ang Api ay nangangailangan ng pagpapatunay', ], ], 'manage_customization' => [ diff --git a/resources/lang/pt_BR/settings.php b/resources/lang/pt_BR/settings.php index 52a2212f..a5030d5b 100644 --- a/resources/lang/pt_BR/settings.php +++ b/resources/lang/pt_BR/settings.php @@ -18,6 +18,8 @@ 'only_show_disrupted_days' => 'Exibir Apenas Dias com Interrupções', 'recent_incidents_only' => 'Exibir Apenas Incidentes Recentes', 'recent_incidents_days' => 'Número de Dias para Exibir Incidentes Recentes', + 'api_enabled' => 'API Ativada', + 'api_protected' => 'API requer autenticação', ], ], 'manage_customization' => [ diff --git a/resources/lang/zh_CN/settings.php b/resources/lang/zh_CN/settings.php index 2067d453..46f1d4bd 100644 --- a/resources/lang/zh_CN/settings.php +++ b/resources/lang/zh_CN/settings.php @@ -18,6 +18,11 @@ 'only_show_disrupted_days' => '仅显示受干扰天数', 'recent_incidents_only' => '仅显示最近事件', 'recent_incidents_days' => '显示最近事件的天数', + 'manage_customization' => [ + 'header_label' => '自定义 Header HTML', + 'footer_label' => '自定义 Footer HTML', + 'stylesheet_label' => '自定义 CSS', + ], ], ], 'manage_customization' => [ diff --git a/resources/lang/zh_TW/settings.php b/resources/lang/zh_TW/settings.php index a2f4a2ff..dd9fbe60 100644 --- a/resources/lang/zh_TW/settings.php +++ b/resources/lang/zh_TW/settings.php @@ -18,6 +18,8 @@ 'only_show_disrupted_days' => '僅顯示受干擾天數', 'recent_incidents_only' => '僅顯示最近事件', 'recent_incidents_days' => '顯示最近事件的天數', + 'api_enabled' => '啟用 Api', + 'api_protected' => 'Api 需要身份驗證', ], ], 'manage_customization' => [ diff --git a/src/CachetCoreServiceProvider.php b/src/CachetCoreServiceProvider.php index 770cfe50..d33744da 100644 --- a/src/CachetCoreServiceProvider.php +++ b/src/CachetCoreServiceProvider.php @@ -26,6 +26,7 @@ use Illuminate\Support\Facades\RateLimiter; use Illuminate\Support\Facades\Route; use Illuminate\Support\ServiceProvider; +use Spatie\LaravelSettings\Exceptions\MissingSettings; use Spatie\WebhookServer\Events\WebhookCallFailedEvent; use Spatie\WebhookServer\Events\WebhookCallSucceededEvent; @@ -111,14 +112,28 @@ private function configureRateLimiting(): void private function registerRoutes(): void { $this->callAfterResolving('router', function (Router $router, Application $application) { - $router->group([ - 'domain' => config('cachet.domain'), - 'as' => 'cachet.api.', - 'prefix' => Cachet::path().'/api', - 'middleware' => ['cachet:api', 'throttle:cachet-api'], - ], function (Router $router) { - $this->loadRoutesFrom(__DIR__.'/../routes/api.php'); - }); + $settings = app(AppSettings::class); + try { + if ($settings->api_enabled) { + $middleware = ['cachet:api', 'throttle:cachet-api']; + if ($settings->api_protected) { + $middleware[] = 'auth:sanctum'; + } + + $router->group([ + 'domain' => config('cachet.domain'), + 'as' => 'cachet.api.', + 'prefix' => Cachet::path().'/api', + 'middleware' => $middleware, + ], function (Router $router) { + $this->loadRoutesFrom(__DIR__.'/../routes/api.php'); + }); + } + } catch (MissingSettings $exception) { + if(! $application->runningInConsole()) { + throw new \Exception("Please run `php artisan migrate` to load missing settings."); + } + } Cachet::routes() ->register(); diff --git a/src/Filament/Pages/Settings/ManageCachet.php b/src/Filament/Pages/Settings/ManageCachet.php index 675b42f2..a50145a5 100644 --- a/src/Filament/Pages/Settings/ManageCachet.php +++ b/src/Filament/Pages/Settings/ManageCachet.php @@ -84,6 +84,8 @@ public function form(Form $form): Form ->label(__('cachet::settings.manage_cachet.toggles.only_show_disrupted_days')), Forms\Components\Toggle::make('dashboard_login_link') ->label(__('cachet::settings.manage_cachet.toggles.show_dashboard_link')), + + Forms\Components\Grid::make(2) ->schema([ Forms\Components\Toggle::make('recent_incidents_only') @@ -98,6 +100,17 @@ public function form(Form $form): Form ->suffix(__('cachet::settings.manage_cachet.recent_incidents_days_suffix_days')) ->hidden(fn (Get $get) => $get('recent_incidents_only') !== true), ]), + + + Forms\Components\Grid::make(2) + ->schema([ + Forms\Components\Toggle::make('api_enabled') + ->label(__('cachet::settings.manage_cachet.toggles.api_enabled')) + ->reactive(), + Forms\Components\Toggle::make('api_protected') + ->label(__('cachet::settings.manage_cachet.toggles.api_protected')) + ->reactive(), + ]), ]), ]); } diff --git a/src/Settings/AppSettings.php b/src/Settings/AppSettings.php index 7413aa7e..e300c908 100644 --- a/src/Settings/AppSettings.php +++ b/src/Settings/AppSettings.php @@ -32,6 +32,10 @@ class AppSettings extends Settings public int $recent_incidents_days = 7; + public bool $api_enabled = true; + + public bool $api_protected = false; + public static function group(): string { return 'app'; From 285c7d19366faed6f78adeb6869fccefa56c92f7 Mon Sep 17 00:00:00 2001 From: Sven Date: Sun, 9 Feb 2025 07:58:38 +0100 Subject: [PATCH 2/9] Small typo --- database/seeders/DatabaseSeeder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index d7964d6d..0c84c6f0 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -223,7 +223,7 @@ public function run(): void $appSettings->recent_incidents_only = false; $appSettings->recent_incidents_days = 7; $appSettings->api_enabled = true; - $appSettings->api_get_protected = false; + $appSettings->api_protected = false; $appSettings->save(); $customizationSettings = app(CustomizationSettings::class); From 6de40311052a31f82e47362907f98ab3090991ec Mon Sep 17 00:00:00 2001 From: Sven Date: Sun, 9 Feb 2025 08:04:28 +0100 Subject: [PATCH 3/9] Composer fail fix --- src/CachetCoreServiceProvider.php | 38 ++++++++++++++++++------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/CachetCoreServiceProvider.php b/src/CachetCoreServiceProvider.php index d33744da..024bcb39 100644 --- a/src/CachetCoreServiceProvider.php +++ b/src/CachetCoreServiceProvider.php @@ -112,29 +112,35 @@ private function configureRateLimiting(): void private function registerRoutes(): void { $this->callAfterResolving('router', function (Router $router, Application $application) { - $settings = app(AppSettings::class); + $apiEnabled = true; + $apiProtected = false; try { - if ($settings->api_enabled) { - $middleware = ['cachet:api', 'throttle:cachet-api']; - if ($settings->api_protected) { - $middleware[] = 'auth:sanctum'; - } - - $router->group([ - 'domain' => config('cachet.domain'), - 'as' => 'cachet.api.', - 'prefix' => Cachet::path().'/api', - 'middleware' => $middleware, - ], function (Router $router) { - $this->loadRoutesFrom(__DIR__.'/../routes/api.php'); - }); - } + $settings = app(AppSettings::class); + $apiEnabled = $settings->api_enabled; + $apiProtected = $settings->api_protected; + } catch (MissingSettings $exception) { if(! $application->runningInConsole()) { throw new \Exception("Please run `php artisan migrate` to load missing settings."); } } + if ($apiEnabled) { + $middleware = ['cachet:api', 'throttle:cachet-api']; + if ($apiProtected) { + $middleware[] = 'auth:sanctum'; + } + + $router->group([ + 'domain' => config('cachet.domain'), + 'as' => 'cachet.api.', + 'prefix' => Cachet::path().'/api', + 'middleware' => $middleware, + ], function (Router $router) { + $this->loadRoutesFrom(__DIR__.'/../routes/api.php'); + }); + } + Cachet::routes() ->register(); }); From a37c30d00cfdf8609dc6efcc37ecae1c0684cfcc Mon Sep 17 00:00:00 2001 From: Sven Date: Sun, 9 Feb 2025 08:07:20 +0100 Subject: [PATCH 4/9] Composer fail fix --- src/CachetCoreServiceProvider.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/CachetCoreServiceProvider.php b/src/CachetCoreServiceProvider.php index 024bcb39..6967b5d4 100644 --- a/src/CachetCoreServiceProvider.php +++ b/src/CachetCoreServiceProvider.php @@ -123,6 +123,8 @@ private function registerRoutes(): void if(! $application->runningInConsole()) { throw new \Exception("Please run `php artisan migrate` to load missing settings."); } + } catch (\Exception $exception) { + // do nothing } if ($apiEnabled) { From c2f1880f11f92ee14baf5991efa440bd9a1444f6 Mon Sep 17 00:00:00 2001 From: Sven Date: Sun, 9 Feb 2025 08:20:17 +0100 Subject: [PATCH 5/9] Hide api protected when api is disabled --- src/Filament/Pages/Settings/ManageCachet.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Filament/Pages/Settings/ManageCachet.php b/src/Filament/Pages/Settings/ManageCachet.php index a50145a5..aa289619 100644 --- a/src/Filament/Pages/Settings/ManageCachet.php +++ b/src/Filament/Pages/Settings/ManageCachet.php @@ -109,6 +109,7 @@ public function form(Form $form): Form ->reactive(), Forms\Components\Toggle::make('api_protected') ->label(__('cachet::settings.manage_cachet.toggles.api_protected')) + ->visible(fn (Get $get) => $get('api_enabled')) ->reactive(), ]), ]), From 5926004fd459a43a8accfa5bd50d6e8f1b0daa80 Mon Sep 17 00:00:00 2001 From: Sven Date: Tue, 11 Feb 2025 17:03:03 +0100 Subject: [PATCH 6/9] Moved api check to 2 different middleware. ApiEnabled & ApiPublicOrProtected which solves the early access to DB Added GetOrDefault to AppSettings for ease of use and removal of duplicate code. --- config/cachet.php | 2 + src/CachetCoreServiceProvider.php | 38 ++------- src/Http/Middleware/ApiEnabled.php | 30 +++++++ src/Http/Middleware/ApiPublicOrProtected.php | 31 +++++++ src/Settings/AppSettings.php | 22 +++++ tests/Feature/Api/ApiFunctionalityTest.php | 87 ++++++++++++++++++++ 6 files changed, 180 insertions(+), 30 deletions(-) create mode 100644 src/Http/Middleware/ApiEnabled.php create mode 100644 src/Http/Middleware/ApiPublicOrProtected.php create mode 100644 tests/Feature/Api/ApiFunctionalityTest.php diff --git a/config/cachet.php b/config/cachet.php index ea368dfd..a3bddd1f 100644 --- a/config/cachet.php +++ b/config/cachet.php @@ -75,6 +75,8 @@ 'api_middleware' => [ 'api', + \Cachet\Http\Middleware\ApiEnabled::class, + \Cachet\Http\Middleware\ApiPublicOrProtected::class, ], /* diff --git a/src/CachetCoreServiceProvider.php b/src/CachetCoreServiceProvider.php index 6967b5d4..9932be45 100644 --- a/src/CachetCoreServiceProvider.php +++ b/src/CachetCoreServiceProvider.php @@ -112,36 +112,14 @@ private function configureRateLimiting(): void private function registerRoutes(): void { $this->callAfterResolving('router', function (Router $router, Application $application) { - $apiEnabled = true; - $apiProtected = false; - try { - $settings = app(AppSettings::class); - $apiEnabled = $settings->api_enabled; - $apiProtected = $settings->api_protected; - - } catch (MissingSettings $exception) { - if(! $application->runningInConsole()) { - throw new \Exception("Please run `php artisan migrate` to load missing settings."); - } - } catch (\Exception $exception) { - // do nothing - } - - if ($apiEnabled) { - $middleware = ['cachet:api', 'throttle:cachet-api']; - if ($apiProtected) { - $middleware[] = 'auth:sanctum'; - } - - $router->group([ - 'domain' => config('cachet.domain'), - 'as' => 'cachet.api.', - 'prefix' => Cachet::path().'/api', - 'middleware' => $middleware, - ], function (Router $router) { - $this->loadRoutesFrom(__DIR__.'/../routes/api.php'); - }); - } + $router->group([ + 'domain' => config('cachet.domain'), + 'as' => 'cachet.api.', + 'prefix' => Cachet::path().'/api', + 'middleware' => ['cachet:api', 'throttle:cachet-api'], + ], function (Router $router) { + $this->loadRoutesFrom(__DIR__.'/../routes/api.php'); + }); Cachet::routes() ->register(); diff --git a/src/Http/Middleware/ApiEnabled.php b/src/Http/Middleware/ApiEnabled.php new file mode 100644 index 00000000..8146f72e --- /dev/null +++ b/src/Http/Middleware/ApiEnabled.php @@ -0,0 +1,30 @@ +$name; + } catch (MissingSettings $exception) { + if(! app()->runningInConsole()) { + throw new \Exception("Please run `php artisan migrate` to load missing settings."); + } + } + return $default; + } } diff --git a/tests/Feature/Api/ApiFunctionalityTest.php b/tests/Feature/Api/ApiFunctionalityTest.php new file mode 100644 index 00000000..71273ef0 --- /dev/null +++ b/tests/Feature/Api/ApiFunctionalityTest.php @@ -0,0 +1,87 @@ +api_enabled = false; + $settings->api_protected = false; + $settings->save(); + + getJson('/status/api/ping') + ->assertNotFound(); +}); + +it('has api disabled with protected and user', function () { + $settings = app(AppSettings::class); + $settings->api_enabled = false; + $settings->api_protected = true; + $settings->save(); + + Sanctum::actingAs(User::factory()->create(), ['general.ping']); + + getJson('/status/api/ping') + ->assertNotFound(); +}); + +it('has api disabled without protected and user', function () { + $settings = app(AppSettings::class); + $settings->api_enabled = false; + $settings->api_protected = true; + $settings->save(); + + Sanctum::actingAs(User::factory()->create(), ['general.ping']); + + getJson('/status/api/ping') + ->assertNotFound(); +}); + + +it('has public api access', function () { + $settings = app(AppSettings::class); + $settings->api_enabled = true; + $settings->api_protected = false; + $settings->save(); + + Sanctum::actingAs(User::factory()->create(), ['general.ping']); + + getJson('/status/api/ping') + ->assertOk(); +}); + + +it('has public api access with a user', function () { + $settings = app(AppSettings::class); + $settings->api_enabled = true; + $settings->api_protected = false; + $settings->save(); + + getJson('/status/api/ping') + ->assertOk(); +}); + +it('has no access to api without a user', function () { + $settings = app(AppSettings::class); + $settings->api_enabled = true; + $settings->api_protected = true; + $settings->save(); + getJson('/status/api/ping') + ->assertUnauthorized(); +}); + + +it('has access to api with a user', function () { + $settings = app(AppSettings::class); + $settings->api_enabled = true; + $settings->api_protected = true; + $settings->save(); + + Sanctum::actingAs(User::factory()->create(), ['general.ping']); + + getJson('/status/api/ping') + ->assertOk(); +}); From f4544963fcadc6999bff482157133146d2330cba Mon Sep 17 00:00:00 2001 From: Sven Date: Tue, 11 Feb 2025 17:17:46 +0100 Subject: [PATCH 7/9] Api Enabled should not have to be extended from Auth --- src/Http/Middleware/ApiEnabled.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Http/Middleware/ApiEnabled.php b/src/Http/Middleware/ApiEnabled.php index 8146f72e..d21d2c2a 100644 --- a/src/Http/Middleware/ApiEnabled.php +++ b/src/Http/Middleware/ApiEnabled.php @@ -4,23 +4,19 @@ use Cachet\Settings\AppSettings; use Closure; -use Illuminate\Auth\AuthenticationException; -use Illuminate\Auth\Middleware\Authenticate as BaseAuthenticationMiddleware; use Illuminate\Http\Request; -class ApiEnabled extends BaseAuthenticationMiddleware +class ApiEnabled { /** * Handle an incoming request. * * @param Request $request * @param Closure(Request):mixed $next - * @param string ...$guards * @return mixed * - * @throws AuthenticationException */ - public function handle($request, Closure $next, ...$guards) + public function handle($request, Closure $next) { if (!AppSettings::getOrDefault('api_enabled', true)) { abort(404); From 4c5d1a3896e9c4ff4b4df735eb859b33c226b6d3 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Thu, 13 Feb 2025 08:48:24 +0000 Subject: [PATCH 8/9] Refactor --- src/Http/Middleware/ApiEnabled.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Http/Middleware/ApiEnabled.php b/src/Http/Middleware/ApiEnabled.php index d21d2c2a..fb70ca51 100644 --- a/src/Http/Middleware/ApiEnabled.php +++ b/src/Http/Middleware/ApiEnabled.php @@ -5,6 +5,7 @@ use Cachet\Settings\AppSettings; use Closure; use Illuminate\Http\Request; +use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; class ApiEnabled { @@ -18,9 +19,8 @@ class ApiEnabled */ public function handle($request, Closure $next) { - if (!AppSettings::getOrDefault('api_enabled', true)) { - abort(404); - } + throw_unless(AppSettings::getOrDefault('api_enabled', true), NotFoundHttpException::class); + return $next($request); } } From 7f87612b5d426b63349699fece31ad4751c15b2f Mon Sep 17 00:00:00 2001 From: Sven Date: Fri, 7 Mar 2025 23:26:11 +0100 Subject: [PATCH 9/9] Fix when api disabled to reset api_protected to false Fix add static types to method --- src/Filament/Pages/Settings/ManageCachet.php | 5 +++++ src/Settings/AppSettings.php | 10 +--------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/Filament/Pages/Settings/ManageCachet.php b/src/Filament/Pages/Settings/ManageCachet.php index aa289619..b91f3b99 100644 --- a/src/Filament/Pages/Settings/ManageCachet.php +++ b/src/Filament/Pages/Settings/ManageCachet.php @@ -106,6 +106,11 @@ public function form(Form $form): Form ->schema([ Forms\Components\Toggle::make('api_enabled') ->label(__('cachet::settings.manage_cachet.toggles.api_enabled')) + ->afterStateUpdated(function (Get $get, Forms\Set $set, ?bool $old, ?bool $state) { + if ($state === false) { + $set('api_protected', $state); + } + }) ->reactive(), Forms\Components\Toggle::make('api_protected') ->label(__('cachet::settings.manage_cachet.toggles.api_protected')) diff --git a/src/Settings/AppSettings.php b/src/Settings/AppSettings.php index e058294a..b7d43ca4 100644 --- a/src/Settings/AppSettings.php +++ b/src/Settings/AppSettings.php @@ -43,15 +43,7 @@ public static function group(): string } - /** - * Retrieve settings without triggering an exception - * unless a migration has to be run - * @param $name - * @param $default - * @return mixed - * @throws \Exception - */ - public static function getOrDefault($name, $default = null): mixed + public static function getOrDefault(string $name, mixed $default = null): mixed { try { return app(self::class)->$name;