Organized
This commit is contained in:
@@ -22,6 +22,31 @@ final class RedmineClient
|
||||
$this->client = $client;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a callback while impersonating an existing Redmine username.
|
||||
*
|
||||
* Redmine impersonation only applies when the authenticated API user has
|
||||
* permission to switch users.
|
||||
*
|
||||
* @param callable $callback
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function withImpersonation(?string $username, callable $callback)
|
||||
{
|
||||
$normalized = is_string($username) ? trim($username) : '';
|
||||
if ($normalized === '') {
|
||||
return $callback();
|
||||
}
|
||||
|
||||
$this->client->startImpersonateUser($normalized);
|
||||
try {
|
||||
return $callback();
|
||||
} finally {
|
||||
$this->client->stopImpersonateUser();
|
||||
}
|
||||
}
|
||||
|
||||
public static function fromCredentials(string $url, string $apiKeyOrUsername, ?string $password = null): self
|
||||
{
|
||||
return new self(new NativeCurlClient($url, $apiKeyOrUsername, $password));
|
||||
|
||||
Reference in New Issue
Block a user