Skip to content

Commit

Permalink
Merge pull request #472 from OpenSynergic/fix--allow-edit-admin-user-…
Browse files Browse the repository at this point in the history
…self

fix: allow admin user to edit themself
  • Loading branch information
rahmanramsi authored Dec 21, 2024
2 parents b9dbf11 + 1984d84 commit ec7d31a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ function ($object) use ($roles, $model, $teamPivot) {
public function syncRoles(...$roles)
{
if ($this->getModel()->exists) {
$this->roles()->detach($this->roles->pluck('id')->toArray());
$this->roles()->detach($this->roles->filter(fn($role) => $role->name != UserRole::Admin->value)->pluck('id')->toArray());
$this->setRelation('roles', collect());
}

Expand Down
11 changes: 5 additions & 6 deletions app/Panel/Conference/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use STS\FilamentImpersonate\Tables\Actions\Impersonate;
Expand Down Expand Up @@ -66,7 +67,6 @@ public static function getNavigationGroup(): string
public static function getEloquentQuery(): Builder
{
return static::getModel()::query()
->where('id', '!=', auth()->id())
->with(['meta', 'media', 'bans']);
}

Expand Down Expand Up @@ -159,12 +159,14 @@ public static function form(Form $form): Form
titleAttribute: 'name',
modifyQueryUsing: fn ($query) => $query->where('name', '!=', UserRole::Admin)
)
->saveRelationshipsUsing(function (Forms\Components\CheckboxList $component, ?array $state) {
->saveRelationshipsUsing(function (Forms\Components\CheckboxList $component, ?array $state, User $record) {

$roles = $state ? Role::whereIn('id', $state)->pluck('name')->toArray() : [];

$roles = array_diff($roles, [UserRole::Admin->value]);

$component->getModelInstance()->syncRoles($roles);

}),
]),
])
Expand Down Expand Up @@ -259,10 +261,7 @@ public static function table(Table $table): Table
->deferFilters()
->actions([
EditAction::make()
->modalWidth('full')
->hidden(fn (User $record) => $record->hasRole(UserRole::Admin))
->mutateRecordDataUsing(fn ($data, User $record) => array_merge($data, ['meta' => $record->getAllMeta()->toArray()]))
->using(fn (array $data, User $record) => UserUpdateAction::run($data, $record)),
->modalWidth('full'),
DeleteAction::make()
->using(function (?array $data, User $record, DeleteAction $action) {
try {
Expand Down
4 changes: 4 additions & 0 deletions app/Policies/UserPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public function update(User $user, User $model)
return true;
}

if ($model->hasRole(UserRole::Admin)) {
return false;
}

if ($user->can('User:update')) {
return true;
}
Expand Down

0 comments on commit ec7d31a

Please sign in to comment.