public function ApiClient::sudo in Rocket.Chat 8
Same name and namespace in other branches
- 8.2 modules/rocket_chat_api/src/RocketChat/ApiClient.php \Drupal\rocket_chat_api\RocketChat\ApiClient::sudo()
Execute as different user.
Parameters
string $otherUserId: UserID of user to sudo as.
string $functionName: Function Name to call.
array $args: Function Arguments.
Return value
mixed Result of Call.
Throws
\Exception When there is an exception in the call, it is rethrown after cleanup.
File
- modules/
rocket_chat_api/ src/ RocketChat/ ApiClient.php, line 348
Class
- ApiClient
- Class ApiClient.
Namespace
Drupal\rocket_chat_api\RocketChatCode
public function sudo($otherUserId, $functionName, ...$args) {
$empty = "";
if ($functionName == 'login' || $functionName == 'logout') {
throw new \BadFunctionCallException("{$functionName} must be used directly not through sudo.", 502);
}
$returnValue = NULL;
$originalConfig = $this->config;
$newConfig = new InMemoryConfig($this->config, $empty, $empty);
try {
$authToken = $this
->postToRocketChat('users.createToken', [
'json' => [
'userId' => $otherUserId,
],
]);
$newConfig
->setElement('rocket_chat_uid', $authToken['body']['data']['userId']);
$newConfig
->setElement('rocket_chat_uit', $authToken['body']['data']['authToken']);
$this->config = $newConfig;
$this->client = $this
->createClient(FALSE);
// Do Function call!
$returnValue = $this
->{$functionName}(...$args);
} catch (\Exception $e) {
throw $e;
} finally {
$this->config = $originalConfig;
$this->client = $this
->createClient(FALSE);
}
return $returnValue;
}