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