Token generation: - Replace Str::ulid() with bin2hex(random_bytes(32)) for 256-bit entropy - Store SHA-256 hash in database, never plaintext tokens - Hash input before lookup on all token endpoints Invitation tokens: - InvitationService: generate crypto random, store hash, pass plain token transiently for email URL via UserInvitation::$plainToken - InvitationController show/accept: hash input before DB lookup - AcceptInvitationRequest: hash token before invitation lookup - Migration: widen user_invitations.token and artists.portal_token from char(26) to char(64) for SHA-256 hex digests Portal token auth: - PortalTokenController: remove Schema::hasTable() runtime checks, hash token before lookup, return shaped response via PortalEventResource instead of raw model data - Create PortalEventResource (name, dates, status only — no internals) - Handle missing production_requests table gracefully via try/catch Portal token middleware: - Implement full token validation: extract from Bearer header or ?token= query param, hash, look up in artists/production_requests, verify event exists and is not draft/closed, set portal context on request - Return generic 401 on any failure (no information leakage) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
116 lines
3.6 KiB
PHP
116 lines
3.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Mail\InvitationMail;
|
|
use App\Models\Organisation;
|
|
use App\Models\User;
|
|
use App\Models\UserInvitation;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
final class InvitationService
|
|
{
|
|
public function invite(Organisation $org, string $email, string $role, User $invitedBy): UserInvitation
|
|
{
|
|
$existingInvitation = UserInvitation::where('email', $email)
|
|
->where('organisation_id', $org->id)
|
|
->pending()
|
|
->where('expires_at', '>', now())
|
|
->first();
|
|
|
|
if ($existingInvitation) {
|
|
throw ValidationException::withMessages([
|
|
'email' => ['Er is al een openstaande uitnodiging voor dit e-mailadres.'],
|
|
]);
|
|
}
|
|
|
|
$existingMember = $org->users()->where('email', $email)->first();
|
|
|
|
if ($existingMember) {
|
|
throw ValidationException::withMessages([
|
|
'email' => ['Gebruiker is al lid van deze organisatie.'],
|
|
]);
|
|
}
|
|
|
|
$plainToken = bin2hex(random_bytes(32));
|
|
|
|
$invitation = new UserInvitation(['email' => $email]);
|
|
$invitation->invited_by_user_id = $invitedBy->id;
|
|
$invitation->organisation_id = $org->id;
|
|
$invitation->role = $role;
|
|
$invitation->token = hash('sha256', $plainToken);
|
|
$invitation->status = 'pending';
|
|
$invitation->expires_at = now()->addDays(7);
|
|
$invitation->save();
|
|
|
|
// Set transient plain token for use in the email URL
|
|
$invitation->plainToken = $plainToken;
|
|
|
|
Mail::to($email)->queue(new InvitationMail($invitation));
|
|
|
|
activity('invitation')
|
|
->performedOn($invitation)
|
|
->causedBy($invitedBy)
|
|
->withProperties(['email' => $email, 'role' => $role])
|
|
->log("Invited {$email} as {$role}");
|
|
|
|
return $invitation;
|
|
}
|
|
|
|
public function accept(UserInvitation $invitation, ?string $password = null): User
|
|
{
|
|
if (! $invitation->isPending() || $invitation->isExpired()) {
|
|
throw ValidationException::withMessages([
|
|
'token' => ['Deze uitnodiging is niet meer geldig.'],
|
|
]);
|
|
}
|
|
|
|
$user = User::where('email', $invitation->email)->first();
|
|
|
|
if (! $user) {
|
|
if (! $password) {
|
|
throw ValidationException::withMessages([
|
|
'password' => ['Een wachtwoord is vereist om een nieuw account aan te maken.'],
|
|
]);
|
|
}
|
|
|
|
$user = User::create([
|
|
'first_name' => Str::before($invitation->email, '@'),
|
|
'last_name' => '',
|
|
'email' => $invitation->email,
|
|
'password' => $password,
|
|
'email_verified_at' => now(),
|
|
]);
|
|
|
|
app(PersonIdentityService::class)->detectMatchesForUser($user);
|
|
}
|
|
|
|
$organisation = $invitation->organisation;
|
|
|
|
if (! $organisation->users()->where('user_id', $user->id)->exists()) {
|
|
$organisation->users()->attach($user, ['role' => $invitation->role]);
|
|
}
|
|
|
|
$invitation->markAsAccepted();
|
|
|
|
activity('invitation')
|
|
->performedOn($invitation)
|
|
->causedBy($user)
|
|
->withProperties(['email' => $invitation->email])
|
|
->log("Accepted invitation for {$organisation->name}");
|
|
|
|
return $user;
|
|
}
|
|
|
|
public function expireOldInvitations(): int
|
|
{
|
|
return UserInvitation::where('status', 'pending')
|
|
->where('expires_at', '<', now())
|
|
->update(['status' => 'expired']);
|
|
}
|
|
}
|