Repository navigation
feat: let organization members claim their page #1273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e58d6d9
feat: let the members of an organization on GitHub claim its page
mnapoli e9a23c2
refactor: claim organizations through their admins relation
mnapoli 9721cad
refactor: erase and export claims through the user's organizations re…
mnapoli 157861c
chore: date the privacy policy from the organization claims
mnapoli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Actions; | ||
|
|
||
| use App\Models\Profile; | ||
| use App\Models\User; | ||
|
|
||
| final readonly class CreateOrganizationAdmin | ||
| { | ||
| /** | ||
| * Makes the user an admin of the organization's page, once GitHub confirmed they belong to it. Claiming again | ||
| * changes nothing. | ||
| */ | ||
| public function handle(Profile $organization, User $user): void | ||
| { | ||
| $organization->admins()->syncWithoutDetaching([$user->id]); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Enums; | ||
|
|
||
| /** | ||
| * What GitHub says about a user's place in an organization, when they ask with the read:org scope. | ||
| */ | ||
| enum GitHubOrganizationMembership | ||
| { | ||
| /** An owner or a member, who accepted the invitation. */ | ||
| case Member; | ||
|
|
||
| case NotMember; | ||
|
|
||
| /** The organization restricts OAuth apps, and its owners haven't approved /dev/null. */ | ||
| case Restricted; | ||
|
|
||
| /** The organization signs its members in with SAML, and the user hasn't signed in that way. */ | ||
| case SingleSignOn; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Http\Controllers; | ||
|
|
||
| use App\Actions\CreateOrganizationAdmin; | ||
| use App\Enums\GitHubOrganizationMembership; | ||
| use App\Enums\ProfileType; | ||
| use App\Http\Requests\CreateOrganizationAdminRequest; | ||
| use App\Models\Profile; | ||
| use App\Models\User; | ||
| use App\Services\GitHub; | ||
| use Illuminate\Container\Attributes\CurrentUser; | ||
| use Illuminate\Http\RedirectResponse; | ||
| use Illuminate\Http\Request; | ||
| use Inertia\Inertia; | ||
| use Laravel\Socialite\Socialite; | ||
| use Laravel\Socialite\Two\GithubProvider; | ||
| use Laravel\Socialite\Two\InvalidStateException; | ||
| use Laravel\Socialite\Two\User as GitHubUser; | ||
| use Symfony\Component\HttpFoundation\Response as SymfonyResponse; | ||
|
|
||
| /** | ||
| * Claiming an organization's page asks GitHub, with the read:org scope sign-in doesn't ask for, whether the user | ||
| * belongs to the organization. The token is only used for that, and isn't kept. | ||
| */ | ||
| final readonly class OrganizationAdminController | ||
| { | ||
| private const string CLAIMING = 'claiming-organization'; | ||
|
|
||
| /** | ||
| * Claiming starts from a form, so no other site can send a user who already granted the scope through it. | ||
| */ | ||
| public function create(CreateOrganizationAdminRequest $request, Profile $organization): SymfonyResponse | ||
| { | ||
| $request->session()->put(self::CLAIMING, $organization->id); | ||
|
|
||
| return Inertia::location($this->github()->scopes(['read:org'])->redirect()->getTargetUrl()); | ||
| } | ||
|
|
||
| /** | ||
| * GitHub only sends back to a path under the callback registered for sign-in, so this is where it lands. | ||
| */ | ||
| public function store(Request $request, #[CurrentUser] User $user, GitHub $github, CreateOrganizationAdmin $action): RedirectResponse | ||
| { | ||
| $organization = Profile::query() | ||
| ->where('type', ProfileType::Organization) | ||
| ->find($request->session()->pull(self::CLAIMING)); | ||
|
|
||
| if (! $organization instanceof Profile) { | ||
| Inertia::flash('toast', ['type' => 'error', 'message' => __('Unable to claim this organization. Please try again.')]); | ||
|
|
||
| return to_route('log.show', $user->log); | ||
| } | ||
|
|
||
| $page = to_route('user.show', $organization->handle); | ||
|
|
||
| if ($request->has('error')) { | ||
| return $this->failed(__('GitHub was not asked about your organizations, so the page was not claimed.'), $page); | ||
| } | ||
|
|
||
| try { | ||
| $githubUser = $this->github()->user(); | ||
| } catch (InvalidStateException) { | ||
| return $this->failed(__('Unable to claim this organization. Please try again.'), $page); | ||
| } | ||
|
|
||
| assert($githubUser instanceof GitHubUser); | ||
|
|
||
| if ((string) $githubUser->getId() !== $user->github_id) { | ||
| return $this->failed(__('GitHub is signed in as @:login. Sign in there as @:handle to claim this page.', [ | ||
| 'login' => $githubUser->getNickname(), | ||
| 'handle' => $user->profile->handle, | ||
| ]), $page); | ||
| } | ||
|
|
||
| $handle = $organization->handle; | ||
|
|
||
| // An organization restricting OAuth apps hides its members from those its owners haven't approved, which they | ||
| // do from the app's page in their settings, where members request it. | ||
| $failure = match ($github->organizationMembership($githubUser->token, $handle, $organization->github_id)) { | ||
| GitHubOrganizationMembership::Member => null, | ||
| GitHubOrganizationMembership::NotMember => [__('GitHub says you are not a member of @:handle.', ['handle' => $handle]), null], | ||
| GitHubOrganizationMembership::Restricted => [ | ||
| __('@:handle only shares its members with apps its owners approved. On GitHub, grant /dev/null access if you are an owner, or request it from them, then try again.', ['handle' => $handle]), | ||
| 'https://github.com/settings/connections/applications/'.config()->string('services.github.client_id'), | ||
| ], | ||
| GitHubOrganizationMembership::SingleSignOn => [ | ||
| __('@:handle signs its members in with single sign-on. Sign in to @:handle on GitHub, then try again.', ['handle' => $handle]), | ||
| "https://github.com/orgs/{$handle}/sso", | ||
| ], | ||
| }; | ||
|
|
||
| if ($failure !== null) { | ||
| return $this->failed($failure[0], $page, $failure[1]); | ||
| } | ||
|
|
||
| $action->handle($organization, $user); | ||
|
|
||
| Inertia::flash('toast', ['type' => 'success', 'message' => __('You are now an admin of @:handle.', ['handle' => $organization->handle])]); | ||
|
|
||
| return $page; | ||
| } | ||
|
|
||
| /** | ||
| * GitHub sends the user back here rather than to sign-in, and checks the token is redeemed for the same URL. | ||
| */ | ||
| private function github(): GithubProvider | ||
| { | ||
| $provider = Socialite::driver('github'); | ||
| assert($provider instanceof GithubProvider); | ||
|
|
||
| return $provider->redirectUrl(route('organization-admin.store')); | ||
| } | ||
|
|
||
| /** | ||
| * The page says why the claim failed until the user leaves it, with where to approve /dev/null when that's why. | ||
| */ | ||
| private function failed(string $message, RedirectResponse $redirect, ?string $approvalUrl = null): RedirectResponse | ||
| { | ||
| Inertia::flash('claimFailed', ['message' => $message, 'approvalUrl' => $approvalUrl]); | ||
|
|
||
| return $redirect; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Http\Requests; | ||
|
|
||
| use App\Enums\ProfileType; | ||
| use App\Models\Profile; | ||
| use Illuminate\Auth\Access\Response; | ||
| use Illuminate\Foundation\Http\FormRequest; | ||
|
|
||
| final class CreateOrganizationAdminRequest extends FormRequest | ||
| { | ||
| /** | ||
| * Only organizations' pages can be claimed. A user's profile looks like it does not exist. | ||
| */ | ||
| public function authorize(): Response | ||
| { | ||
| $organization = $this->route('organization'); | ||
| assert($organization instanceof Profile); | ||
|
|
||
| return $organization->type === ProfileType::Organization ? Response::allow() : Response::denyAsNotFound(); | ||
| } | ||
|
|
||
| /** | ||
| * @return array{} | ||
| */ | ||
| public function rules(): array | ||
| { | ||
| return []; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.