An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.
PHP coolifydatabasesdeploymentdockerdocker-composeinertiajslaravelmariadbmysqlnextjsnodejsphppostgresredisself-hostedself-hostingserverstatic-sitesveltesvelte5
feat(auth): auto-join OAuth users to root team
Andras Bacsaifc32bc6b6c9d
diff --git a/app/Livewire/SettingsOauth.php b/app/Livewire/SettingsOauth.phpindex d038d9a956..a3ee2c9873 100644--- a/app/Livewire/SettingsOauth.php+++ b/app/Livewire/SettingsOauth.php@@ -42,6 +42,7 @@ class SettingsOauth extends Component$carry["oauth_settings_map.$setting->provider.custom_label"] = 'nullable|string|max:255';$carry["oauth_settings_map.$setting->provider.scopes"] = 'nullable|string|max:1000';$carry["oauth_settings_map.$setting->provider.allow_registration"] = 'boolean';+ $carry["oauth_settings_map.$setting->provider.auto_join_root_team"] = 'boolean';$carry["oauth_settings_map.$setting->provider.require_email_verified"] = 'boolean';$carry["oauth_settings_map.$setting->provider.use_pkce"] = 'boolean';$carry["oauth_settings_map.$setting->provider.clock_skew_seconds"] = 'nullable|integer|min:0|max:600';@@ -148,6 +149,7 @@ class SettingsOauth extends Component'custom_label' => $data['custom_label'] ?? null,'scopes' => $data['scopes'] ?? null,'allow_registration' => (bool) ($data['allow_registration'] ?? false),+ 'auto_join_root_team' => (bool) ($data['auto_join_root_team'] ?? false),'require_email_verified' => (bool) ($data['require_email_verified'] ?? true),'use_pkce' => (bool) ($data['use_pkce'] ?? true),'clock_skew_seconds' => (int) ($data['clock_skew_seconds'] ?? 60),@@ -196,6 +198,7 @@ class SettingsOauth extends Component'custom_label' => $setting->custom_label,'scopes' => $setting->scopes ?: 'openid email profile','allow_registration' => $setting->allow_registration,+ 'auto_join_root_team' => $setting->auto_join_root_team,'require_email_verified' => $setting->require_email_verified ?? true,'use_pkce' => $setting->use_pkce ?? true,'clock_skew_seconds' => $setting->clock_skew_seconds ?? 60,diff --git a/app/Models/OauthSetting.php b/app/Models/OauthSetting.phpindex 25e38b4d1c..bc6fc06c17 100644--- a/app/Models/OauthSetting.php+++ b/app/Models/OauthSetting.php@@ -11,13 +11,14 @@ class OauthSetting extends Model{use HasFactory;- protected $fillable = ['provider', 'client_id', 'client_secret', 'redirect_uri', 'tenant', 'base_url', 'enabled', 'custom_label', 'scopes', 'allow_registration', 'require_email_verified', 'use_pkce', 'clock_skew_seconds'];+ protected $fillable = ['provider', 'client_id', 'client_secret', 'redirect_uri', 'tenant', 'base_url', 'enabled', 'custom_label', 'scopes', 'allow_registration', 'auto_join_root_team', 'require_email_verified', 'use_pkce', 'clock_skew_seconds'];protected function casts(): array{return ['enabled' => 'boolean','allow_registration' => 'boolean',+ 'auto_join_root_team' => 'boolean','require_email_verified' => 'boolean','use_pkce' => 'boolean','clock_skew_seconds' => 'integer',diff --git a/app/Services/Auth/OauthLoginService.php b/app/Services/Auth/OauthLoginService.phpindex f1c3b4d827..f656fad08f 100644--- a/app/Services/Auth/OauthLoginService.php+++ b/app/Services/Auth/OauthLoginService.php@@ -44,7 +44,7 @@ class OauthLoginServicethrow new HttpException(403, 'Registration is disabled');}- return $this->createUser($oauthUser->name ?: $email, $email);+ return $this->createUser($oauthUser->name ?: $email, $email, $oauthSetting);}private function resolveOidcUser(object $oauthUser, OauthSetting $oauthSetting, string $email): User@@ -100,7 +100,7 @@ class OauthLoginServicethrow new HttpException(403, 'Registration is disabled');}- $user = $this->createUser($oauthUser->name ?: $email, $email);+ $user = $this->createUser($oauthUser->name ?: $email, $email, $oauthSetting);}OauthIdentity::create([@@ -122,7 +122,7 @@ class OauthLoginServicereturn instanceSettings()->is_registration_enabled || $oauthSetting->allow_registration;}- private function createUser(string $name, string $email): User+ private function createUser(string $name, string $email, OauthSetting $oauthSetting): User{if (User::count() === 0) {$user = (new User)->forceFill([@@ -143,10 +143,34 @@ class OauthLoginServicereturn $user;}+ if ($oauthSetting->auto_join_root_team) {+ return $this->createRootTeamOnlyUser($name, $email);+ }+return User::create(['name' => $name,'email' => $email,'password' => Hash::make(Str::random(64)),]);}++ private function createRootTeamOnlyUser(string $name, string $email): User+ {+ return DB::transaction(function () use ($name, $email) {+ $rootTeam = Team::find(0);+ if ($rootTeam === null) {+ throw new HttpException(403, 'Root team is not available for OAuth user provisioning');+ }++ $user = User::withoutEvents(fn () => User::create([+ 'name' => $name,+ 'email' => $email,+ 'password' => Hash::make(Str::random(64)),+ ]));++ $user->teams()->attach($rootTeam, ['role' => 'member']);++ return $user;+ });+ }}diff --git a/database/migrations/2026_06_23_151229_add_auto_join_root_team_to_oauth_settings_table.php b/database/migrations/2026_06_23_151229_add_auto_join_root_team_to_oauth_settings_table.phpnew file mode 100644index 0000000000..b0f5aad18a--- /dev/null+++ b/database/migrations/2026_06_23_151229_add_auto_join_root_team_to_oauth_settings_table.php@@ -0,0 +1,28 @@+<?php++use Illuminate\Database\Migrations\Migration;+use Illuminate\Database\Schema\Blueprint;+use Illuminate\Support\Facades\Schema;++return new class extends Migration+{+ /**+ * Run the migrations.+ */+ public function up(): void+ {+ Schema::table('oauth_settings', function (Blueprint $table) {+ $table->boolean('auto_join_root_team')->default(false);+ });+ }++ /**+ * Reverse the migrations.+ */+ public function down(): void+ {+ Schema::table('oauth_settings', function (Blueprint $table) {+ $table->dropColumn('auto_join_root_team');+ });+ }+};diff --git a/resources/views/livewire/settings-oauth.blade.php b/resources/views/livewire/settings-oauth.blade.phpindex 8308e88d06..1ff75512e4 100644--- a/resources/views/livewire/settings-oauth.blade.php+++ b/resources/views/livewire/settings-oauth.blade.php@@ -136,6 +136,14 @@</div></div>@endif+ <div class="flex flex-col gap-2 pt-2">+ <div class="md:w-96">+ <x-forms.checkbox canGate="update" :canResource="$settings"+ id="oauth_settings_map.{{ $oauth_setting['provider'] }}.auto_join_root_team"+ label="Auto-join new users to Root team"+ helper="When enabled, newly-created OAuth users are added to the Root team as members and no personal team is created for them." />+ </div>+ </div></div></div>@endifdiff --git a/tests/Feature/OidcOauthControllerTest.php b/tests/Feature/OidcOauthControllerTest.phpindex 6ea861ec1f..21b335016e 100644--- a/tests/Feature/OidcOauthControllerTest.php+++ b/tests/Feature/OidcOauthControllerTest.php@@ -93,6 +93,40 @@ it('creates a new oidc user when provider registration is allowed while normal r]);});+it('creates a new oidc user in the root team only when provider root auto-join is enabled', function () {+ Team::forceCreate(['id' => 0, 'name' => 'Root Team', 'personal_team' => true]);+ (new User)->forceFill([+ 'id' => 0,+ 'name' => 'Root User',+ 'email' => '[email protected]',+ 'password' => 'password',+ ])->save();++ OauthSetting::where('provider', 'oidc')->update([+ 'allow_registration' => true,+ 'auto_join_root_team' => true,+ ]);++ fakeOidcProvider(['email' => '[email protected]', 'name' => 'Root Member']);++ $response = $this->get(route('auth.callback', 'oidc'));++ $response->assertRedirect('/');+ $user = User::whereEmail('[email protected]')->first();+ expect($user)->not->toBeNull()+ ->and($user->teams()->count())->toBe(1);++ $rootMembership = $user->teams()->where('teams.id', 0)->first();+ expect($rootMembership)->not->toBeNull()+ ->and($rootMembership->pivot->role)->toBe('member');++ $this->assertDatabaseMissing('teams', [+ 'name' => "Root Member's Team",+ ]);+ expect(session('currentTeam')->id)->toBe(0);+ $this->assertAuthenticatedAs($user);+});+it('rejects linking an unverified oidc email to an existing local account', function () {$user = User::factory()->create(['email' => '[email protected]']);diff --git a/tests/Feature/SettingsOauthTest.php b/tests/Feature/SettingsOauthTest.phpindex 0d25ce0f37..e55cdac4f1 100644--- a/tests/Feature/SettingsOauthTest.php+++ b/tests/Feature/SettingsOauthTest.php@@ -26,6 +26,9 @@ function actingAsInstanceAdmin(): User}beforeEach(function () {+ $this->withoutVite();+ config()->set('app.maintenance.driver', 'file');+InstanceSettings::forceCreate(['id' => 0, 'is_registration_enabled' => true]);Once::flush();OauthSetting::create(['provider' => 'oidc']);@@ -124,7 +127,8 @@ it('defaults oidc user creation and verified email requirement to enabled', func$setting = OauthSetting::where('provider', 'oidc')->first();expect($setting->allow_registration)->toBeTrue()- ->and($setting->require_email_verified)->toBeTrue();+ ->and($setting->require_email_verified)->toBeTrue()+ ->and($setting->auto_join_root_team)->toBeFalse();});it('persists oidc oauth settings from livewire', function () {@@ -139,6 +143,7 @@ it('persists oidc oauth settings from livewire', function () {->set('oauth_settings_map.oidc.scopes', 'openid email profile groups')->set('oauth_settings_map.oidc.custom_label', 'Login with Okta')->set('oauth_settings_map.oidc.allow_registration', true)+ ->set('oauth_settings_map.oidc.auto_join_root_team', true)->set('oauth_settings_map.oidc.require_email_verified', true)->set('disable_registration_when_oauth_enabled', true)->call('submit')@@ -150,7 +155,8 @@ it('persists oidc oauth settings from livewire', function () {->and($setting->base_url)->toBe('https://idp.example.com')->and($setting->custom_label)->toBe('Login with Okta')->and($setting->scopeList())->toBe(['openid', 'email', 'profile', 'groups'])- ->and($setting->allow_registration)->toBeTrue();+ ->and($setting->allow_registration)->toBeTrue()+ ->and($setting->auto_join_root_team)->toBeTrue();expect(instanceSettings()->fresh()->disable_registration_when_oauth_enabled)->toBeTrue();});