coolify/database/seeders/OauthSettingSeeder.php

70 lines
2 KiB
PHP
Raw Normal View History

<?php
namespace Database\Seeders;
use App\Models\OauthSetting;
use Illuminate\Database\Seeder;
2024-12-16 08:48:41 +00:00
use Illuminate\Support\Facades\Log;
class OauthSettingSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
2024-12-16 08:48:41 +00:00
try {
$providers = collect([
'azure',
'bitbucket',
'clerk',
'discord',
2024-12-16 08:48:41 +00:00
'github',
'gitlab',
'google',
'authentik',
2024-12-16 17:24:11 +00:00
'infomaniak',
'zitadel',
2024-12-17 09:43:31 +00:00
]);
2024-12-16 08:48:41 +00:00
$isOauthSeeded = OauthSetting::count() > 0;
2024-12-16 08:48:41 +00:00
// We changed how providers are defined in the database, so we authentik does not exists, we need to recreate all of the auth providers
// Before authentik was a provider, providers started with 0 id
2024-12-16 08:48:41 +00:00
$isOauthAuthentik = OauthSetting::where('provider', 'authentik')->exists();
if (! $isOauthSeeded || $isOauthAuthentik) {
2024-12-12 09:04:12 +00:00
foreach ($providers as $provider) {
OauthSetting::updateOrCreate([
'provider' => $provider,
]);
}
2024-12-16 08:48:41 +00:00
return;
2024-12-12 09:04:12 +00:00
}
2024-12-16 08:48:41 +00:00
$allProviders = OauthSetting::all();
$notFoundProviders = $providers->diff($allProviders->pluck('provider'));
$allProviders->each(function ($provider) {
$provider->delete();
});
$allProviders->each(function ($provider) {
$provider = new OauthSetting;
$provider->provider = $provider->provider;
unset($provider->id);
$provider->save();
});
foreach ($notFoundProviders as $provider) {
OauthSetting::create([
2024-12-12 09:04:12 +00:00
'provider' => $provider,
]);
}
2024-12-16 08:48:41 +00:00
} catch (\Exception $e) {
Log::error($e->getMessage());
}
}
}