coolify/app/Livewire/Team/InviteLink.php

107 lines
3.6 KiB
PHP
Raw Normal View History

2023-06-09 15:55:21 +02:00
<?php
2023-12-07 19:06:32 +01:00
namespace App\Livewire\Team;
2023-06-09 15:55:21 +02:00
use App\Models\TeamInvitation;
use App\Models\User;
2023-09-15 11:19:36 +02:00
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
2024-06-10 22:43:34 +02:00
use Livewire\Component;
use Visus\Cuid2\Cuid2;
2023-06-09 15:55:21 +02:00
class InviteLink extends Component
{
public string $email;
2024-06-10 22:43:34 +02:00
2023-06-12 12:00:01 +02:00
public string $role = 'member';
2024-05-31 11:03:43 +02:00
protected $rules = [
'email' => 'required|email',
'role' => 'required|string',
];
2024-06-10 22:43:34 +02:00
2023-06-09 15:55:21 +02:00
public function mount()
{
$this->email = isDev() ? 'test3@example.com' : '';
2023-06-09 15:55:21 +02:00
}
2023-06-12 12:00:01 +02:00
public function viaEmail()
{
2023-09-15 11:19:36 +02:00
$this->generate_invite_link(sendEmail: true);
2023-06-12 12:00:01 +02:00
}
2023-09-15 11:19:36 +02:00
public function viaLink()
{
$this->generate_invite_link(sendEmail: false);
}
2024-06-10 22:43:34 +02:00
2023-09-15 11:19:36 +02:00
private function generate_invite_link(bool $sendEmail = false)
2023-06-09 15:55:21 +02:00
{
try {
2024-05-31 11:03:43 +02:00
$this->validate();
2023-08-22 17:44:49 +02:00
$member_emails = currentTeam()->members()->get()->pluck('email');
2023-06-12 12:00:01 +02:00
if ($member_emails->contains($this->email)) {
2024-06-10 22:43:34 +02:00
return handleError(livewire: $this, customErrorMessage: "$this->email is already a member of ".currentTeam()->name.'.');
2023-06-12 12:00:01 +02:00
}
2023-09-15 11:19:36 +02:00
$uuid = new Cuid2(32);
2024-06-10 22:43:34 +02:00
$link = url('/').config('constants.invitation.link.base_url').$uuid;
2023-09-15 11:19:36 +02:00
$user = User::whereEmail($this->email)->first();
2023-06-12 12:00:01 +02:00
2023-09-15 11:19:36 +02:00
if (is_null($user)) {
$password = Str::password();
$user = User::create([
'name' => Str::of($this->email)->before('@'),
'email' => $this->email,
'password' => Hash::make($password),
'force_password_reset' => true,
]);
$token = Crypt::encryptString("{$user->email}@@@$password");
$link = route('auth.link', ['token' => $token]);
}
$invitation = TeamInvitation::whereEmail($this->email)->first();
2024-06-10 22:43:34 +02:00
if (! is_null($invitation)) {
2023-09-15 11:19:36 +02:00
$invitationValid = $invitation->isValid();
if ($invitationValid) {
return handleError(livewire: $this, customErrorMessage: "Pending invitation already exists for $this->email.");
2023-06-09 15:55:21 +02:00
} else {
$invitation->delete();
}
}
2023-06-12 12:00:01 +02:00
2023-09-15 11:19:36 +02:00
$invitation = TeamInvitation::firstOrCreate([
2023-08-22 17:44:49 +02:00
'team_id' => currentTeam()->id,
2023-06-12 12:00:01 +02:00
'uuid' => $uuid,
2023-06-09 15:55:21 +02:00
'email' => $this->email,
2023-06-12 12:00:01 +02:00
'role' => $this->role,
2023-06-09 15:55:21 +02:00
'link' => $link,
2023-09-15 11:19:36 +02:00
'via' => $sendEmail ? 'email' : 'link',
2023-06-09 15:55:21 +02:00
]);
2023-09-15 11:19:36 +02:00
if ($sendEmail) {
$mail = new MailMessage();
$mail->view('emails.invitation-link', [
'team' => currentTeam()->name,
'invitation_link' => $link,
]);
2024-06-10 22:43:34 +02:00
$mail->subject('You have been invited to '.currentTeam()->name.' on '.config('app.name').'.');
2023-09-15 11:19:36 +02:00
send_user_an_email($mail, $this->email);
2024-02-22 14:53:42 +01:00
$this->dispatch('success', 'Invitation sent via email.');
2023-12-07 19:06:32 +01:00
$this->dispatch('refreshInvitations');
2024-06-10 22:43:34 +02:00
2023-09-15 11:19:36 +02:00
return;
2023-06-12 13:10:34 +02:00
} else {
2023-12-07 19:06:32 +01:00
$this->dispatch('success', 'Invitation link generated.');
$this->dispatch('refreshInvitations');
2023-06-12 12:00:01 +02:00
}
2023-06-09 15:55:21 +02:00
} catch (\Throwable $e) {
$error_message = $e->getMessage();
if ($e->getCode() === '23505') {
$error_message = 'Invitation already sent.';
}
2024-06-10 22:43:34 +02:00
return handleError(error: $e, livewire: $this, customErrorMessage: $error_message);
2023-06-09 15:55:21 +02:00
}
}
}