Repository navigation
Expand file tree
/
Copy pathOrganizationAdminController.php
More file actions
77 lines (61 loc) · 2.73 KB
/
Copy pathOrganizationAdminController.php
File metadata and controls
77 lines (61 loc) · 2.73 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
<?php
declare(strict_types=1);
namespace App\Http\Controllers;
use App\Actions\CreateOrganizationAdmin;
use App\Contracts\Services\GitHubProvider;
use App\Http\Requests\CreateOrganizationAdminRequest;
use App\Models\Organization;
use App\Models\User;
use Illuminate\Container\Attributes\CurrentUser;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
/**
* Claiming an organization's page asks GitHub whether the user shows their membership of it publicly, so it needs no
* permission beyond sign-in's, and no approval from the organization's owners.
*/
final readonly class OrganizationAdminController
{
/**
* Claiming is a form, so no other site can claim a page for the user.
*/
public function store(CreateOrganizationAdminRequest $request, #[CurrentUser] User $user, Organization $organization, GitHubProvider $github, CreateOrganizationAdmin $action): RedirectResponse
{
$page = to_route('user.show', $organization->handle);
$claimed = $this->claimed($organization);
if ($claimed !== null) {
return $this->failed($claimed, $page);
}
$handle = $organization->handle;
if (! $github->isPublicMember($organization->profile->github_id, $user->github_id)) {
return $this->failed(
__('GitHub does not list you as a public member of @:handle. On GitHub, make your membership of @:handle public, then try again.', ['handle' => $handle]),
$page,
"https://github.com/orgs/{$handle}/people",
);
}
if (! $action->handle($organization, $user)) {
return $this->failed($this->claimed($organization) ?? __('Unable to claim this organization. Please try again.'), $page);
}
Inertia::flash('toast', ['type' => 'success', 'message' => __('You are now an admin of @:handle.', ['handle' => $handle])]);
return $page;
}
/**
* Only the first claim makes an admin. Why another one fails, if the page was claimed.
*/
private function claimed(Organization $organization): ?string
{
$admin = $organization->admins()->oldest('organization_members.created_at')->first();
if ($admin === null) {
return null;
}
return __('@:handle was already claimed by @:admin.', ['handle' => $organization->handle, 'admin' => $admin->handle]);
}
/**
* The page says why the claim failed until the user leaves it, with where to fix it on GitHub when there's a way.
*/
private function failed(string $message, RedirectResponse $redirect, ?string $githubUrl = null): RedirectResponse
{
Inertia::flash('claimFailed', ['message' => $message, 'githubUrl' => $githubUrl]);
return $redirect;
}
}