Skip to content

Commit

Permalink
Do not call userLogin() at __construct()
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys committed Jan 22, 2020
1 parent a5944bb commit ed3c8f5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
17 changes: 16 additions & 1 deletion build/templates/abstract.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ abstract class <CLASSNAME_ABSTRACT>
*/
private $defaultParams = array();

/**
* @var string
*/
private $user;

/**
* @var string
*/
private $password;

/**
* Auth string.
*
Expand Down Expand Up @@ -144,7 +154,8 @@ public function __construct($apiUrl = '', $user = '', $password = '', $httpUser
if ($authToken) {
$this->setAuthToken($authToken);
} elseif ($user && $password) {
$this->userLogin(array('user' => $user, 'password' => $password));
$this->user = $user;
$this->password = $password;
}
}

Expand Down Expand Up @@ -277,6 +288,10 @@ public function printCommunication($print = true)
*/
public function request($method, $params = null, $resultArrayKey = '', $auth = true)
{
if (!$this->authToken && $auth && $this->user && $this->password) {
$this->userLogin(array('user' => $this->user, 'password' => $this->password));
}

// sanity check and conversion for params array
if (!$params) {
$params = array();
Expand Down
8 changes: 4 additions & 4 deletions tests/ZabbixApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ public function testZabbixApiClass()

public function testZabbixApiConnectionNotTriggered()
{
$zabbix = new ZabbixApi('http://localhost/json_rpc.php');
$zabbix = new ZabbixApi('http://localhost/json_rpc.php', 'zabbix');
$zabbix = new ZabbixApi('http://localhost/json_rpc.php', '', 'very_secret');
$zabbix = new ZabbixApi('http://localhost/json_rpc.php', 'zabbix', 'very_secret');

$this->assertSame('http://localhost/json_rpc.php', $zabbix->getApiUrl());
}
Expand All @@ -70,6 +68,8 @@ public function testZabbixApiConnectionNotTriggered()
*/
public function testZabbixApiConnectionError()
{
new ZabbixApi('http://not.found.tld/json_rpc.php', 'zabbix', 'very_secret_pass');
$zabbix = new ZabbixApi('http://not.found.tld/json_rpc.php', 'zabbix', 'very_secret_pass');

$zabbix->userGet();
}
}

0 comments on commit ed3c8f5

Please sign in to comment.