Add invite-only registration mode

This commit is contained in:
Constantin Graf
2026-08-31 13:14:09 +02:00
parent 303de4930d
commit 3161bbef4a
12 changed files with 82 additions and 17 deletions

View File

@@ -63,11 +63,18 @@ class CreateNewUser implements CreatesNewUsers
],
])->validate();
if ($registrationMode === RegistrationMode::InviteOnly
&& ! app(InvitationService::class)->hasInvitationForEmail((string) $validated['email'])) {
throw ValidationException::withMessages([
'email' => [__('Registration is only available to invited users.')],
]);
if ($registrationMode === RegistrationMode::InviteOnly) {
$invitationService = app(InvitationService::class);
$email = (string) $validated['email'];
if (! $invitationService->hasAcceptedInvitationForEmail($email)) {
$message = $invitationService->hasPendingInvitationForEmail($email)
? __('Please accept the organization invitation sent to your email address before registering.')
: __('Registration is only available to invited users.');
throw ValidationException::withMessages([
'email' => [$message],
]);
}
}
$timezone = null;

View File

@@ -22,7 +22,7 @@ enum RegistrationMode: string
return match (strtolower(trim((string) $value))) {
'1', 'on', 'true' => self::On,
'invite', 'invite-only' => self::InviteOnly,
'invite-only' => self::InviteOnly,
default => self::Off,
};
}

View File

@@ -36,6 +36,7 @@ class OrganizationInvitationController extends Controller
}
return redirect(route('register'))
->with('registration_email', $email)
->with('bannerText', __('Please create an account to finish joining the :organization organization.', [
'organization' => $organization->name,
]))

View File

@@ -45,6 +45,7 @@ class FortifyServiceProvider extends ServiceProvider
Fortify::registerView(function () {
return Inertia::render('Auth/Register', [
'email' => session('registration_email', ''),
'terms_url' => config('auth.terms_url'),
'privacy_policy_url' => config('auth.privacy_policy_url'),
'newsletter_consent' => config('auth.newsletter_consent'),

View File

@@ -18,10 +18,19 @@ use Illuminate\Support\Facades\Mail;
class InvitationService
{
public function hasInvitationForEmail(string $email): bool
public function hasAcceptedInvitationForEmail(string $email): bool
{
return OrganizationInvitation::query()
->whereRaw('lower(email) = ?', [strtolower($email)])
->whereNotNull('accepted_at')
->exists();
}
public function hasPendingInvitationForEmail(string $email): bool
{
return OrganizationInvitation::query()
->whereRaw('lower(email) = ?', [strtolower($email)])
->whereNull('accepted_at')
->exists();
}