koel/app/Services/UserInvitationService.php

74 lines
2.1 KiB
PHP
Raw Normal View History

2023-08-20 22:35:58 +00:00
<?php
namespace App\Services;
use App\Exceptions\InvitationNotFoundException;
use App\Mail\UserInvite;
use App\Models\User;
use App\Repositories\UserRepository;
2023-08-20 22:35:58 +00:00
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
2024-06-04 13:35:00 +00:00
use Illuminate\Support\Facades\Hash;
2023-08-20 22:35:58 +00:00
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Str;
class UserInvitationService
{
2024-06-04 13:35:00 +00:00
public function __construct(private readonly UserRepository $userRepository)
2023-08-20 22:35:58 +00:00
{
}
/** @return Collection<array-key, User> */
2023-08-20 22:35:58 +00:00
public function invite(array $emails, bool $isAdmin, User $invitor): Collection
{
return DB::transaction(function () use ($emails, $isAdmin, $invitor) {
return collect($emails)->map(fn ($email) => $this->inviteOne($email, $isAdmin, $invitor));
});
}
public function getUserProspectByToken(string $token): User
{
2024-06-04 13:35:00 +00:00
return User::query()->where('invitation_token', $token)->firstOr(static function (): never {
2023-08-20 22:35:58 +00:00
throw new InvitationNotFoundException();
});
}
public function revokeByEmail(string $email): void
{
$user = $this->userRepository->findOneByEmail($email);
2023-08-20 22:35:58 +00:00
throw_unless($user?->is_prospect, new InvitationNotFoundException());
$user->delete();
}
private function inviteOne(string $email, bool $isAdmin, User $invitor): User
{
$invitee = User::query()->create([
'name' => '',
'email' => $email,
'password' => '',
'is_admin' => $isAdmin,
'invited_by_id' => $invitor->id,
'invitation_token' => Str::uuid()->toString(),
'invited_at' => now(),
]);
Mail::to($email)->queue(new UserInvite($invitee));
return $invitee;
}
public function accept(string $token, string $name, string $password): User
{
$user = $this->getUserProspectByToken($token);
2024-06-04 13:35:00 +00:00
$user->update(attributes: [
2023-08-20 22:35:58 +00:00
'name' => $name,
2024-06-04 13:35:00 +00:00
'password' => Hash::make($password),
2023-08-20 22:35:58 +00:00
'invitation_token' => null,
'invitation_accepted_at' => now(),
]);
return $user;
}
}