2026-01-20 19:53:54 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
|
|
2026-04-15 01:22:04 +00:00
|
|
|
use App\Enums\UserWorkspace\Role;
|
2026-01-20 19:53:54 +00:00
|
|
|
use App\Http\Controllers\Controller;
|
2026-04-15 01:22:04 +00:00
|
|
|
use App\Models\Invite;
|
|
|
|
|
use App\Models\Workspace;
|
2026-01-20 19:53:54 +00:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Inertia\Inertia;
|
|
|
|
|
use Inertia\Response;
|
|
|
|
|
|
|
|
|
|
class AcceptInviteController extends Controller
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Display the invite view.
|
|
|
|
|
*/
|
2026-04-15 01:22:04 +00:00
|
|
|
public function show(Invite $invite): Response
|
2026-01-20 19:53:54 +00:00
|
|
|
{
|
2026-04-15 01:22:04 +00:00
|
|
|
$invite->load('account');
|
2026-01-20 19:53:54 +00:00
|
|
|
|
|
|
|
|
return Inertia::render('auth/AcceptInvite', [
|
|
|
|
|
'invite' => [
|
|
|
|
|
'id' => $invite->id,
|
|
|
|
|
'email' => $invite->email,
|
2026-04-15 01:22:04 +00:00
|
|
|
'account' => [
|
|
|
|
|
'id' => $invite->account->id,
|
|
|
|
|
'name' => $invite->account->name,
|
2026-01-20 19:53:54 +00:00
|
|
|
],
|
2026-04-15 01:22:04 +00:00
|
|
|
'workspaces' => $invite->workspaces,
|
2026-01-20 19:53:54 +00:00
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Accept the invite.
|
|
|
|
|
*/
|
2026-04-15 01:22:04 +00:00
|
|
|
public function accept(Request $request, Invite $invite): RedirectResponse
|
2026-01-20 19:53:54 +00:00
|
|
|
{
|
|
|
|
|
$user = $request->user();
|
|
|
|
|
|
|
|
|
|
// Verify the invite is for this user
|
|
|
|
|
if ($invite->email !== $user->email) {
|
2026-01-22 01:08:18 +00:00
|
|
|
session()->flash('flash.banner', __('settings.members.flash.wrong_email'));
|
2026-01-20 19:53:54 +00:00
|
|
|
session()->flash('flash.bannerStyle', 'danger');
|
|
|
|
|
|
2026-03-29 22:24:28 +00:00
|
|
|
return redirect()->route('app.calendar');
|
2026-01-20 19:53:54 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-15 01:22:04 +00:00
|
|
|
// Check if already a member of the account
|
|
|
|
|
if ($user->account_id === $invite->account_id) {
|
|
|
|
|
$invite->update(['accepted_at' => now()]);
|
2026-01-20 19:53:54 +00:00
|
|
|
|
2026-01-22 01:08:18 +00:00
|
|
|
session()->flash('flash.banner', __('settings.members.flash.already_member'));
|
2026-01-20 19:53:54 +00:00
|
|
|
session()->flash('flash.bannerStyle', 'info');
|
|
|
|
|
|
2026-03-29 22:24:28 +00:00
|
|
|
return redirect()->route('app.calendar');
|
2026-01-20 19:53:54 +00:00
|
|
|
}
|
|
|
|
|
|
2026-04-15 01:22:04 +00:00
|
|
|
// Add user to the account
|
|
|
|
|
$user->update(['account_id' => $invite->account_id]);
|
|
|
|
|
|
|
|
|
|
// Attach user to the invited workspaces
|
|
|
|
|
if ($invite->workspaces) {
|
|
|
|
|
foreach ($invite->workspaces as $workspaceId) {
|
|
|
|
|
$workspace = Workspace::find($workspaceId);
|
|
|
|
|
|
|
|
|
|
if ($workspace && $workspace->account_id === $invite->account_id) {
|
|
|
|
|
$workspace->members()->syncWithoutDetaching([
|
|
|
|
|
$user->id => ['role' => Role::Member->value],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// Set first workspace as current
|
|
|
|
|
if (! $user->current_workspace_id) {
|
|
|
|
|
$user->update(['current_workspace_id' => $workspace->id]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$invite->update(['accepted_at' => now()]);
|
2026-01-20 19:53:54 +00:00
|
|
|
|
2026-01-22 01:08:18 +00:00
|
|
|
session()->flash('flash.banner', __('settings.members.flash.invite_accepted'));
|
2026-01-20 19:53:54 +00:00
|
|
|
session()->flash('flash.bannerStyle', 'success');
|
|
|
|
|
|
2026-03-29 22:24:28 +00:00
|
|
|
return redirect()->route('app.calendar');
|
2026-01-20 19:53:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Decline the invite.
|
|
|
|
|
*/
|
2026-04-15 01:22:04 +00:00
|
|
|
public function decline(Request $request, Invite $invite): RedirectResponse
|
2026-01-20 19:53:54 +00:00
|
|
|
{
|
|
|
|
|
$user = $request->user();
|
|
|
|
|
|
|
|
|
|
// Verify the invite is for this user
|
|
|
|
|
if ($invite->email !== $user->email) {
|
2026-01-22 01:08:18 +00:00
|
|
|
session()->flash('flash.banner', __('settings.members.flash.wrong_email'));
|
2026-01-20 19:53:54 +00:00
|
|
|
session()->flash('flash.bannerStyle', 'danger');
|
|
|
|
|
|
2026-03-29 22:24:28 +00:00
|
|
|
return redirect()->route('app.calendar');
|
2026-01-20 19:53:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$invite->delete();
|
|
|
|
|
|
2026-01-22 01:08:18 +00:00
|
|
|
session()->flash('flash.banner', __('settings.members.flash.invite_declined'));
|
2026-01-20 19:53:54 +00:00
|
|
|
session()->flash('flash.bannerStyle', 'info');
|
|
|
|
|
|
2026-03-29 22:24:28 +00:00
|
|
|
return redirect()->route('app.calendar');
|
2026-01-20 19:53:54 +00:00
|
|
|
}
|
|
|
|
|
}
|