Skip to content

Kirby 3.8.4

$kirby->impersonate()

Become any existing user or disable the current user

$kirby->impersonate(string $who = null, Closure $callback = null): mixed

Parameters

Name Type Default Description
$who string null User ID or email address,
null to use the actual user again,
'kirby' for a virtual admin user or
'nobody' to disable the actual user
$callback Closure null Optional action function that will be run with
the permissions of the impersonated user; the
impersonation will be reset afterwards

Return type

mixed

Exceptions

Type Description
Throwable

Parent class

Kirby\Cms\App

$who

There are four options for the $who parameter:

  • a username (impersonates the specific user with their permissions)
  • 'kirby' (impersonates the allmighty user with full permissions)
  • 'nobody' (disables the currently logged in user, added in Kirby 3.5.0)
  • null (resets the impersonation)

Example

<?php

$kirby = kirby();
$kirby->impersonate('kirby');

page('notes/a-great-article')->update([
  'author' => 'Homer Simpson'
]);

The impersonation will be active for all code that runs in the current request after the call to $kirby->impersonate(). If you want to limit the impact to a single operation, call the method with a callback (see below).

With callback

If you only want a single operation to run with different privileges, use a callback:

<?php

$kirby = kirby();
$result = $kirby->impersonate('kirby', function () {
  page('notes/a-great-article')->update([
    'author' => 'Homer Simpson'
  ]);

  return 'this will be returned to $result above';
});

The impersonation automatically gets reset to the previous value after the callback returns.