121 lines
4.2 KiB
PHP
121 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Enums\OrganisationRole;
|
|
use App\Http\Requests\StoreOrganisationMemberRequest;
|
|
use App\Http\Requests\UpdateOrganisationInvitationRequest;
|
|
use App\Http\Requests\UpdateOrganisationMemberRequest;
|
|
use App\Models\Organisation;
|
|
use App\Models\OrganisationInvitation;
|
|
use App\Models\User;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Str;
|
|
use Inertia\Response;
|
|
|
|
class OrganisationMemberController extends Controller
|
|
{
|
|
public function index(Organisation $organisation): Response
|
|
{
|
|
return inertia('organisation-members/Index', [
|
|
'organisation' => $organisation->load(['members', 'invitations.invitedBy']),
|
|
'roles' => array_values(OrganisationRole::toArray()),
|
|
]);
|
|
}
|
|
|
|
public function store(StoreOrganisationMemberRequest $request, Organisation $organisation): RedirectResponse
|
|
{
|
|
$email = Str::lower($request->string('email')->toString());
|
|
$user = User::query()
|
|
->where('email', $email)
|
|
->first();
|
|
|
|
if ($user === null) {
|
|
abort_if(
|
|
$organisation->invitations()->where('email', $email)->whereNull('accepted_at')->exists(),
|
|
422,
|
|
'This email already has a pending invitation.'
|
|
);
|
|
|
|
$organisation->invitations()->create([
|
|
'email' => $email,
|
|
'role' => $request->enum('role', OrganisationRole::class),
|
|
'token' => Str::random(40),
|
|
'invited_by_user_id' => $request->user()?->id,
|
|
'expires_at' => now()->addDays(14),
|
|
]);
|
|
|
|
return redirect()
|
|
->route('organisation-members.index', ['organisation' => $organisation->id])
|
|
->with('success', 'Invitation created.');
|
|
}
|
|
|
|
$organisation->members()->syncWithoutDetaching([
|
|
$user->id => ['role' => $request->enum('role', OrganisationRole::class)],
|
|
]);
|
|
|
|
$organisation->invitations()
|
|
->where('email', $email)
|
|
->delete();
|
|
|
|
return redirect()
|
|
->route('organisation-members.index', ['organisation' => $organisation->id])
|
|
->with('success', 'Member added.');
|
|
}
|
|
|
|
public function update(UpdateOrganisationMemberRequest $request, Organisation $organisation, User $member): RedirectResponse
|
|
{
|
|
abort_unless($organisation->members()->whereKey($member->id)->exists(), 404);
|
|
|
|
$organisation->members()->updateExistingPivot($member->id, [
|
|
'role' => $request->enum('role', OrganisationRole::class),
|
|
]);
|
|
|
|
return redirect()
|
|
->route('organisation-members.index', ['organisation' => $organisation->id])
|
|
->with('success', 'Member role updated.');
|
|
}
|
|
|
|
public function updateInvitation(
|
|
UpdateOrganisationInvitationRequest $request,
|
|
Organisation $organisation,
|
|
OrganisationInvitation $invitation
|
|
): RedirectResponse {
|
|
abort_unless($invitation->organisation_id === $organisation->id, 404);
|
|
|
|
$invitation->update([
|
|
'role' => $request->enum('role', OrganisationRole::class),
|
|
]);
|
|
|
|
return redirect()
|
|
->route('organisation-members.index', ['organisation' => $organisation->id])
|
|
->with('success', 'Invitation role updated.');
|
|
}
|
|
|
|
public function destroy(Request $request, Organisation $organisation, User $member): RedirectResponse
|
|
{
|
|
abort_if($organisation->owner_id === $member->id, 422, 'The organisation owner cannot be removed.');
|
|
|
|
$organisation->members()->detach($member->id);
|
|
|
|
return redirect()
|
|
->route('organisation-members.index', ['organisation' => $organisation->id])
|
|
->with('success', 'Member removed.');
|
|
}
|
|
|
|
public function destroyInvitation(
|
|
Request $request,
|
|
Organisation $organisation,
|
|
OrganisationInvitation $invitation
|
|
): RedirectResponse {
|
|
abort_unless($invitation->organisation_id === $organisation->id, 404);
|
|
|
|
$invitation->delete();
|
|
|
|
return redirect()
|
|
->route('organisation-members.index', ['organisation' => $organisation->id])
|
|
->with('success', 'Invitation cancelled.');
|
|
}
|
|
}
|