Skip to content

docs: warn that setIdentity()/clearIdentity() end impersonation#789

Draft
dereuromark wants to merge 1 commit into4.xfrom
docs-impersonation-set-identity
Draft

docs: warn that setIdentity()/clearIdentity() end impersonation#789
dereuromark wants to merge 1 commit into4.xfrom
docs-impersonation-set-identity

Conversation

@dereuromark
Copy link
Copy Markdown
Member

@dereuromark dereuromark commented Apr 28, 2026

Summary

  • Adds a 3rd "Limitations" bullet to docs/en/impersonation.md explaining that setIdentity() and clearIdentity() (and therefore logout()) silently end impersonation, with a recommended workaround for the common "refresh active user" use case.
  • Adds a "Replacing the current identity" section to docs/en/authentication-component.md that documents setIdentity() (it wasn't documented in this file before) and links the impersonation warning.

Why

The interaction is real and surprising:

AuthenticationComponent::setIdentity() calls the service's clearIdentity() first, and AuthenticationService::clearIdentity() actively calls stopImpersonating() on any ImpersonationInterface authenticator that returns true for isImpersonating() (AuthenticationService.php#L194).

So an app pattern like

// AppController::beforeFilter()
if ($user && !$user->some_association) {
    $reloaded = $this->Users->get($user->id, finder: 'fullProfile');
    $this->Authentication->setIdentity($reloaded);
}

silently ends impersonation on every request where the association was missing. The recommended workaround (write to the request attribute directly) is small but isn't currently documented.

Notes

Calling setIdentity() (and clearIdentity() / logout()) silently ends an
active impersonation session because the service's clearIdentity()
actively calls stopImpersonating() on impersonation-aware authenticators.
This is surprising when applications use setIdentity() in beforeFilter()
to eager-load associations on the active user.

Add a third "Limitations" bullet to the impersonation docs with the
recommended workaround (write to the request attribute directly), and
add a "Replacing the current identity" section to the component docs
that documents setIdentity() and links to the impersonation note.
@dereuromark
Copy link
Copy Markdown
Member Author

With #788 accepted, it would superseed this PR, so draft for now again.

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.

1 participant