-
Notifications
You must be signed in to change notification settings - Fork 43
feat(squads): PromoteToSubCaptain / demote (#358) #501
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
guisaliba
wants to merge
6
commits into
feat/squads
Choose a base branch
from
story/358-promote-to-sub-captain
base: feat/squads
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 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5401268
feat(squads): add sub-captain role transitions
guisaliba 305e0e2
fix(squads): protect the captain seat from sub-captain transitions (#…
guisaliba 1558660
docs(squads): document the PromoteToSubCaptain governance scope (#358)
guisaliba 9fdd48e
Merge remote-tracking branch 'origin/feat/squads' into story/358-prom…
guisaliba fea823b
fix(squads): enforce the sub-captain transition contract (#358)
guisaliba 62ab525
docs(squads): clarify leadership transition authority (#358)
guisaliba 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace He4rt\Squads\Actions; | ||
|
|
||
| use He4rt\Identity\User\Models\User; | ||
| use He4rt\Squads\Enums\MembershipAction; | ||
| use He4rt\Squads\Enums\SquadRole; | ||
| use He4rt\Squads\Exceptions\NotAnActiveSquadMember; | ||
| use He4rt\Squads\Models\Squad; | ||
| use He4rt\Squads\Models\SquadMember; | ||
| use He4rt\Squads\Policies\SquadPolicy; | ||
| use Illuminate\Auth\Access\AuthorizationException; | ||
| use Illuminate\Support\Facades\DB; | ||
|
|
||
| /** | ||
| * Records the outcome of an off-system promotion to sub-captain, together | ||
| * with its inverse demotion back to `Member` — one governance concern in | ||
| * both directions, mirroring how `AssignCaptain` owns the captain seat. | ||
| * | ||
| * The name follows the module's action map (`CONTEXT.md`), which records | ||
| * outcomes rather than personas: this is not an action "for" a sub-captain; | ||
| * it records someone BECOMING one (or leaving that role) after a decision | ||
| * made off-platform, appending `promote`/`demote` to the audit trail. | ||
| * | ||
| * The seated captain is above sub-captain authority: only super-admins or | ||
| * the captain themselves may move that row, matching the admin-only rule | ||
| * `AssignCaptain` applies when it replaces an incumbent. | ||
| */ | ||
| final readonly class PromoteToSubCaptain | ||
| { | ||
| public function __construct( | ||
| private SquadPolicy $squadPolicy, | ||
| private RecordMembershipEvent $recordMembershipEvent, | ||
| ) {} | ||
|
|
||
| public function handle(User $actor, Squad $squad, User $subject, ?string $reason = null): SquadMember | ||
| { | ||
| $this->squadPolicy->authorize($actor, $squad); | ||
|
|
||
| return $this->transition($squad, $subject, $actor, SquadRole::SubCaptain, $reason); | ||
| } | ||
|
|
||
| public function demote(User $actor, Squad $squad, User $subject, ?string $reason = null): SquadMember | ||
| { | ||
| $this->squadPolicy->authorize($actor, $squad); | ||
|
|
||
| return $this->transition($squad, $subject, $actor, SquadRole::Member, $reason); | ||
| } | ||
|
guisaliba marked this conversation as resolved.
|
||
|
|
||
| private function transition( | ||
| Squad $squad, | ||
| User $subject, | ||
| User $actor, | ||
| SquadRole $targetRole, | ||
| ?string $reason, | ||
| ): SquadMember { | ||
| return DB::transaction(function () use ($squad, $subject, $actor, $targetRole, $reason): SquadMember { | ||
| $member = SquadMember::query() | ||
| ->where('squad_id', $squad->id) | ||
| ->where('user_id', $subject->id) | ||
| ->whereNot('role', SquadRole::ExMember) | ||
| ->lockForUpdate() | ||
| ->first(); | ||
|
|
||
| throw_if($member === null, NotAnActiveSquadMember::for($squad, $subject)); | ||
|
|
||
| $fromRole = $member->role; | ||
|
|
||
| // The captain seat is only moved by super-admins (via `AssignCaptain` | ||
| // or this action) or by the seated captain stepping down themselves. | ||
| // Leadership roles below the seat never outrank it, so they cannot | ||
| // record a transition over the current `Captain`. | ||
| throw_if( | ||
| $fromRole === SquadRole::Captain | ||
| && !$actor->isAdmin() | ||
| && !$actor->is($subject), | ||
| AuthorizationException::class, | ||
| ); | ||
|
|
||
| if ($fromRole === $targetRole) { | ||
| return $member; | ||
| } | ||
|
|
||
| $member->update([ | ||
| 'role' => $targetRole, | ||
| ]); | ||
|
|
||
| $this->recordMembershipEvent->handle( | ||
| squad: $squad, | ||
| subject: $subject, | ||
| action: $this->actionFor($fromRole, $targetRole), | ||
| fromRole: $fromRole, | ||
| toRole: $targetRole, | ||
| actor: $actor, | ||
| reason: $reason, | ||
| ); | ||
|
|
||
| return $member->refresh(); | ||
| }); | ||
| } | ||
|
|
||
| private function actionFor(SquadRole $fromRole, SquadRole $targetRole): MembershipAction | ||
| { | ||
| return $fromRole === SquadRole::Member && $targetRole === SquadRole::SubCaptain | ||
| ? MembershipAction::Promote | ||
| : MembershipAction::Demote; | ||
| } | ||
| } | ||
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.