$kirby->impersonate()
Become any existing user or disable the current user
$kirby->impersonate(string|null $who = null, Closure|null $callback = null): mixed
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
| $who | stringornull |
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 | Closureornull |
null
|
Optional action function that will be run with the permissions of the impersonated user; the impersonation will be reset afterwards |
Return type
mixed
If called without callback: User that was impersonated; if called with callback: Return value from the callback
This method modifies the existing $app object it is applied to and returns it again.
Exceptions
| Type | Description |
|---|---|
Throwable |
Parent class
$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)null(resets the impersonation)
Examples
<?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.