first commit
This commit is contained in:
77
app/Http/Controllers/Teams/TeamInvitationController.php
Normal file
77
app/Http/Controllers/Teams/TeamInvitationController.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Teams;
|
||||
|
||||
use App\Enums\TeamRole;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Teams\AcceptTeamInvitationRequest;
|
||||
use App\Http\Requests\Teams\CreateTeamInvitationRequest;
|
||||
use App\Models\Team;
|
||||
use App\Models\TeamInvitation;
|
||||
use App\Notifications\Teams\TeamInvitation as TeamInvitationNotification;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
|
||||
class TeamInvitationController extends Controller
|
||||
{
|
||||
/**
|
||||
* Store a newly created invitation.
|
||||
*/
|
||||
public function store(CreateTeamInvitationRequest $request, Team $team): RedirectResponse
|
||||
{
|
||||
Gate::authorize('inviteMember', $team);
|
||||
|
||||
$invitation = $team->invitations()->create([
|
||||
'email' => $request->validated('email'),
|
||||
'role' => TeamRole::from($request->validated('role')),
|
||||
'invited_by' => $request->user()->id,
|
||||
'expires_at' => now()->addDays(3),
|
||||
]);
|
||||
|
||||
Notification::route('mail', $invitation->email)
|
||||
->notify(new TeamInvitationNotification($invitation));
|
||||
|
||||
return to_route('teams.edit', ['team' => $team->slug]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel the specified invitation.
|
||||
*/
|
||||
public function destroy(Team $team, TeamInvitation $invitation): RedirectResponse
|
||||
{
|
||||
abort_unless($invitation->team_id === $team->id, 404);
|
||||
|
||||
Gate::authorize('cancelInvitation', $team);
|
||||
|
||||
$invitation->delete();
|
||||
|
||||
return to_route('teams.edit', ['team' => $team->slug]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Accept the invitation.
|
||||
*/
|
||||
public function accept(AcceptTeamInvitationRequest $request, TeamInvitation $invitation): RedirectResponse
|
||||
{
|
||||
$user = $request->user();
|
||||
|
||||
DB::transaction(function () use ($user, $invitation) {
|
||||
$team = $invitation->team;
|
||||
|
||||
$membership = $team->memberships()->firstOrCreate(
|
||||
['user_id' => $user->id],
|
||||
['role' => $invitation->role],
|
||||
);
|
||||
|
||||
$wasRecentlyCreated = $membership->wasRecentlyCreated;
|
||||
|
||||
$invitation->update(['accepted_at' => now()]);
|
||||
|
||||
$user->switchTeam($team);
|
||||
});
|
||||
|
||||
return to_route('dashboard');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user