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

Would be nice to add an @isImposter blade directive #180

Open
BigBlockStudios opened this issue Apr 20, 2023 · 5 comments
Open

Would be nice to add an @isImposter blade directive #180

BigBlockStudios opened this issue Apr 20, 2023 · 5 comments

Comments

@BigBlockStudios
Copy link

Would be nice to add an @isImposter() blade directive - checking a session variable for someone impersonating ATM.

@drbyte
Copy link
Contributor

drbyte commented Apr 20, 2023

To clarify: Are you asking for a way to check whether another user is logged-in via impersonation?

This package currently only exposes that the current user is logged-in via impersonation. It does that by checking the current user's session for the impersonation keys being present.

But there's nothing in this package that allows reaching into another user's session to track whether that person is being impersonated. Something would have to be stored in the database to know that state, and of course to reset it when the person logs out (and also to figure out when that user's session expires even if they didn't click logout).
You could do that yourself by listening for the TakeImpersonation and LeaveImpersonation events in your own app, and store/reset something about those users in your own app's database to track it.

@BigBlockStudios
Copy link
Author

Sorry, no I meant just to check if the current user is currently an imposter - as you said, you can find out by checking a session variable. so super tiny problem. maybe it just feels more consistent to me.,

@drbyte
Copy link
Contributor

drbyte commented Apr 21, 2023

Sorry, no I meant just to check if the current user is currently an imposter - as you said, you can find out by checking a session variable. so super tiny problem. maybe it just feels more consistent to me.,

You can use @impersonating for that: https://github.com/404labfr/laravel-impersonate#when-the-user-is-impersonated

@simplyphp-moe
Copy link

simplyphp-moe commented May 4, 2023

I think @BigBlockStudios meant when we have a @canImpersonate and @impersonating at the same time, with what we have now, both the Impersonate Person and Leave Impersonation buttons are visible cause they both return true.

As a solution, I've did something like this

@impersonating($guard = null)
    <a class="dropdown-item" href="{{ route('impersonate.leave') }}">
          <i class="dropdown-icon fe fe-user"></i> Leave Impersonation
    </a>
@else
    @canImpersonate($guard = null)
          <a class="dropdown-item" href="{{ route('impersonate', $user->id) }}">
             <i class="dropdown-icon fe fe-user"></i> Impersonate Partner User
          </a>
    @endCanImpersonate 
@endImpersonating

@drbyte
Copy link
Contributor

drbyte commented May 6, 2023

I've done it similarly:

//User.php model
    /**
     * Return true or false whether the user can be impersonated.
     * Here we deny impersonation of oneself as that would be pointless.
     */
    public function canBeImpersonated(): bool
    {
        return $this->id != Auth::id();
    }
                        @canImpersonate
                          @canBeImpersonated($user)
                        <div class="btn-group float-right d-print-none" role="group" aria-label="Impersonate">
                            <a href="{{ route('impersonate', $user->id) }}"><button type="button" class="btn btn-success text-center"><i class="fa fa-user-circle fa-lg" title="Impersonate this user"></i></button></a>&nbsp;
                        </div>
                          @endCanBeImpersonated
                        @endCanImpersonate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants