webservice_user.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.webservices
  5. */
  6. require_once(dirname(__FILE__).'/../inc/global.inc.php');
  7. require_once(dirname(__FILE__).'/webservice.php');
  8. /**
  9. * Web services available for the User module. This class extends the WS class
  10. */
  11. class WSUser extends WS {
  12. /**
  13. * Enables or disables a user
  14. *
  15. * @param string User id field name
  16. * @param string User id value
  17. * @param int Set to 1 to enable and to 0 to disable
  18. */
  19. protected function changeUserActiveState($user_id_field_name, $user_id_value, $state) {
  20. $user_id = $this->getUserId($user_id_field_name, $user_id_value);
  21. if($user_id instanceof WSError) {
  22. return $user_id;
  23. } else {
  24. if($state == 0) {
  25. UserManager::disable($user_id);
  26. } else if($state == 1) {
  27. UserManager::enable($user_id);
  28. }
  29. }
  30. }
  31. /**
  32. * Enables or disables multiple users
  33. *
  34. * @param array Users
  35. * @param int Set to 1 to enable and to 0 to disable
  36. * @return array Array of results
  37. */
  38. protected function changeUsersActiveState($users, $state) {
  39. $results = array();
  40. foreach($users as $user) {
  41. $result_tmp = array();
  42. $result_op = $this->changeUserActiveState($user['user_id_field_name'], $user['user_id_value'], $state);
  43. $result_tmp['user_id_value'] = $user['user_id_value'];
  44. if($result_op instanceof WSError) {
  45. // Return the error in the results
  46. $result_tmp['result'] = $result_op->toArray();
  47. } else {
  48. $result_tmp['result'] = $this->getSuccessfulResult();
  49. }
  50. $results[] = $result_tmp;
  51. }
  52. return $results;
  53. }
  54. /**
  55. * Disables a user
  56. *
  57. * @param string API secret key
  58. * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
  59. * @param string User id value
  60. */
  61. public function DisableUser($secret_key, $user_id_field_name, $user_id_value) {
  62. $verifKey = $this->verifyKey($secret_key);
  63. if($verifKey instanceof WSError) {
  64. // Let the implementation handle it
  65. $this->handleError($verifKey);
  66. } else {
  67. $result = $this->changeUserActiveState($user_id_field_name, $user_id_value, 0);
  68. if($result instanceof WSError) {
  69. $this->handleError($result);
  70. }
  71. }
  72. }
  73. /**
  74. * Disables multiple users
  75. *
  76. * @param string API secret key
  77. * @param array Array of users with elements of the form array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value')
  78. * @return array Array with elements like array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). Note that if the result array contains a code different
  79. * than 0, an error occured
  80. */
  81. public function DisableUsers($secret_key, $users) {
  82. $verifKey = $this->verifyKey($secret_key);
  83. if($verifKey instanceof WSError) {
  84. // Let the implementation handle it
  85. $this->handleError($verifKey);
  86. } else {
  87. return $this->changeUsersActiveState($users, 0);
  88. }
  89. }
  90. /**
  91. * Enables a user
  92. *
  93. * @param string API secret key
  94. * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
  95. * @param string User id value
  96. */
  97. public function EnableUser($secret_key, $user_id_field_name, $user_id_value) {
  98. $verifKey = $this->verifyKey($secret_key);
  99. if($verifKey instanceof WSError) {
  100. $this->handleError($verifKey);
  101. } else {
  102. $result = $this->changeUserActiveState($user_id_field_name, $user_id_value, 1);
  103. if($result instanceof WSError) {
  104. $this->handleError($result);
  105. }
  106. }
  107. }
  108. /**
  109. * Enables multiple users
  110. *
  111. * @param string API secret key
  112. * @param array Array of users with elements of the form array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value')
  113. * @return array Array with elements like array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). Note that if the result array contains a code different
  114. * than 0, an error occured
  115. */
  116. public function EnableUsers($secret_key, $users) {
  117. $verifKey = $this->verifyKey($secret_key);
  118. if($verifKey instanceof WSError) {
  119. // Let the implementation handle it
  120. $this->handleError($verifKey);
  121. } else {
  122. return $this->changeUsersActiveState($users, 1);
  123. }
  124. }
  125. /**
  126. * Deletes a user (helper method)
  127. *
  128. * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
  129. * @param string User id value
  130. * @return mixed True if user was successfully deleted, WSError otherwise
  131. */
  132. protected function deleteUserHelper($user_id_field_name, $user_id_value) {
  133. $user_id = $this->getUserId($user_id_field_name, $user_id_value);
  134. if($user_id instanceof WSError) {
  135. return $user_id;
  136. } else {
  137. if(!UserManager::delete_user($user_id)) {
  138. return new WSError(101, "There was a problem while deleting this user");
  139. } else {
  140. return true;
  141. }
  142. }
  143. }
  144. /**
  145. * Deletes a user
  146. *
  147. * @param string API secret key
  148. * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
  149. * @param string User id value
  150. */
  151. public function DeleteUser($secret_key, $user_id_field_name, $user_id_value) {
  152. $verifKey = $this->verifyKey($secret_key);
  153. if($verifKey instanceof WSError) {
  154. $this->handleError($verifKey);
  155. } else {
  156. $result = $this->deleteUserHelper($user_id_field_name, $user_id_value);
  157. if($result instanceof WSError) {
  158. $this->handleError($result);
  159. }
  160. }
  161. }
  162. /**
  163. * Deletes multiple users
  164. *
  165. * @param string API secret key
  166. * @param array Array of users with elements of the form array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value')
  167. * @return array Array with elements like array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). Note that if the result array contains a code different
  168. * than 0, an error occured
  169. */
  170. public function DeleteUsers($secret_key, $users) {
  171. $verifKey = $this->verifyKey($secret_key);
  172. if($verifKey instanceof WSError) {
  173. $this->handleError($verifKey);
  174. } else {
  175. $results = array();
  176. foreach($users as $user) {
  177. $result_tmp = array();
  178. $result_op = $this->deleteUserHelper($user['user_id_field_name'], $user['user_id_value']);
  179. $result_tmp['user_id_value'] = $user['user_id_value'];
  180. if($result_op instanceof WSError) {
  181. // Return the error in the results
  182. $result_tmp['result'] = $result_op->toArray();
  183. } else {
  184. $result_tmp['result'] = $this->getSuccessfulResult();
  185. }
  186. $results[] = $result_tmp;
  187. }
  188. return $results;
  189. }
  190. }
  191. /**
  192. * Creates a user (helper method)
  193. *
  194. * @param string User first name
  195. * @param string User last name
  196. * @param int User status
  197. * @param string Login name
  198. * @param string Password (encrypted or not)
  199. * @param string Encrypt method. Leave blank if you are passing the password in clear text, set to the encrypt method used to encrypt the password otherwise. Remember
  200. * to include the salt in the extra fields if you are encrypting the password
  201. * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
  202. * @param string User id value. Leave blank if you are using the internal user_id
  203. * @param int Visibility.
  204. * @param string User email.
  205. * @param string Language.
  206. * @param string Phone.
  207. * @param string Expiration date
  208. * @param array Extra fields. An array with elements of the form ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field').
  209. * @return mixed New user id generated by the system, WSError otherwise
  210. */
  211. protected function createUserHelper($firstname, $lastname, $status, $login, $password, $encrypt_method, $user_id_field_name, $user_id_value, $visibility, $email, $language, $phone, $expiration_date, $extras) {
  212. global $api_failureList;
  213. // Add the original user id field name and value to the extra fields if needed
  214. $extras_associative = array();
  215. if($user_id_field_name != "chamilo_user_id") {
  216. $extras_associative[$user_id_field_name] = $user_id_value;
  217. }
  218. foreach($extras as $extra) {
  219. $extras_associative[$extra['field_name']] = $extra['field_value'];
  220. }
  221. $result = UserManager::create_user($firstname, $lastname, $status, $email, $login, $password, '', $language, $phone, '', PLATFORM_AUTH_SOURCE, $expiration_date, $visibility, 0, $extras_associative, $encrypt_method);
  222. if (!$result) {
  223. $failure = $api_failureList[0];
  224. if($failure == 'login-pass already taken') {
  225. return new WSError(102, 'This username is already taken');
  226. } else if($failure == 'encrypt_method invalid') {
  227. return new WSError(103, 'The encryption of the password is invalid');
  228. } else {
  229. return new WSError(104, 'There was an error creating the user');
  230. }
  231. } else {
  232. return $result;
  233. }
  234. }
  235. /**
  236. * Creates a user
  237. *
  238. * @param string API secret key
  239. * @param string User first name
  240. * @param string User last name
  241. * @param int User status
  242. * @param string Login name
  243. * @param string Password (encrypted or not)
  244. * @param string Encrypt method. Leave blank if you are passing the password in clear text, set to the encrypt method used to encrypt the password otherwise. Remember
  245. * to include the salt in the extra fields if you are encrypting the password
  246. * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
  247. * @param string User id value. Leave blank if you are using the internal user_id
  248. * @param int Visibility. Set by default to 1
  249. * @param string User email. Set by default to an empty string
  250. * @param string Language. Set by default to english
  251. * @param string Phone. Set by default to an empty string
  252. * @param string Expiration date. Set to null by default
  253. * @param array Extra fields. An array with elements of the form ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). Set to an empty array by default
  254. * @return int New user id generated by the system
  255. */
  256. public function CreateUser($secret_key, $firstname, $lastname, $status, $login, $password, $encrypt_method, $user_id_field_name, $user_id_value, $visibility = 1, $email = '', $language = 'english', $phone = '', $expiration_date = '0000-00-00 00:00:00', $extras = array()) {
  257. // First, verify the secret key
  258. $verifKey = $this->verifyKey($secret_key);
  259. if($verifKey instanceof WSError) {
  260. $this->handleError($verifKey);
  261. } else {
  262. $result = $this->createUserHelper($firstname, $lastname, $status, $login, $password, $encrypt_method, $user_id_field_name, $user_id_value, $visibility, $email, $language, $phone, $expiration_date, $extras);
  263. if($result instanceof WSError) {
  264. $this->handleError($result);
  265. } else {
  266. return $result;
  267. }
  268. }
  269. }
  270. /**
  271. * Creates multiple users
  272. *
  273. * @param string API secret key
  274. * @param array Users array. Each member of this array must follow the structure imposed by the CreateUser method
  275. * @return array Array with elements of the form array('user_id_value' => 'original value sent', 'user_id_generated' => 'value_generated', 'result' => array('code' => 0, 'message' => 'Operation was successful'))
  276. */
  277. public function CreateUsers($secret_key, $users) {
  278. $verifKey = $this->verifyKey($secret_key);
  279. if($verifKey instanceof WSError) {
  280. $this->handleError($verifKey);
  281. } else {
  282. $results = array();
  283. foreach($users as $user) {
  284. $result_tmp = array();
  285. // re-initialize variables just in case
  286. $firstname = $lastname = $status = $login = $password = $encrypt_method = $user_id_field_name = $user_id_value = $visibility = $email = $language = $phone = $expiration_date = $extras = null;
  287. extract($user);
  288. $result = $this->createUserHelper($firstname, $lastname, $status, $login, $password, $encrypt_method, $user_id_field_name, $user_id_value, $visibility, $email, $language, $phone, $expiration_date, $extras);
  289. if($result instanceof WSError) {
  290. $result_tmp['result'] = $result->toArray();
  291. $result_tmp['user_id_value'] = $user_id_value;
  292. $result_tmp['user_id_generated'] = 0;
  293. } else {
  294. $result_tmp['result'] = $this->getSuccessfulResult();
  295. $result_tmp['user_id_value'] = $user_id_value;
  296. $result_tmp['user_id_generated'] = $result;
  297. }
  298. $results[] = $result_tmp;
  299. }
  300. return $results;
  301. }
  302. }
  303. /**
  304. * Edits user info (helper method)
  305. *
  306. * @param string User id field name. Use "chamilo_user_id" in order to use internal system id
  307. * @param string User id value
  308. * @param string First name
  309. * @param string Last name
  310. * @param int User status
  311. * @param string Login name
  312. * @param string Password. Leave blank if you don't want to update it
  313. * @param string Encrypt method
  314. * @param string User email
  315. * @param string Language. Set by default to english
  316. * @param string Phone. Set by default to an empty string
  317. * @param string Expiration date. Set to null by default
  318. * @param array Extra fields. An array with elements of the form ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). Leave empty if you don't want to update
  319. * @return mixed True if user was successfully updated, WSError otherwise
  320. */
  321. protected function editUserHelper($user_id_field_name, $user_id_value, $firstname, $lastname, $status, $loginname, $password, $encrypt_method, $email, $language, $phone, $expiration_date, $extras) {
  322. global $api_failureList;
  323. $user_id = $this->getUserId($user_id_field_name, $user_id_value);
  324. if($user_id instanceof WSError) {
  325. return $user_id;
  326. } else {
  327. if($password == '') {
  328. $password = null;
  329. }
  330. $user_info = UserManager::get_user_info_by_id($user_id);
  331. if(count($extras) == 0) {
  332. $extras = null;
  333. }
  334. $result = UserManager::update_user($user_id, $firstname, $lastname, $loginname, $password, PLATFORM_AUTH_SOURCE, $email, $status, '', $phone, $user_info['picture_uri'], $expiration_date, $user_info['active'], null, $user_info['hr_dept_id'], $extras, $encrypt_method);
  335. if (!$result) {
  336. $failure = $api_failureList[0];
  337. if($failure == 'encrypt_method invalid') {
  338. return new WSError(103, 'The encryption of the password is invalid');
  339. } else {
  340. return new WSError(105, 'There was an error updating the user');
  341. }
  342. } else {
  343. return $result;
  344. }
  345. }
  346. }
  347. /**
  348. * Edits user info
  349. *
  350. * @param string API secret key
  351. * @param string User id field name. Use "chamilo_user_id" in order to use internal system id
  352. * @param string User id value
  353. * @param string First name
  354. * @param string Last name
  355. * @param int User status
  356. * @param string Login name
  357. * @param string Password. Leave blank if you don't want to update it
  358. * @param string Encrypt method
  359. * @param string User email
  360. * @param string Language. Set by default to english
  361. * @param string Phone. Set by default to an empty string
  362. * @param string Expiration date. Set to null by default
  363. * @param array Extra fields. An array with elements of the form ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). Leave empty if you don't want to update
  364. */
  365. public function EditUser($secret_key, $user_id_field_name, $user_id_value, $firstname, $lastname, $status, $loginname, $password, $encrypt_method, $email, $language, $phone, $expiration_date, $extras) {
  366. // First, verify the secret key
  367. $verifKey = $this->verifyKey($secret_key);
  368. if($verifKey instanceof WSError) {
  369. $this->handleError($verifKey);
  370. } else {
  371. $result = $this->editUserHelper($user_id_field_name, $user_id_value, $firstname, $lastname, $status, $loginname, $password, $encrypt_method, $email, $language, $phone, $expiration_date, $extras);
  372. if($result instanceof WSError) {
  373. $this->handleError($result);
  374. }
  375. }
  376. }
  377. /**
  378. * Edits multiple users
  379. *
  380. * @param string API secret key
  381. * @param array Users array. Each member of this array must follow the structure imposed by the EditUser method
  382. * @return array Array with elements like array('user_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). Note that if the result array contains a code different
  383. * than 0, an error occured
  384. */
  385. public function EditUsers($secret_key, $users) {
  386. $verifKey = $this->verifyKey($secret_key);
  387. if($verifKey instanceof WSError) {
  388. $this->handleError($verifKey);
  389. } else {
  390. $results = array();
  391. foreach($users as $user) {
  392. $result_tmp = array();
  393. // re-initialize variables just in case
  394. $user_id_field_name = $user_id_value = $firstname = $lastname = $status = $loginname = $password = $encrypt_method = $email = $language = $phone = $expiration_date = $extras = null;
  395. extract($user);
  396. $result_op = $this->editUserHelper($user_id_field_name, $user_id_value, $firstname, $lastname, $status, $loginname, $password, $encrypt_method, $email, $language, $phone, $expiration_date, $extras);
  397. $result_tmp['user_id_value'] = $user['user_id_value'];
  398. if($result_op instanceof WSError) {
  399. // Return the error in the results
  400. $result_tmp['result'] = $result_op->toArray();
  401. } else {
  402. $result_tmp['result'] = $this->getSuccessfulResult();
  403. }
  404. $results[] = $result_tmp;
  405. }
  406. return $results;
  407. }
  408. }
  409. }