coolify/app/Providers/FortifyServiceProvider.php

139 lines
5.4 KiB
PHP
Raw Normal View History

<?php
namespace App\Providers;
use App\Actions\Fortify\CreateNewUser;
use App\Actions\Fortify\ResetUserPassword;
use App\Actions\Fortify\UpdateUserPassword;
use App\Actions\Fortify\UpdateUserProfileInformation;
use App\Models\OauthSetting;
use App\Models\TeamInvitation;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\ServiceProvider;
2023-06-01 10:15:33 +00:00
use Laravel\Fortify\Contracts\RegisterResponse;
use Laravel\Fortify\Fortify;
class FortifyServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
2023-08-11 18:48:52 +00:00
$this->app->instance(RegisterResponse::class, new class implements RegisterResponse
{
2023-06-01 10:15:33 +00:00
public function toResponse($request)
{
// First user (root) will be redirected to /settings instead of / on registration.
if ($request->user()->currentTeam->id === 0) {
2024-01-07 15:23:41 +00:00
return redirect()->route('settings.index');
2023-06-01 10:15:33 +00:00
}
2024-06-10 20:43:34 +00:00
2023-12-27 15:45:01 +00:00
return redirect(RouteServiceProvider::HOME);
2023-06-01 10:15:33 +00:00
}
});
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
2023-06-01 10:15:33 +00:00
Fortify::createUsersUsing(CreateNewUser::class);
2023-04-25 08:06:45 +00:00
Fortify::registerView(function () {
$settings = instanceSettings();
if (! $settings->is_registration_enabled) {
2023-04-27 09:29:02 +00:00
return redirect()->route('login');
2023-04-25 08:06:45 +00:00
}
2024-12-09 15:37:36 +00:00
$isFirstUser = User::count() === 0;
2024-12-09 15:37:36 +00:00
return view('auth.register', [
'isFirstUser' => $isFirstUser,
]);
2023-04-25 08:06:45 +00:00
});
Fortify::loginView(function () {
$settings = instanceSettings();
$enabled_oauth_providers = OauthSetting::where('enabled', true)->get();
$users = User::count();
// MapleDeploy branding: public registration is disabled by default
// because the dashboard creates the first admin over SSH. Do not
// redirect to /register in that fail-closed state, or fresh/failed
// provisioning loops between login and registration.
if ($users == 0 && $settings->is_registration_enabled) {
return redirect()->route('register');
}
2024-06-10 20:43:34 +00:00
2023-04-25 08:06:45 +00:00
return view('auth.login', [
'is_registration_enabled' => $settings->is_registration_enabled,
'enabled_oauth_providers' => $enabled_oauth_providers,
2023-04-25 08:06:45 +00:00
]);
});
Fortify::authenticateUsing(function (Request $request) {
$email = strtolower($request->email);
$user = User::where('email', $email)->with('teams')->first();
if (
$user &&
Hash::check($request->password, $user->password)
) {
2023-08-17 13:18:03 +00:00
$user->updated_at = now();
$user->save();
// Check if user has a pending invitation they haven't accepted yet
$invitation = TeamInvitation::whereEmail($email)->first();
if ($invitation && $invitation->isValid()) {
// User is logging in for the first time after being invited
// Attach them to the invited team if not already attached
if (! $user->teams()->where('team_id', $invitation->team->id)->exists()) {
$user->teams()->attach($invitation->team->id, ['role' => $invitation->role]);
}
$user->currentTeam = $invitation->team;
$invitation->delete();
session(['currentTeam' => $user->currentTeam]);
} else {
// Restore the last active team; only fall back when unambiguous.
$team = $user->resolveStoredTeam();
// MapleDeploy branding: without a stored choice, root-team
// admins should land in the managed instance team, not an
// empty personal team or the team-selection screen.
if (! $team) {
$team = $user->mapledeployPreferredTeam();
}
if (! $team && $user->teams->isEmpty()) {
$team = $user->recreate_personal_team();
}
if ($team) {
session(['currentTeam' => $user->currentTeam = $team]);
}
// Otherwise (multiple teams, no stored choice) leave the session
// team unset so the user is sent to the team-selection screen.
2024-03-05 08:19:15 +00:00
}
2024-06-10 20:43:34 +00:00
return $user;
}
});
2023-06-01 06:19:33 +00:00
Fortify::requestPasswordResetLinkView(function () {
return view('auth.forgot-password');
});
2023-06-01 10:15:33 +00:00
Fortify::resetPasswordView(function ($request) {
return view('auth.reset-password', ['request' => $request]);
});
Fortify::resetUserPasswordsUsing(ResetUserPassword::class);
Fortify::updateUserProfileInformationUsing(UpdateUserProfileInformation::class);
Fortify::updateUserPasswordsUsing(UpdateUserPassword::class);
2023-06-01 10:15:33 +00:00
Fortify::confirmPasswordView(function () {
return view('auth.confirm-password');
});
Fortify::twoFactorChallengeView(function () {
return view('auth.two-factor-challenge');
});
}
}