Skip to content
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

fix(manage): sync dev role attach/detach with legacy permissions value #2878

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
30 changes: 27 additions & 3 deletions app/Filament/Resources/UserResource/Pages/Roles.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ public function table(Table $table): Table

$attachedRole = Role::findById((int) $data['recordId']);

if (!in_array($attachedRole->name, [
Role::DEVELOPER_JUNIOR,
Role::DEVELOPER,
Role::DEVELOPER_STAFF,
Role::DEVELOPER_RETIRED,
])) {
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having this as a secondary gate duplicates the lower logic and creates an opportunity for them to become desync'd. Suggest adding an else return to the logic below:

else
{
    // nothing to synchronize for non-developer roles. we're done
    return;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in latest.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you missed my point. You're still checking the conditions twice.

Instead of:

if (in_array(A, B)) {
    if (A) {
    } elseif (B) {
    }
    do_stuff();
}

just do:

if (A) {
} elseif (B) {
} else {
    return;
}
do_stuff();


$newPermissions = Permissions::Registered;
Jamiras marked this conversation as resolved.
Show resolved Hide resolved
if ($attachedRole->name === Role::DEVELOPER_JUNIOR) {
$targetUser->removeRole(Role::DEVELOPER);
Expand Down Expand Up @@ -91,13 +100,28 @@ public function table(Table $table): Table
Tables\Actions\DetachAction::make()
->label(__('Remove'))
->authorize(fn (SpatieRole $record) => $user->can('detachRole', [$this->getRecord(), $record]))
->after(function () {
->after(function (SpatieRole $record) {
/** @var User $targetUser */
$targetUser = $this->getRecord();

// Only reset permissions if it's a developer role being removed.
// Users can only have a single kind of developer role attached.
if (!in_array($record->name, [
Role::DEVELOPER_JUNIOR,
Role::DEVELOPER,
Role::DEVELOPER_STAFF,
Role::DEVELOPER_RETIRED,
])) {
return;
}

// Keep legacy permissions in sync.
$targetUser->setAttribute('Permissions', Permissions::Registered);
$targetUser->save();
$currentPermissions = (int) $targetUser->getAttribute('Permissions');
// Don't strip moderation power away if the user already has it.
if ($currentPermissions < Permissions::Moderator) {
$targetUser->setAttribute('Permissions', Permissions::Registered);
$targetUser->save();
}
}),
])
->bulkActions([
Expand Down