diff --git a/main/inc/lib/usermanager.lib.php b/main/inc/lib/usermanager.lib.php index c94e5a3b25..57a6d35362 100755 --- a/main/inc/lib/usermanager.lib.php +++ b/main/inc/lib/usermanager.lib.php @@ -61,15 +61,16 @@ class UserManager * @param string Authentication source (optional, defaults to 'platform', dependind on constant) * @param string Account expiration date (optional, defaults to '0000-00-00 00:00:00') * @param int Whether the account is enabled or disabled by default - * @param int The user ID of the person who registered this user (optional, defaults to null) * @param int The department of HR in which the user is registered (optional, defaults to 0) + * @param array Extra fields + * @param string Encrypt method used if password is given encrypted. Set to an empty string by default * @return mixed new user id - if the new user creation succeeds, false otherwise * * @desc The function tries to retrieve $_user['user_id'] from the global space. * if it exists, $_user['user_id'] is the creator id. If a problem arises, * it stores the error message in global $api_failureList */ - public static function create_user($firstName, $lastName, $status, $email, $loginName, $password, $official_code = '', $language = '', $phone = '', $picture_uri = '', $auth_source = PLATFORM_AUTH_SOURCE, $expiration_date = '0000-00-00 00:00:00', $active = 1, $hr_dept_id = 0, $extra = null) { + public static function create_user($firstName, $lastName, $status, $email, $loginName, $password, $official_code = '', $language = '', $phone = '', $picture_uri = '', $auth_source = PLATFORM_AUTH_SOURCE, $expiration_date = '0000-00-00 00:00:00', $active = 1, $hr_dept_id = 0, $extra = null, $encrypt_method = '') { global $_user, $userPasswordCrypted; $firstName = Security::remove_XSS($firstName); @@ -94,7 +95,19 @@ class UserManager return api_set_failure('login-pass already taken'); } //$password = "PLACEHOLDER"; - $password = api_get_encrypted_password($password); + if($encrypt_method == '') { + $password = api_get_encrypted_password($password); + } else { + if ($userPasswordCrypted === $encrypt_method ) { + if ($encrypt_method == 'md5' && !preg_match('/^[A-Fa-f0-9]{32}$/', $password)) { + return api_set_failure('encrypt_method invalid'); + } else if ($encrypt_method == 'sha1' && !preg_match('/^[A-Fa-f0-9]{40}$/', $password)) { + return api_set_failure('encrypt_method invalid'); + } + } else { + return api_set_failure('encrypt_method invalid'); + } + } //$password = ($userPasswordCrypted ? md5($password) : $password); $current_date = date('Y-m-d H:i:s', time()); $sql = "INSERT INTO $table_user @@ -144,7 +157,7 @@ class UserManager if (is_array($extra) && count($extra) > 0) { $res = true; foreach($extra as $fname => $fvalue) { - $res = $res && self::update_extra_field($return, $fname, $fvalue); + $res = $res && self::update_extra_field_value($return, $fname, $fvalue); } } return $return; @@ -500,6 +513,25 @@ class UserManager // 2. Length limitation. return substr(preg_replace(USERNAME_PURIFIER_SHALLOW, '', $username), 0, USERNAME_MAX_LENGTH); } + + /** + * Checks whether the user id exists in the database + * + * @param int User id + * @return bool True if user id was found, false otherwise + */ + public static function is_user_id_valid($user_id) { + $user_id = (int)$user_id; + $table_user = Database :: get_main_table(TABLE_MAIN_USER); + $sql = "SELECT user_id FROM $table_user WHERE user_id = '".$user_id."'"; + $res = Database::query($sql); + $num_rows = Database::num_rows($res); + if($num_rows == 0) { + return false; + } else { + return true; + } + } /** * Checks whether a given username matches to the specification strictly. The empty username is assumed here as invalid. diff --git a/tests/test_webservices.php b/tests/test_webservices.php index c78ca16eb8..b5cb2f918d 100644 --- a/tests/test_webservices.php +++ b/tests/test_webservices.php @@ -1,19 +1,92 @@ */ ini_set('soap.wsdl_cache_enabled', 0); require_once(dirname(__FILE__).'/../main/inc/global.inc.php'); -$security_key = $_configuration['security_key']; -$ip_address = '::1'; -$secret_key = sha1($ip_address.$security_key); +require_once(dirname(__FILE__).'/simpletest/autorun.php'); -$client = new SoapClient($_configuration['root_web'].'main/webservices/registration.soap.php?wsdl'); +class TestSoapWebService extends UnitTestCase { + protected $_secret_key; + + protected $_encrypt_method; + + protected $_client; + + public function __construct() { + $configuration = $GLOBALS['_configuration']; + $security_key = $configuration['security_key']; + $ip_address = '::1'; + $this->_secret_key = sha1($ip_address.$security_key); + $this->_encrypt_method = $_GLOBALS['userPasswordCrypted']; + $this->_client = new SoapClient($configuration['root_web'].'main/webservices/soap.php?wsdl'); + } + + protected function getUserArray() { + $user = array( + 'firstname' => 'Guillaume', + 'lastname' => 'Viguier', + 'status' => 5, + 'loginname' => 'guillaumev', + 'password' => 'guillaume', + 'encrypt_method' => '', + 'user_id_field_name' => 'chamilo_user_id', + 'user_id_field_value' => '', + 'visibility' => 1, + 'email' => 'guillaume.viguier@beeznest.com', + 'language' => 'english', + 'phone' => '123456', + 'expiration_date' => '0000-00-00 00:00:00', + 'extras' => array()); + return $user; + } + + protected function soapCall($method, $arguments) { + return $this->_client->__soapCall($method, $arguments); + } + + public function testTest() { + $result = $this->soapCall('WS.test', array()); + $this->assertEqual($result, "success"); + } + + public function testInvalidKey() { + $secret_key = 'invalid'; + try { + $this->soapCall('WS.DisableUser', array('secret_key' => $secret_key, 'user_id_field_name' => 'chamilo_user_id', 'user_id_value' => 3)); + $this->fail('Exception was expected'); + } catch(SOAPFault $f) { + $this->pass(); + } + } + + public function testCreateUser() { + $user = $this->getUserArray(); + $result = $this->soapCall('WS.CreateUser', array_merge(array('secret_key' => $this->_secret_key), $user)); + $this->assertIsA($result, 'int'); + //Delete user created + $this->soapCall('WS.DeleteUser', array('secret_key' => $this->_secret_key, 'user_id_field_name' => 'chamilo_user_id', 'user_id_value' => $result)); + } + + public function testCreateUserEncrypted() { + $user = $this->getUserArray(); + $user['encrypt_method'] = $this->_encrypt_method; + if($this->_encrypt_method == 'md5') { + $user['password'] = md5('guillaume'); + } else if($this->_encrypt_method == 'sha1') { + $user['password'] = sha1('guillaume'); + } + $user['extras'] = array(array('field_name' => 'salt', 'field_value' => '1234')); + $result = $this->soapCall('WS.CreateUser', array_merge(array('secret_key' => $this->_secret_key), $user)); + $this->assertIsA($result, 'int'); + //Delete user created + $this->soapCall('WS.DeleteUser', array('secret_key' => $this->_secret_key, 'user_id_field_name' => 'chamilo_user_id', 'user_id_value' => $result)); + } +} -$params = array('secret_key' => $secret_key, 'ids' => array(3)); -$client->WSEnableUsers($params);