Reference

Guide

$kirby->impersonate()

Become any existing user

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

Parameters

Name Type Default Description
$who string null

User ID or email address

$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

$who

There are three options for the $who parameter:

  • kirby (impersonates the allmighty with full permissions)
  • a username (impersonates the specific user with its permissions)
  • null (resets the impersonation)

Example

<?php

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

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

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.

Source code

kirby/src/Cms/AppUsers.php#L47