Skip to content

Commit 7353944

Browse files
committed
Merge pull request #101 from zendesk/jose/update-test-connection-button
"Test Connection" button improvement
2 parents 06cc42e + 95f2798 commit 7353944

File tree

4 files changed

+93
-7
lines changed

4 files changed

+93
-7
lines changed

src/app/code/community/Zendesk/Zendesk/Helper/Data.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,24 @@ public function isValidDate($date) {
360360
public function getFormatedDateTime($dateToFormat) {
361361
return Mage::helper('core')->formatDate($dateToFormat, 'medium', true);
362362
}
363-
364-
public function getConnectionStatus() {
363+
364+
/**
365+
* Tests if the provided username and password is correct. If either is empty the database values will be used.
366+
*
367+
* @param string $domain
368+
* @param string $username
369+
* @param string $password
370+
* @return array
371+
*/
372+
public function getConnectionStatus($domain = null, $username = null, $password = null) {
365373
try {
366-
$user = Mage::getModel('zendesk/api_users')->me();
374+
$usersApi = Mage::getModel('zendesk/api_users');
375+
376+
$usersApi->setUsername($username);
377+
$usersApi->setPassword($password);
378+
$usersApi->setDomain($domain);
379+
380+
$user = $usersApi->me();
367381

368382
if(isset($user['id'])) {
369383
return array(

src/app/code/community/Zendesk/Zendesk/Model/Api/Abstract.php

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,70 @@
1717

1818
class Zendesk_Zendesk_Model_Api_Abstract extends Mage_Core_Model_Abstract
1919
{
20+
protected $username = null;
21+
protected $password = null;
22+
protected $domain = null;
23+
24+
/**
25+
* Sets the domain to be used for this instance
26+
*
27+
* @param string $username The user domain
28+
*/
29+
public function setDomain($domain)
30+
{
31+
$this->domain = $domain;
32+
}
33+
34+
/**
35+
* Sets the email to be used for this instance
36+
*
37+
* @param string $username The user email
38+
*/
39+
public function setUsername($username)
40+
{
41+
$this->username = $username;
42+
}
43+
44+
/**
45+
* Sets the API token for this instance
46+
*
47+
* @param string $password The API token
48+
*/
49+
public function setPassword($password)
50+
{
51+
$this->password = $password;
52+
}
53+
54+
public function getUsername()
55+
{
56+
if ($this->username === null) {
57+
$this->username = Mage::getStoreConfig('zendesk/general/email');
58+
}
59+
60+
return $this->username . '/token';
61+
}
62+
63+
public function getPassword()
64+
{
65+
if ($this->password === null) {
66+
$this->password = Mage::getStoreConfig('zendesk/general/password');
67+
}
68+
69+
return $this->password;
70+
}
71+
72+
public function getDomain()
73+
{
74+
if ($this->domain === null) {
75+
$this->domain = Mage::getStoreConfig('zendesk/general/domain');
76+
}
77+
78+
return $this->domain;
79+
}
80+
2081
protected function _getUrl($path)
2182
{
22-
$base_url = 'https://' . Mage::getStoreConfig('zendesk/general/domain') . '/api/v2';
83+
$base_url = 'https://' . $this->getDomain() . '/api/v2';
2384
$path = trim($path, '/');
2485
return $base_url . '/' . $path;
2586
}
@@ -48,8 +109,8 @@ protected function _call($endpoint, $params = null, $method = 'GET', $data = nul
48109
);
49110

50111
$client->setAuth(
51-
Mage::getStoreConfig('zendesk/general/email'). '/token',
52-
Mage::getStoreConfig('zendesk/general/password')
112+
$this->getUsername(),
113+
$this->getPassword()
53114
);
54115

55116
if($method == 'POST' || $method == "PUT") {

src/app/code/community/Zendesk/Zendesk/controllers/Adminhtml/ZendeskController.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,13 @@ public function clearLogAction()
463463

464464
public function checkOutboundAction()
465465
{
466-
$connection = Mage::helper('zendesk')->getConnectionStatus();
466+
$request = Mage::app()->getRequest();
467+
468+
$connection = Mage::helper('zendesk')->getConnectionStatus(
469+
$request->getParam('domain'),
470+
$request->getParam('username'),
471+
$request->getParam('password')
472+
);
467473

468474
$this->getResponse()->clearHeaders()->setHeader('Content-type','application/json', true);
469475
$this->getResponse()->setBody(json_encode($connection));

src/app/design/adminhtml/default/default/template/zendesk/config/button-test-zendesk.phtml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
function checkZendApiConnection() {
2323
var request = new Ajax.Request('<?php echo $this->getUrl('adminhtml/zendesk/checkOutbound'); ?>', {
2424
method: 'get',
25+
parameters: {
26+
domain: document.getElementById('zendesk_general_domain').value,
27+
username: document.getElementById('zendesk_general_email').value,
28+
password: document.getElementById('zendesk_general_password').value
29+
},
2530
onCreate: function() {
2631
document.getElementById('zendesk-api-connection-results').innerHTML = '';
2732
},

0 commit comments

Comments
 (0)