Merge branch 'mkllnk-password-cpanel-webmail'

pull/5288/merge
Aleksander Machniak 8 years ago
commit f6e5085a73

@ -7,7 +7,6 @@
* *
* This driver has been tested successfully with Digital Pacific hosting. * This driver has been tested successfully with Digital Pacific hosting.
* *
* @version 4.0
* @author Maikel Linke <maikel@email.org.au> * @author Maikel Linke <maikel@email.org.au>
* *
* Copyright (C) 2005-2016, The Roundcube Dev Team * Copyright (C) 2005-2016, The Roundcube Dev Team
@ -28,6 +27,16 @@
class rcube_cpanel_webmail_password class rcube_cpanel_webmail_password
{ {
/**
* Changes the user's password. It is called by password.php.
* See "Driver API" README and password.php for the interface details.
*
* @param string $curpas current (old) password
* @param string $newpass new requested password
*
* @return mixed int code or assoc array with 'code' and 'message', see
* "Driver API" README and password.php
*/
public function save($curpas, $newpass) public function save($curpas, $newpass)
{ {
$user = $_SESSION['username']; $user = $_SESSION['username'];
@ -66,21 +75,26 @@ class rcube_cpanel_webmail_password
* *
* @param string $response JSON response by the Cpanel UAPI * @param string $response JSON response by the Cpanel UAPI
* *
* @return mixed response code or array * @return mixed response code or array, see <code>save</code>
*/ */
public static function decode_response($response) public static function decode_response($response)
{ {
if (!$response) {
return PASSWORD_CONNECT_ERROR;
}
// $result should be `null` or `stdClass` object
$result = json_decode($response); $result = json_decode($response);
if ($result['status'] === 1) { // The UAPI may return HTML instead of JSON on missing authentication
if ($result && $result->status === 1) {
return PASSWORD_SUCCESS; return PASSWORD_SUCCESS;
} }
$errors = $result['errors']; if ($result && is_array($result->errors) && count($result->errors) > 0) {
if (is_array($errors) && count($errors) > 0) {
return array( return array(
'code' => PASSWORD_ERROR, 'code' => PASSWORD_ERROR,
'message' => $errors[0], 'message' => $result->errors[0],
); );
} }
@ -104,8 +118,7 @@ class rcube_cpanel_webmail_password
* @param string $url the URL to post data to * @param string $url the URL to post data to
* @param array $postdata the data to post * @param array $postdata the data to post
* *
* @return string the body of the reply * @return string|false The body of the reply, False on error
* @throws Exception
*/ */
private function curl_auth_post($userpwd, $url, $postdata) private function curl_auth_post($userpwd, $url, $postdata)
{ {
@ -127,7 +140,7 @@ class rcube_cpanel_webmail_password
curl_close($ch); curl_close($ch);
if ($result === false) { if ($result === false) {
throw new Exception("curl error: $error"); rcube::raise_error("curl error: $error", true, false);
} }
return $result; return $result;

@ -19,5 +19,59 @@ class Password_Plugin extends PHPUnit_Framework_TestCase
$this->assertInstanceOf('password', $plugin); $this->assertInstanceOf('password', $plugin);
$this->assertInstanceOf('rcube_plugin', $plugin); $this->assertInstanceOf('rcube_plugin', $plugin);
} }
}
/**
* cpanel_webmail driver test
*/
function test_driver_cpanel_webmail()
{
$driver_class = $this->load_driver('cpanel_webmail');
$error_result = $driver_class::decode_response(false);
$this->assertEquals($error_result, PASSWORD_CONNECT_ERROR);
$bad_result = $driver_class::decode_response(null);
$this->assertEquals($bad_result, PASSWORD_CONNECT_ERROR);
$null_result = $driver_class::decode_response('null');
$this->assertEquals($null_result, PASSWORD_ERROR);
$malformed_result = $driver_class::decode_response('random {string]!');
$this->assertEquals($malformed_result, PASSWORD_ERROR);
$other_result = $driver_class::decode_response('{"a":"b"}');
$this->assertEquals($other_result, PASSWORD_ERROR);
$fail_response = '{"data":null,"errors":["Execution of Email::passwdp'
. 'op (api version:3) is not permitted inside of webmail"],"sta'
. 'tus":0,"metadata":{},"messages":null}';
$error_message = 'Execution of Email::passwdpop (api version:3) is no'
. 't permitted inside of webmail';
$expected_result = array(
'code' => PASSWORD_ERROR,
'message' => $error_message
);
$fail_result = $driver_class::decode_response($fail_response);
$this->assertEquals($expected_result, $fail_result);
$success_response = '{"metadata":{},"data":null,"messages":null,"errors'
. '":null,"status":1}';
$good_result = $driver_class::decode_response($success_response);
$this->assertEquals($good_result, PASSWORD_SUCCESS);
}
/**
* Loads a driver's source file, checks that its class exist and returns the
* driver's class name.
*
* @param string $driver driver name, example: "chpasswd"
* @return string driver's class name, example: "rcube_chpasswd_password"
*/
function load_driver($driver)
{
include_once __DIR__ . "/../drivers/$driver.php";
$driver_class = "rcube_${driver}_password";
$this->assertTrue(class_exists($driver_class));
return $driver_class;
}
}

Loading…
Cancel
Save