Repository navigation
Expand file tree
/
Copy pathOrganizationMemberController.php
More file actions
101 lines (85 loc) · 3.9 KB
/
Copy pathOrganizationMemberController.php
File metadata and controls
101 lines (85 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
declare(strict_types=1);
namespace App\Http\Controllers;
use App\Actions\CreateOrganizationMember;
use App\Actions\DeleteOrganizationMember;
use App\Actions\UpdateOrganizationMemberRole;
use App\Enums\OrganizationRole;
use App\Enums\UserUpdate;
use App\Exceptions\OrganizationMustHaveAnAdmin;
use App\Http\Requests\CreateOrganizationMemberRequest;
use App\Http\Requests\DeleteOrganizationMemberRequest;
use App\Http\Requests\ListOrganizationMembersRequest;
use App\Http\Requests\UpdateOrganizationMemberRequest;
use App\Models\Organization;
use App\Models\User;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Container\Attributes\CurrentUser;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;
/**
* The organization's members, in its settings. GitHub only lets the first admin in: admins manage everyone else here.
*/
final readonly class OrganizationMemberController
{
/**
* Every member sees who the members are, first joined first. Only admins can change them.
*/
public function index(ListOrganizationMembersRequest $request, #[CurrentUser] User $user, Organization $organization): Response
{
return Inertia::render('organization-member/index', [
'organization' => $organization,
// Changed by another admin, the members and the user's own role read again, the form being filled in stays.
'role' => Inertia::live(fn (): ?OrganizationRole => $organization->roleOf($user), UserUpdate::Account->value, new PrivateChannel($user)),
'members' => Inertia::live(fn (): Collection => $organization->members()
->oldest('organization_members.created_at')
->withCasts(['joined_at' => 'datetime'])
->get(['users.*', 'organization_members.role', 'organization_members.created_at as joined_at'])
->makeHidden('pivot'), UserUpdate::Account->value, new PrivateChannel($user)),
]);
}
public function store(CreateOrganizationMemberRequest $request, Organization $organization, CreateOrganizationMember $action): RedirectResponse
{
$member = $request->member();
assert($member instanceof User);
$action->handle($organization, $member);
return back();
}
public function update(UpdateOrganizationMemberRequest $request, Organization $organization, User $member, UpdateOrganizationMemberRole $action): RedirectResponse
{
$role = $request->role();
assert($role instanceof OrganizationRole);
try {
$action->handle($organization, $member, $role);
} catch (OrganizationMustHaveAnAdmin $organizationMustHaveAnAdmin) {
return $this->refused($organizationMustHaveAnAdmin);
}
return back();
}
/**
* Leaving takes the user to the organization's page, as its settings are for members only.
*/
public function destroy(DeleteOrganizationMemberRequest $request, #[CurrentUser] User $user, Organization $organization, User $member, DeleteOrganizationMember $action): RedirectResponse
{
try {
$action->handle($organization, $member);
} catch (OrganizationMustHaveAnAdmin $organizationMustHaveAnAdmin) {
return $this->refused($organizationMustHaveAnAdmin);
}
if ($user->is($member)) {
Inertia::flash('toast', ['type' => 'success', 'message' => __('You left @:handle.', ['handle' => $organization->handle])]);
return to_route('user.show', $organization->handle);
}
return back();
}
/**
* Another admin was made a member or left meanwhile, so the change would have left no admin.
*/
private function refused(OrganizationMustHaveAnAdmin $exception): RedirectResponse
{
Inertia::flash('toast', ['type' => 'error', 'message' => $exception->getMessage()]);
return back();
}
}