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
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 55 additions & 3 deletions app/Filament/Resources/UserResource/Pages/Roles.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

namespace App\Filament\Resources\UserResource\Pages;

use App\Enums\Permissions;
use App\Filament\Resources\UserResource;
use App\Models\Role;
use App\Models\User;
use Filament\Resources\Pages\ManageRelatedRecords;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Spatie\Permission\Models\Role as SpatieRole;

class Roles extends ManageRelatedRecords
Expand All @@ -23,6 +26,9 @@ class Roles extends ManageRelatedRecords

public function table(Table $table): Table
{
/** @var User $user */
$user = Auth::user();

// TODO using the resource's table inherits all the actions which open in empty modals
// see https://github.com/filamentphp/filament/issues/9492
// return RoleResource::table($table)
Expand All @@ -37,16 +43,62 @@ public function table(Table $table): Table
Tables\Actions\AttachAction::make()
->label(__('Add'))
->color('primary')
->authorize(fn () => auth()->user()->can('updateRoles', $this->getRecord()))
->authorize(fn () => $user->can('updateRoles', $this->getRecord()))
->recordTitle(fn (Model $record) => __('permission.role.' . $record->name))
->preloadRecordSelect()
->recordSelectOptionsQuery(fn (Builder $query) => $query->whereIn('name', auth()->user()->assignableRoles)),
->recordSelectOptionsQuery(fn (Builder $query) => $query->whereIn('name', $user->assignableRoles))
->after(function ($data) {
/** @var User $targetUser */
$targetUser = $this->getRecord();

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

$newPermissions = Permissions::Registered;
Jamiras marked this conversation as resolved.
Show resolved Hide resolved
if ($attachedRole->name === Role::DEVELOPER_JUNIOR) {
$targetUser->removeRole(Role::DEVELOPER);
$targetUser->removeRole(Role::DEVELOPER_STAFF);
$targetUser->removeRole(Role::DEVELOPER_RETIRED);

$newPermissions = Permissions::JuniorDeveloper;
} elseif ($attachedRole->name === Role::DEVELOPER) {
$targetUser->removeRole(Role::DEVELOPER_JUNIOR);
$targetUser->removeRole(Role::DEVELOPER_STAFF);
$targetUser->removeRole(Role::DEVELOPER_RETIRED);

$newPermissions = Permissions::Developer;
} elseif ($attachedRole->name === Role::DEVELOPER_STAFF) {
$targetUser->removeRole(Role::DEVELOPER_JUNIOR);
$targetUser->removeRole(Role::DEVELOPER);
$targetUser->removeRole(Role::DEVELOPER_RETIRED);

$newPermissions = Permissions::Developer;
} elseif ($attachedRole->name === Role::DEVELOPER_RETIRED) {
$targetUser->removeRole(Role::DEVELOPER_JUNIOR);
$targetUser->removeRole(Role::DEVELOPER);
$targetUser->removeRole(Role::DEVELOPER_STAFF);
}

$currentPermissions = (int) $targetUser->getAttribute('Permissions');
// Don't strip moderation power away if the user already has it.
if ($currentPermissions < Permissions::Moderator) {
$targetUser->setAttribute('Permissions', $newPermissions);
$targetUser->save();
}
}),
])
->paginated(false)
->actions([
Tables\Actions\DetachAction::make()
->label(__('Remove'))
->authorize(fn (SpatieRole $record) => auth()->user()->can('detachRole', [$this->getRecord(), $record])),
->authorize(fn (SpatieRole $record) => $user->can('detachRole', [$this->getRecord(), $record]))
->after(function () {
/** @var User $targetUser */
$targetUser = $this->getRecord();

// Keep legacy permissions in sync.
$targetUser->setAttribute('Permissions', Permissions::Registered);
$targetUser->save();
}),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([]),
Expand Down
4 changes: 2 additions & 2 deletions app/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class Role extends \Spatie\Permission\Models\Role

// vanity roles assigned by admin

public const DEVELOPER_VETERAN = 'developer-veteran';
public const DEVELOPER_RETIRED = 'developer-retired';

public static function toFilamentColor(string $role): string
{
Expand Down Expand Up @@ -117,7 +117,7 @@ public static function toFilamentColor(string $role): string

// vanity roles assigned by admin

Role::DEVELOPER_VETERAN => 'primary',
Role::DEVELOPER_RETIRED => 'primary',
default => 'gray',
};
}
Expand Down
4 changes: 2 additions & 2 deletions config/roles.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
Role::CHEAT_INVESTIGATOR,
Role::DEVELOPER_JUNIOR,
Role::DEVELOPER_STAFF,
Role::DEVELOPER_VETERAN,
Role::DEVELOPER_RETIRED,
Role::DEVELOPER,
Role::EVENT_MANAGER,
Role::FORUM_MANAGER,
Expand Down Expand Up @@ -183,7 +183,7 @@
// vanity roles assigned by admin

[
'name' => Role::DEVELOPER_VETERAN,
'name' => Role::DEVELOPER_RETIRED,
'display' => 5,
'legacy_role' => Permissions::Registered,
],
Expand Down
2 changes: 1 addition & 1 deletion lang/en/permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@

// vanity roles assigned by admins

Role::DEVELOPER_VETERAN => __('Veteran Developer'),
Role::DEVELOPER_RETIRED => __('Developer (retired)'),
],
];
2 changes: 1 addition & 1 deletion resources/js/common/utils/generatedAppConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const UserRole = {
ENGINEER: 'engineer',
TEAM_ACCOUNT: 'team-account',
BETA: 'beta',
DEVELOPER_VETERAN: 'developer-veteran',
DEVELOPER_RETIRED: 'developer-retired',
} as const;


Expand Down
3 changes: 2 additions & 1 deletion resources/js/types/generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ declare namespace App.Models {
| 'engineer'
| 'team-account'
| 'beta'
| 'developer-veteran';
| 'developer-retired';
}
declare namespace App.Platform.Data {
export type Achievement = {
Expand Down Expand Up @@ -377,6 +377,7 @@ declare namespace App.Platform.Data {
};
}
declare namespace App.Platform.Enums {
export type AchievementAuthorTask = 'artwork' | 'design' | 'logic' | 'testing' | 'writing';
export type UnlockMode = 0 | 1;
export type AchievementFlag = 3 | 5;
export type AchievementSetType =
Expand Down