stechstudio / filament-impersonate

Filament plugin that makes it easy to impersonate your users
271 stars 55 forks source link

Trouble on impersonate from resources other than UserResource #74

Closed z4yed closed 3 months ago

z4yed commented 8 months ago

I have a resource named InstructorResource. The Instructor model has a relational user method which belongs to User model.

I am trying to impersonate from InstructorResource by:

return $table
       ->actions([
                Impersonate::make()
                    ->redirectTo('/instructor'),

                Tables\Actions\ViewAction::make(),
                Tables\Actions\EditAction::make(),
            ]),

From the UserResource class, it is working perfectly though. Is it possible to impersonate from InstructResource?

jszobody commented 8 months ago

If you look at the table action...

https://github.com/stechstudio/filament-impersonate/blob/master/src/Tables/Actions/Impersonate.php#L20C13-L20C66

... you can see how the current $record is being passed to the impersonate method. In theory, if the current $record is an instructor, you could probably do this:

Impersonate::make()
    ->redirectTo('/instructor')
    ->action(fn ($record) => $this->impersonate($record->user))

This is untested, but it seems like this sort of approach should work. I think you just need to manually handle the action, and tell the impersonate method where the actual user model is.

z4yed commented 8 months ago
->actions([
  Impersonate::make()
      ->redirectTo('/instructor')
      ->action(fn($record) => $this->impersonate($record->user)),
  Tables\Actions\ViewAction::make(),
  Tables\Actions\EditAction::make(),
])

This code return the below error.

image

KonstantinosVazaios commented 5 months ago

I am having the same issue. Tried to override the record method but did not work as well

AurelDemiri commented 5 months ago

@z4yed This should work:

            ->actions([
                Impersonate::make()
                    ->action(fn(Interpreter $record, Impersonate $action) => $action->impersonate($record->user)),
                Tables\Actions\EditAction::make(),
            ])
z4yed commented 3 months ago

@AurelDemiri awesome! Thanks.