Skip to content

Commit

Permalink
Allow to set user, password and auth token simultaneously
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys committed May 31, 2021
1 parent 9e7940d commit cd815ba
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/ZabbixApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ public function __construct($apiUrl = null, $user = null, $password = null, $htt

if (null !== $authToken) {
$this->setAuthToken($authToken);
} elseif (null !== $user && null !== $password) {
}

if (null !== $user && null !== $password) {
$this->user = $user;
$this->password = $password;
}
Expand Down Expand Up @@ -8570,7 +8572,8 @@ private function request($method, $params = null, $resultArrayKey = null, $assoc
$response = $this->decodeResponse($this->response, $resultArrayKey, $assoc);
} catch (Exception $e) {
// If the request is not authorized due an authentication issue, attempt to login again and retry the operation.
if ($auth && self::UNAUTHORIZED_ERROR_CODE === $e->getCode() && self::UNAUTHORIZED_ERROR_MESSAGE === $e->getMessage() &&
if ($auth && self::UNAUTHORIZED_ERROR_CODE === $e->getCode() &&
in_array($e->getMessage(), [self::UNAUTHORIZED_ERROR_MESSAGE, self::UNAUTHORIZED_SESSION_TERMINATED_ERROR_MESSAGE], true) &&
$remainingAuthAttempts > 0 && null !== $this->user && null !== $this->password
) {
$this->getAuthToken(false);
Expand Down Expand Up @@ -8699,7 +8702,7 @@ private function getAuthToken($fromCache = true)
}

// No cached token found so far, so login.
if (null === $this->authToken) {
if (!$fromCache || null === $this->authToken) {
// login to get the auth token
$params = $this->getRequestParamsArray(['user' => $this->user, 'password' => $this->password]);
$this->authToken = $this->userLogin($params);
Expand Down
2 changes: 2 additions & 0 deletions src/ZabbixApiInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ interface ZabbixApiInterface

const UNAUTHORIZED_ERROR_MESSAGE = 'Not authorised.';

const UNAUTHORIZED_SESSION_TERMINATED_ERROR_MESSAGE = 'Session terminated, re-login, please.';

const ACCESS_DENY_OBJECT = 0;

const ACCESS_DENY_PAGE = 1;
Expand Down

0 comments on commit cd815ba

Please sign in to comment.