Skip to content

Commit

Permalink
fix: Oauth seeder
Browse files Browse the repository at this point in the history
  • Loading branch information
peaklabs-dev committed Dec 23, 2024
1 parent e51207a commit 68362d0
Showing 1 changed file with 36 additions and 44 deletions.
80 changes: 36 additions & 44 deletions database/seeders/OauthSettingSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\OauthSetting;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Log;

class OauthSettingSeeder extends Seeder
{
Expand All @@ -12,62 +13,53 @@ class OauthSettingSeeder extends Seeder
*/
public function run(): void
{
$providers = collect([
'azure',
'bitbucket',
'github',
'gitlab',
'google',
'authentik',
]);
try {
$providers = collect([
'azure',
'bitbucket',
'github',
'gitlab',
'google',
'authentik',
]);

$isOauthSeeded = OauthSetting::count() > 0;
$isOauthSeeded = OauthSetting::count() > 0;

// 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
// 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

$isOauthAuthentik = OauthSetting::where('provider', 'authentik')->exists();
if ($isOauthSeeded) {
if (! $isOauthAuthentik) {
$allProviders = OauthSetting::all();
$notFoundProviders = $providers->diff($allProviders->pluck('provider'));

$allProviders->each(function ($provider) {
$provider->delete();
});
$allProviders->each(function ($provider) use ($providers) {
$providerName = $provider->provider;

$foundProvider = $providers->first(function ($provider) use ($providerName) {
return $provider === $providerName;
});

if ($foundProvider) {
$newProvder = new OauthSetting;
$newProvder = $provider;
unset($newProvder->id);
$newProvder->save();
}
});

foreach ($notFoundProviders as $provider) {
OauthSetting::create([
'provider' => $provider,
]);
}
} else {
$isOauthAuthentik = OauthSetting::where('provider', 'authentik')->exists();
if (! $isOauthSeeded || $isOauthAuthentik) {
foreach ($providers as $provider) {
OauthSetting::updateOrCreate([
'provider' => $provider,
]);
}

return;
}
} else {
foreach ($providers as $provider) {
OauthSetting::updateOrCreate([

$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([
'provider' => $provider,
]);
}

} catch (\Exception $e) {
Log::error($e->getMessage());
}
}
}

0 comments on commit 68362d0

Please sign in to comment.