Skip to content

[6.x] Allow control over who can be impersonated in UserPolicy#14469

Merged
jasonvarga merged 3 commits intostatamic:6.xfrom
ryanmitchell:feature/impersonate-check-policy
Apr 9, 2026
Merged

[6.x] Allow control over who can be impersonated in UserPolicy#14469
jasonvarga merged 3 commits intostatamic:6.xfrom
ryanmitchell:feature/impersonate-check-policy

Conversation

@ryanmitchell
Copy link
Copy Markdown
Contributor

@ryanmitchell ryanmitchell commented Apr 9, 2026

This PR adds an impersonate($authed, $user) method to the Statamic UserPolicy which is checked by the Impersonate action. This allows a developer to add custom logic in their own UserPolicy to determine if the authed user can impersonate the user in question... e.g. if you want admins not to be able to impersonate super admins.

Usage example:

// app/Policies/UserPolicy.php
namespace App\Policies;

use Statamic\Policies\UserPolicy as StatamicUserPolicy;

class UserPolicy extends StatamicUserPolicy
{
    public function impersonate($authed, $user)
    {
        // don't allow impersonating super users
        return ! $user->super;
    }
}

// app/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\Gate;
use Statamic\Auth\File\User;

public function boot(): void
{
    Gate::policy(User::class, \App\Policies\UserPolicy::class);
}

ryanmitchell and others added 2 commits April 9, 2026 19:06
@jasonvarga jasonvarga merged commit df5474d into statamic:6.x Apr 9, 2026
17 checks passed
@ryanmitchell ryanmitchell deleted the feature/impersonate-check-policy branch April 10, 2026 06:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants