OctopyID / LaraPersonate

Login as a different user quickly for Laravel
MIT License
295 stars 27 forks source link

TypeError : Wrong argument on impersonated method. #23

Closed Ceepster14 closed 7 months ago

Ceepster14 commented 7 months ago

I'm trying to implement this package in a project using Laravel 11 (11.2.0) and Jetstream (5.0.2). I'm not using the UI as I'm initiating the impersonation through an existing CRUD. When trying to impersonate a user as follows:

$current_user->impersonate($impersonated_user);

Where $current_user is the currently logged in super user and $impersonated_user is the impersonation target, I get a Method Illuminate\Auth\RequestGuard::login does not exist exception thrown.

I believe I've followed the installation and configuration instructions correctly, but possibly I'm missing something?

Do you have any suggestions?

SupianIDz commented 7 months ago

Hi @Ceepster14 , sorry for the late reply.

Can you provide a minimum repository for reproduction?

Ceepster14 commented 7 months ago

Hi,

Apologies for the delay in replying.

I don't know whether there are some documentation issues here.

I decided to abandon using my own UI and use yours instead, which I kind of got working, however I did come across a few issues which might possibly point to a disconnect between the current package and the current documentation. If I follow your example for implementing the setImpersonateAuthorization method in my User model using a Boolean I have set on my user table to govern who can impersonate and who can be impersonated as follows:

public function setImpersonateAuthorization(Authorization $authorization) : void
    {
        $authorization->impersonator(function (User $user) {
            return $user->is_super_user;
        });

        $authorization->impersonated(function (User $user) {
            return !$user->is_super_user;
        });
    }

This throws the following error when it tries to evaluate the impersonated function:

App\Models\User::App\Models\{closure}(): Argument #1 ($user) must be of type App\Models\User, Octopy\Impersonate\Http\Resources\ImpersonateResource given

And the popup doesn't function and displays 'The results could not be loaded'

If I comment out the impersonated clause, but leave the impersonator clause, the pop-up works correctly and I can impersonate other users.

I also noticed in your example documentation covering this functionality that it references the Impersonate Concern:

use Octopy\Impersonate\Concerns\Impersonate;

But this concern does not exist in the package.

Hence my feeling that there's something out of sync between the docs and the package?

SupianIDz commented 7 months ago

hi @Ceepster14 sorry, there was an typo in the documentation, it should have been Octopy\Impersonate\Concerns\HasImpersonation;

Can you try again?

Ceepster14 commented 7 months ago

Hi,

Yes, I'm using that concern, but this still exhibits the error detailed previously with 'impersonated' call.

SupianIDz commented 7 months ago

@Ceepster14 okay, I have found the problem, while you can use this code to solve the problem while I fixed it.

$authorization->impersonated(function ($user){
    return !$user->is_admin;
});
Screenshot 2024-04-19 at 00 32 31
Ceepster14 commented 7 months ago

Thanks, I really appreciate your time on this! That works for me.

SupianIDz commented 7 months ago

Thanks, I really appreciate your time on this! That works for me.

If you want to use the User argument You can upgrade it to version 4.0.3, it has been fixed.

SupianIDz commented 7 months ago

thank you for the report @Ceepster14