Repository navigation
feat: let API clients manage organization members and color #1569
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
Open
WendellAdriel
wants to merge
6
commits into
dev-null-sh:production
Choose a base branch
from
WendellAdriel:feat/api-organization-settings
base: production
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d1ffbf1
refactor: move the organization members query into its own class
WendellAdriel d496578
test: cover organization members and lane in the API
WendellAdriel 25cbd2f
feat: manage organization members and lane from the API
WendellAdriel a4ee8d2
docs: list organization settings in the signed API
WendellAdriel 238eadc
fix: reject non-string roles with a validation error
WendellAdriel 23cd6f2
fix: keep member adds on their own minute limit
WendellAdriel 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Http\Controllers\Api; | ||
|
|
||
| use App\Actions\UpdateOrganizationLane; | ||
| use App\Http\Requests\UpdateApiOrganizationLaneRequest; | ||
| use App\Http\Resources\OrganizationResource; | ||
| use App\Models\Organization; | ||
|
|
||
| final readonly class OrganizationLaneController | ||
| { | ||
| public function update(UpdateApiOrganizationLaneRequest $request, UpdateOrganizationLane $action): OrganizationResource | ||
| { | ||
| $organization = $request->organization(); | ||
| assert($organization instanceof Organization); | ||
|
|
||
| $action->handle($organization, $request->lane(), $request->color()); | ||
|
|
||
| return new OrganizationResource($organization); | ||
| } | ||
| } |
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,91 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Http\Controllers\Api; | ||
|
|
||
| use App\Actions\CreateOrganizationMember; | ||
| use App\Actions\DeleteOrganizationMember; | ||
| use App\Actions\UpdateOrganizationMemberRole; | ||
| use App\Enums\OrganizationRole; | ||
| use App\Exceptions\OrganizationMustHaveAnAdmin; | ||
| use App\Http\Requests\CreateApiOrganizationMemberRequest; | ||
| use App\Http\Requests\DeleteApiOrganizationMemberRequest; | ||
| use App\Http\Requests\ListApiOrganizationMembersRequest; | ||
| use App\Http\Requests\UpdateApiOrganizationMemberRequest; | ||
| use App\Http\Resources\OrganizationMemberResource; | ||
| use App\Models\Organization; | ||
| use App\Models\User; | ||
| use App\Queries\OrganizationMembersQuery; | ||
| use Illuminate\Http\JsonResponse; | ||
| use Illuminate\Http\Resources\Json\AnonymousResourceCollection; | ||
| use Illuminate\Http\Response; | ||
|
|
||
| /** | ||
| * An organization's members, as its settings manage them on the web. | ||
| */ | ||
| final readonly class OrganizationMemberController | ||
| { | ||
| public function index(ListApiOrganizationMembersRequest $request, OrganizationMembersQuery $members): AnonymousResourceCollection | ||
| { | ||
| $organization = $request->organization(); | ||
| assert($organization instanceof Organization); | ||
|
|
||
| return OrganizationMemberResource::collection($members->get($organization)); | ||
| } | ||
|
|
||
| public function store(CreateApiOrganizationMemberRequest $request, CreateOrganizationMember $action, OrganizationMembersQuery $members): JsonResponse | ||
| { | ||
| $organization = $request->organization(); | ||
| assert($organization instanceof Organization); | ||
|
|
||
| $member = $request->member(); | ||
| assert($member instanceof User); | ||
|
|
||
| $action->handle($organization, $member); | ||
|
|
||
| return new OrganizationMemberResource($members->find($organization, $member))->response()->setStatusCode(201); | ||
| } | ||
|
|
||
| public function update(UpdateApiOrganizationMemberRequest $request, string $handle, User $member, UpdateOrganizationMemberRole $action, OrganizationMembersQuery $members): OrganizationMemberResource|JsonResponse | ||
| { | ||
| $organization = $request->organization(); | ||
| assert($organization instanceof Organization); | ||
|
|
||
| $role = $request->role(); | ||
| assert($role instanceof OrganizationRole); | ||
|
|
||
| try { | ||
| $action->handle($organization, $member, $role); | ||
| } catch (OrganizationMustHaveAnAdmin $organizationMustHaveAnAdmin) { | ||
| return $this->refused('role', $organizationMustHaveAnAdmin); | ||
| } | ||
|
|
||
| return new OrganizationMemberResource($members->find($organization, $member)); | ||
| } | ||
|
|
||
| public function destroy(DeleteApiOrganizationMemberRequest $request, string $handle, User $member, DeleteOrganizationMember $action): Response|JsonResponse | ||
| { | ||
| $organization = $request->organization(); | ||
| assert($organization instanceof Organization); | ||
|
|
||
| try { | ||
| $action->handle($organization, $member); | ||
| } catch (OrganizationMustHaveAnAdmin $organizationMustHaveAnAdmin) { | ||
| return $this->refused('member', $organizationMustHaveAnAdmin); | ||
| } | ||
|
|
||
| return response()->noContent(); | ||
| } | ||
|
|
||
| /** | ||
| * Another admin was made a member or left meanwhile: the same 422 the form request gives the last admin's change. | ||
| */ | ||
| private function refused(string $field, OrganizationMustHaveAnAdmin $exception): JsonResponse | ||
| { | ||
| return response()->json([ | ||
| 'message' => $exception->getMessage(), | ||
| 'errors' => [$field => [$exception->getMessage()]], | ||
| ], 422); | ||
| } | ||
| } | ||
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,93 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Http\Requests; | ||
|
|
||
| use App\Models\Organization; | ||
| use App\Models\User; | ||
| use Illuminate\Auth\Access\Response; | ||
| use Illuminate\Foundation\Http\FormRequest; | ||
| use Illuminate\Validation\Validator; | ||
|
|
||
| final class CreateApiOrganizationMemberRequest extends FormRequest | ||
| { | ||
| private ?Organization $organization = null; | ||
|
|
||
| public function authorize(): Response | ||
| { | ||
| $user = $this->user(); | ||
| assert($user instanceof User); | ||
|
|
||
| $organization = $this->organization(); | ||
|
|
||
| return match (true) { | ||
| ! $organization instanceof Organization || ! $organization->isMember($user) => Response::denyAsNotFound(), | ||
| ! $organization->isAdmin($user) => Response::deny('Only admins can add members.'), | ||
| default => Response::allow(), | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, list<string>> | ||
| */ | ||
| public function rules(): array | ||
| { | ||
| return [ | ||
| 'handle' => ['required', 'string', 'max:40'], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @return list<callable(Validator): void> | ||
| */ | ||
| public function after(): array | ||
| { | ||
| return [function (Validator $validator): void { | ||
| if ($validator->errors()->isNotEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| $user = $this->user(); | ||
| assert($user instanceof User); | ||
|
|
||
| $organization = $this->organization(); | ||
| assert($organization instanceof Organization); | ||
|
|
||
| $member = $this->member(); | ||
|
|
||
| if (! $member instanceof User) { | ||
| $validator->errors()->add('handle', "There's no @{$this->handle()} on /dev/null."); | ||
| } elseif ($user->hasBlockWith($member)) { | ||
| $validator->errors()->add('handle', "You can't add {$member->mention()}."); | ||
| } elseif ($organization->isMember($member)) { | ||
| $validator->errors()->add('handle', "{$member->mention()} is already a member."); | ||
| } | ||
| }]; | ||
| } | ||
|
|
||
| public function organization(): ?Organization | ||
| { | ||
| $handle = $this->route('handle'); | ||
| assert(is_string($handle)); | ||
|
|
||
| return $this->organization ??= Organization::query() | ||
| ->whereRelation('profile', 'handle', $handle) | ||
| ->first(); | ||
| } | ||
|
|
||
| /** | ||
| * The user at the handle, with or without its "@", resolved like "@handle" URLs, whatever its case. | ||
| */ | ||
| public function member(): ?User | ||
| { | ||
| $member = (new User)->resolveRouteBinding($this->handle(), 'handle'); | ||
|
|
||
| return $member instanceof User ? $member : null; | ||
| } | ||
|
|
||
| private function handle(): string | ||
| { | ||
| return $this->string('handle')->trim()->ltrim('@')->value(); | ||
| } | ||
| } |
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,77 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Http\Requests; | ||
|
|
||
| use App\Models\Organization; | ||
| use App\Models\User; | ||
| use Illuminate\Auth\Access\Response; | ||
| use Illuminate\Foundation\Http\FormRequest; | ||
| use Illuminate\Validation\Validator; | ||
|
|
||
| final class DeleteApiOrganizationMemberRequest extends FormRequest | ||
| { | ||
| private ?Organization $organization = null; | ||
|
|
||
| /** | ||
| * Any member can leave; only admins can remove someone else. | ||
| */ | ||
| public function authorize(): Response | ||
| { | ||
| $user = $this->user(); | ||
| assert($user instanceof User); | ||
|
|
||
| $organization = $this->organization(); | ||
|
|
||
| return match (true) { | ||
| ! $organization instanceof Organization || ! $organization->isMember($user) || ! $organization->isMember($this->member()) => Response::denyAsNotFound(), | ||
| ! $user->is($this->member()) && ! $organization->isAdmin($user) => Response::deny('Only admins can remove members.'), | ||
| default => Response::allow(), | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * @return array{} | ||
| */ | ||
| public function rules(): array | ||
| { | ||
| return []; | ||
| } | ||
|
|
||
| /** | ||
| * @return list<callable(Validator): void> | ||
| */ | ||
| public function after(): array | ||
| { | ||
| return [function (Validator $validator): void { | ||
| $organization = $this->organization(); | ||
| assert($organization instanceof Organization); | ||
|
|
||
| $isLastAdmin = $organization->isAdmin($this->member()) | ||
| && $organization->admins()->whereKeyNot($this->member()->id)->doesntExist(); | ||
|
|
||
| if ($isLastAdmin) { | ||
| $validator->errors()->add('member', "@{$organization->handle} needs at least one admin."); | ||
| } | ||
| }]; | ||
| } | ||
|
|
||
| public function organization(): ?Organization | ||
| { | ||
| $handle = $this->route('handle'); | ||
| assert(is_string($handle)); | ||
|
|
||
| return $this->organization ??= Organization::query() | ||
| ->whereRelation('profile', 'handle', $handle) | ||
| ->first(); | ||
| } | ||
|
|
||
| public function member(): User | ||
| { | ||
| $member = $this->route('member'); | ||
| assert($member instanceof User); | ||
|
|
||
| return $member; | ||
| } | ||
| } |
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,44 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Http\Requests; | ||
|
|
||
| use App\Models\Organization; | ||
| use App\Models\User; | ||
| use Illuminate\Auth\Access\Response; | ||
| use Illuminate\Foundation\Http\FormRequest; | ||
|
|
||
| final class ListApiOrganizationMembersRequest extends FormRequest | ||
| { | ||
| private ?Organization $organization = null; | ||
|
|
||
| /** | ||
| * Only the organization's members see who its members are. To everyone else, the list doesn't exist. | ||
| */ | ||
| public function authorize(): Response | ||
| { | ||
| $user = $this->user(); | ||
| assert($user instanceof User); | ||
|
|
||
| return $this->organization()?->isMember($user) === true ? Response::allow() : Response::denyAsNotFound(); | ||
| } | ||
|
|
||
| /** | ||
| * @return array{} | ||
| */ | ||
| public function rules(): array | ||
| { | ||
| return []; | ||
| } | ||
|
|
||
| public function organization(): ?Organization | ||
| { | ||
| $handle = $this->route('handle'); | ||
| assert(is_string($handle)); | ||
|
|
||
| return $this->organization ??= Organization::query() | ||
| ->whereRelation('profile', 'handle', $handle) | ||
| ->first(); | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recheck the acting admin under the organization lock.
UpdateOrganizationMemberRole::handle()does not check the acting user. The APIupdate()and webupdate()both authorize the acting admin before the action, then call the action without that user. If another admin demotes the acting admin while the request waits on the organization lock, the change still proceeds and can promote or demote another member.Pass the acting user into the action. Check that user's admin role after the lock is acquired.
Suggested fix
+use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Support\Facades\DB;Update both callers to pass the acting user. The web
update()can use its existing#[CurrentUser] User $userpattern. The APIupdate()needs the same parameter added.Organization::isAdmin()must read the current role for the post-lock check to be valid.