webservice_user.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 = array()) {
  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. if (!empty($extras)) {
  219. foreach($extras as $extra) {
  220. $extras_associative[$extra['field_name']] = $extra['field_value'];
  221. }
  222. }
  223. $result = UserManager::create_user($firstname, $lastname, $status, $email, $login, $password, '', $language, $phone, '', PLATFORM_AUTH_SOURCE, $expiration_date, $visibility, 0, $extras_associative, $encrypt_method);
  224. if (!$result) {
  225. $failure = $api_failureList[0];
  226. if($failure == 'login-pass already taken') {
  227. return new WSError(102, 'This username is already taken');
  228. } else if($failure == 'encrypt_method invalid') {
  229. return new WSError(103, 'The encryption of the password is invalid');
  230. } else {
  231. return new WSError(104, 'There was an error creating the user');
  232. }
  233. } else {
  234. return $result;
  235. }
  236. }
  237. /**
  238. * Creates a user
  239. *
  240. * @param string API secret key
  241. * @param string User first name
  242. * @param string User last name
  243. * @param int User status
  244. * @param string Login name
  245. * @param string Password (encrypted or not)
  246. * @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
  247. * to include the salt in the extra fields if you are encrypting the password
  248. * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
  249. * @param string User id value. Leave blank if you are using the internal user_id
  250. * @param int Visibility. Set by default to 1
  251. * @param string User email. Set by default to an empty string
  252. * @param string Language. Set by default to english
  253. * @param string Phone. Set by default to an empty string
  254. * @param string Expiration date. Set to null by default
  255. * @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
  256. * @return int New user id generated by the system
  257. */
  258. 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()) {
  259. // First, verify the secret key
  260. $verifKey = $this->verifyKey($secret_key);
  261. if($verifKey instanceof WSError) {
  262. $this->handleError($verifKey);
  263. } else {
  264. $result = $this->createUserHelper($firstname, $lastname, $status, $login, $password, $encrypt_method, $user_id_field_name, $user_id_value, $visibility, $email, $language, $phone, $expiration_date, $extras);
  265. if($result instanceof WSError) {
  266. $this->handleError($result);
  267. } else {
  268. return $result;
  269. }
  270. }
  271. }
  272. /**
  273. * Creates multiple users
  274. *
  275. * @param string API secret key
  276. * @param array Users array. Each member of this array must follow the structure imposed by the CreateUser method
  277. * @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'))
  278. */
  279. public function CreateUsers($secret_key, $users) {
  280. $verifKey = $this->verifyKey($secret_key);
  281. if($verifKey instanceof WSError) {
  282. $this->handleError($verifKey);
  283. } else {
  284. $results = array();
  285. foreach($users as $user) {
  286. $result_tmp = array();
  287. // re-initialize variables just in case
  288. $firstname = $lastname = $status = $login = $password = $encrypt_method = $user_id_field_name = $user_id_value = $visibility = $email = $language = $phone = $expiration_date = $extras = null;
  289. extract($user);
  290. $result = $this->createUserHelper($firstname, $lastname, $status, $login, $password, $encrypt_method, $user_id_field_name, $user_id_value, $visibility, $email, $language, $phone, $expiration_date, $extras);
  291. if($result instanceof WSError) {
  292. $result_tmp['result'] = $result->toArray();
  293. $result_tmp['user_id_value'] = $user_id_value;
  294. $result_tmp['user_id_generated'] = 0;
  295. } else {
  296. $result_tmp['result'] = $this->getSuccessfulResult();
  297. $result_tmp['user_id_value'] = $user_id_value;
  298. $result_tmp['user_id_generated'] = $result;
  299. }
  300. $results[] = $result_tmp;
  301. }
  302. return $results;
  303. }
  304. }
  305. /**
  306. * Edits user info (helper method)
  307. *
  308. * @param string User id field name. Use "chamilo_user_id" in order to use internal system id
  309. * @param string User id value
  310. * @param string First name
  311. * @param string Last name
  312. * @param int User status
  313. * @param string Login name
  314. * @param string Password. Leave blank if you don't want to update it
  315. * @param string Encrypt method
  316. * @param string User email
  317. * @param string Language. Set by default to english
  318. * @param string Phone. Set by default to an empty string
  319. * @param string Expiration date. Set to null by default
  320. * @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
  321. * @return mixed True if user was successfully updated, WSError otherwise
  322. */
  323. protected function editUserHelper(
  324. $user_id_field_name,
  325. $user_id_value,
  326. $firstname,
  327. $lastname,
  328. $status,
  329. $loginname,
  330. $password,
  331. $encrypt_method,
  332. $email,
  333. $language,
  334. $phone,
  335. $expiration_date,
  336. $extras
  337. ) {
  338. global $api_failureList;
  339. $user_id = $this->getUserId($user_id_field_name, $user_id_value);
  340. if($user_id instanceof WSError) {
  341. return $user_id;
  342. } else {
  343. if($password == '') {
  344. $password = null;
  345. }
  346. $user_info = api_get_user_info($user_id);
  347. if (count($extras) == 0) {
  348. $extras = null;
  349. }
  350. $result = UserManager::update_user(
  351. $user_id,
  352. $firstname,
  353. $lastname,
  354. $loginname,
  355. $password,
  356. PLATFORM_AUTH_SOURCE,
  357. $email,
  358. $status,
  359. '',
  360. $phone,
  361. $user_info['picture_uri'],
  362. $expiration_date,
  363. $user_info['active'],
  364. null,
  365. $user_info['hr_dept_id'],
  366. $extras,
  367. $encrypt_method
  368. );
  369. if (!$result) {
  370. $failure = $api_failureList[0];
  371. if($failure == 'encrypt_method invalid') {
  372. return new WSError(103, 'The encryption of the password is invalid');
  373. } else {
  374. return new WSError(105, 'There was an error updating the user');
  375. }
  376. } else {
  377. return $result;
  378. }
  379. }
  380. }
  381. /**
  382. * Edits user info
  383. *
  384. * @param string API secret key
  385. * @param string User id field name. Use "chamilo_user_id" in order to use internal system id
  386. * @param string User id value
  387. * @param string First name
  388. * @param string Last name
  389. * @param int User status
  390. * @param string Login name
  391. * @param string Password. Leave blank if you don't want to update it
  392. * @param string Encrypt method
  393. * @param string User email
  394. * @param string Language. Set by default to english
  395. * @param string Phone. Set by default to an empty string
  396. * @param string Expiration date. Set to null by default
  397. * @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
  398. */
  399. 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) {
  400. // First, verify the secret key
  401. $verifKey = $this->verifyKey($secret_key);
  402. if($verifKey instanceof WSError) {
  403. $this->handleError($verifKey);
  404. } else {
  405. $extras_associative = array();
  406. if (!empty($extras)) {
  407. foreach($extras as $extra) {
  408. $extras_associative[$extra['field_name']] = $extra['field_value'];
  409. }
  410. }
  411. $result = $this->editUserHelper($user_id_field_name, $user_id_value, $firstname, $lastname, $status, $loginname, $password, $encrypt_method, $email, $language, $phone, $expiration_date, $extras_associative);
  412. if($result instanceof WSError) {
  413. $this->handleError($result);
  414. }
  415. }
  416. }
  417. /**
  418. * Edits multiple users
  419. *
  420. * @param string API secret key
  421. * @param array Users array. Each member of this array must follow the structure imposed by the EditUser method
  422. * @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
  423. * than 0, an error occured
  424. */
  425. public function EditUsers($secret_key, $users) {
  426. $verifKey = $this->verifyKey($secret_key);
  427. if($verifKey instanceof WSError) {
  428. $this->handleError($verifKey);
  429. } else {
  430. $results = array();
  431. foreach($users as $user) {
  432. $result_tmp = array();
  433. // re-initialize variables just in case
  434. $user_id_field_name = $user_id_value = $firstname = $lastname = $status = $loginname = $password = $encrypt_method = $email = $language = $phone = $expiration_date = $extras = null;
  435. extract($user);
  436. $result_op = $this->editUserHelper($user_id_field_name, $user_id_value, $firstname, $lastname, $status, $loginname, $password, $encrypt_method, $email, $language, $phone, $expiration_date, $extras);
  437. $result_tmp['user_id_value'] = $user['user_id_value'];
  438. if($result_op instanceof WSError) {
  439. // Return the error in the results
  440. $result_tmp['result'] = $result_op->toArray();
  441. } else {
  442. $result_tmp['result'] = $this->getSuccessfulResult();
  443. }
  444. $results[] = $result_tmp;
  445. }
  446. return $results;
  447. }
  448. }
  449. }