webservice_user.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.webservices
  5. */
  6. require_once __DIR__.'/../inc/global.inc.php';
  7. require_once __DIR__.'/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. /**
  14. * Enables or disables a user
  15. *
  16. * @param string User id field name
  17. * @param string User id value
  18. * @param int Set to 1 to enable and to 0 to disable
  19. */
  20. protected function changeUserActiveState(
  21. $user_id_field_name,
  22. $user_id_value,
  23. $state
  24. ) {
  25. $user_id = $this->getUserId($user_id_field_name, $user_id_value);
  26. if ($user_id instanceof WSError) {
  27. return $user_id;
  28. } else {
  29. if ($state == 0) {
  30. UserManager::disable($user_id);
  31. } else {
  32. if ($state == 1) {
  33. UserManager::enable($user_id);
  34. }
  35. }
  36. }
  37. }
  38. /**
  39. * Enables or disables multiple users
  40. *
  41. * @param array Users
  42. * @param int Set to 1 to enable and to 0 to disable
  43. * @return array Array of results
  44. */
  45. protected function changeUsersActiveState($users, $state)
  46. {
  47. $results = array();
  48. foreach ($users as $user) {
  49. $result_tmp = array();
  50. $result_op = $this->changeUserActiveState(
  51. $user['user_id_field_name'],
  52. $user['user_id_value'],
  53. $state
  54. );
  55. $result_tmp['user_id_value'] = $user['user_id_value'];
  56. if ($result_op instanceof WSError) {
  57. // Return the error in the results
  58. $result_tmp['result'] = $result_op->toArray();
  59. } else {
  60. $result_tmp['result'] = $this->getSuccessfulResult();
  61. }
  62. $results[] = $result_tmp;
  63. }
  64. return $results;
  65. }
  66. /**
  67. * Disables a user
  68. *
  69. * @param string API secret key
  70. * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
  71. * @param string User id value
  72. */
  73. public function DisableUser(
  74. $secret_key,
  75. $user_id_field_name,
  76. $user_id_value
  77. ) {
  78. $verifKey = $this->verifyKey($secret_key);
  79. if ($verifKey instanceof WSError) {
  80. // Let the implementation handle it
  81. $this->handleError($verifKey);
  82. } else {
  83. $result = $this->changeUserActiveState(
  84. $user_id_field_name,
  85. $user_id_value,
  86. 0
  87. );
  88. if ($result instanceof WSError) {
  89. $this->handleError($result);
  90. }
  91. }
  92. }
  93. /**
  94. * Disables multiple users
  95. *
  96. * @param string API secret key
  97. * @param array Array of users with elements of the form array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value')
  98. * @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
  99. * than 0, an error occured
  100. */
  101. public function DisableUsers($secret_key, $users)
  102. {
  103. $verifKey = $this->verifyKey($secret_key);
  104. if ($verifKey instanceof WSError) {
  105. // Let the implementation handle it
  106. $this->handleError($verifKey);
  107. } else {
  108. return $this->changeUsersActiveState($users, 0);
  109. }
  110. }
  111. /**
  112. * Enables a user
  113. *
  114. * @param string API secret key
  115. * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
  116. * @param string User id value
  117. */
  118. public function EnableUser($secret_key, $user_id_field_name, $user_id_value)
  119. {
  120. $verifKey = $this->verifyKey($secret_key);
  121. if ($verifKey instanceof WSError) {
  122. $this->handleError($verifKey);
  123. } else {
  124. $result = $this->changeUserActiveState(
  125. $user_id_field_name,
  126. $user_id_value,
  127. 1
  128. );
  129. if ($result instanceof WSError) {
  130. $this->handleError($result);
  131. }
  132. }
  133. }
  134. /**
  135. * Enables multiple users
  136. *
  137. * @param string API secret key
  138. * @param array Array of users with elements of the form array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value')
  139. * @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
  140. * than 0, an error occured
  141. */
  142. public function EnableUsers($secret_key, $users)
  143. {
  144. $verifKey = $this->verifyKey($secret_key);
  145. if ($verifKey instanceof WSError) {
  146. // Let the implementation handle it
  147. $this->handleError($verifKey);
  148. } else {
  149. return $this->changeUsersActiveState($users, 1);
  150. }
  151. }
  152. /**
  153. * Deletes a user (helper method)
  154. *
  155. * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
  156. * @param string User id value
  157. * @return mixed True if user was successfully deleted, WSError otherwise
  158. */
  159. protected function deleteUserHelper($user_id_field_name, $user_id_value)
  160. {
  161. $user_id = $this->getUserId($user_id_field_name, $user_id_value);
  162. if ($user_id instanceof WSError) {
  163. return $user_id;
  164. } else {
  165. if (!UserManager::delete_user($user_id)) {
  166. return new WSError(
  167. 101,
  168. "There was a problem while deleting this user"
  169. );
  170. } else {
  171. return true;
  172. }
  173. }
  174. }
  175. /**
  176. * Deletes a user
  177. *
  178. * @param string API secret key
  179. * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
  180. * @param string User id value
  181. */
  182. public function DeleteUser($secret_key, $user_id_field_name, $user_id_value)
  183. {
  184. $verifKey = $this->verifyKey($secret_key);
  185. if ($verifKey instanceof WSError) {
  186. $this->handleError($verifKey);
  187. } else {
  188. $result = $this->deleteUserHelper(
  189. $user_id_field_name,
  190. $user_id_value
  191. );
  192. if ($result instanceof WSError) {
  193. $this->handleError($result);
  194. }
  195. }
  196. }
  197. /**
  198. * Deletes multiple users
  199. *
  200. * @param string API secret key
  201. * @param array Array of users with elements of the form array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value')
  202. * @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
  203. * than 0, an error occured
  204. */
  205. public function DeleteUsers($secret_key, $users)
  206. {
  207. $verifKey = $this->verifyKey($secret_key);
  208. if ($verifKey instanceof WSError) {
  209. $this->handleError($verifKey);
  210. } else {
  211. $results = array();
  212. foreach ($users as $user) {
  213. $result_tmp = array();
  214. $result_op = $this->deleteUserHelper(
  215. $user['user_id_field_name'],
  216. $user['user_id_value']
  217. );
  218. $result_tmp['user_id_value'] = $user['user_id_value'];
  219. if ($result_op instanceof WSError) {
  220. // Return the error in the results
  221. $result_tmp['result'] = $result_op->toArray();
  222. } else {
  223. $result_tmp['result'] = $this->getSuccessfulResult();
  224. }
  225. $results[] = $result_tmp;
  226. }
  227. return $results;
  228. }
  229. }
  230. /**
  231. * Creates a user (helper method)
  232. *
  233. * @param string User first name
  234. * @param string User last name
  235. * @param int User status
  236. * @param string Login name
  237. * @param string Password (encrypted or not)
  238. * @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
  239. * to include the salt in the extra fields if you are encrypting the password
  240. * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
  241. * @param string User id value. Leave blank if you are using the internal user_id
  242. * @param int Visibility.
  243. * @param string User email.
  244. * @param string Language.
  245. * @param string Phone.
  246. * @param string Expiration date
  247. * @param array Extra fields. An array with elements of the form ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field').
  248. * @return mixed New user id generated by the system, WSError otherwise
  249. */
  250. protected function createUserHelper(
  251. $firstname,
  252. $lastname,
  253. $status,
  254. $login,
  255. $password,
  256. $encrypt_method,
  257. $user_id_field_name,
  258. $user_id_value,
  259. $visibility,
  260. $email,
  261. $language,
  262. $phone,
  263. $expiration_date,
  264. $extras = array()
  265. ) {
  266. // Add the original user id field name and value to the extra fields if needed
  267. $extras_associative = array();
  268. if ($user_id_field_name != "chamilo_user_id") {
  269. $extras_associative[$user_id_field_name] = $user_id_value;
  270. }
  271. if (!empty($extras)) {
  272. foreach ($extras as $extra) {
  273. $extras_associative[$extra['field_name']] = $extra['field_value'];
  274. }
  275. }
  276. $result = UserManager::create_user(
  277. $firstname,
  278. $lastname,
  279. $status,
  280. $email,
  281. $login,
  282. $password,
  283. '',
  284. $language,
  285. $phone,
  286. '',
  287. PLATFORM_AUTH_SOURCE,
  288. $expiration_date,
  289. $visibility,
  290. 0,
  291. $extras_associative,
  292. $encrypt_method
  293. );
  294. if (!$result) {
  295. return new WSError(104, 'There was an error creating the user');
  296. /*$failure = $api_failureList[0];
  297. if($failure == 'login-pass already taken') {
  298. return new WSError(102, 'This username is already taken');
  299. } else if($failure == 'encrypt_method invalid') {
  300. return new WSError(103, 'The encryption of the password is invalid');
  301. } else {
  302. return new WSError(104, 'There was an error creating the user');
  303. }*/
  304. } else {
  305. return $result;
  306. }
  307. }
  308. /**
  309. * Creates a user
  310. *
  311. * @param string API secret key
  312. * @param string User first name
  313. * @param string User last name
  314. * @param int User status
  315. * @param string Login name
  316. * @param string Password (encrypted or not)
  317. * @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
  318. * to include the salt in the extra fields if you are encrypting the password
  319. * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
  320. * @param string User id value. Leave blank if you are using the internal user_id
  321. * @param int Visibility. Set by default to 1
  322. * @param string User email. Set by default to an empty string
  323. * @param string Language. Set by default to english
  324. * @param string Phone. Set by default to an empty string
  325. * @param string Expiration date. Set to null by default
  326. * @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
  327. * @return int New user id generated by the system
  328. */
  329. public function CreateUser(
  330. $secret_key,
  331. $firstname,
  332. $lastname,
  333. $status,
  334. $login,
  335. $password,
  336. $encrypt_method,
  337. $user_id_field_name,
  338. $user_id_value,
  339. $visibility = 1,
  340. $email = '',
  341. $language = 'english',
  342. $phone = '',
  343. $expiration_date = '0000-00-00 00:00:00',
  344. $extras = array()
  345. ) {
  346. // First, verify the secret key
  347. $verifKey = $this->verifyKey($secret_key);
  348. if ($verifKey instanceof WSError) {
  349. $this->handleError($verifKey);
  350. } else {
  351. $result = $this->createUserHelper(
  352. $firstname,
  353. $lastname,
  354. $status,
  355. $login,
  356. $password,
  357. $encrypt_method,
  358. $user_id_field_name,
  359. $user_id_value,
  360. $visibility,
  361. $email,
  362. $language,
  363. $phone,
  364. $expiration_date,
  365. $extras
  366. );
  367. if ($result instanceof WSError) {
  368. $this->handleError($result);
  369. } else {
  370. return $result;
  371. }
  372. }
  373. }
  374. /**
  375. * Creates multiple users
  376. *
  377. * @param string API secret key
  378. * @param array Users array. Each member of this array must follow the structure imposed by the CreateUser method
  379. * @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'))
  380. */
  381. public function CreateUsers($secret_key, $users)
  382. {
  383. $verifKey = $this->verifyKey($secret_key);
  384. if ($verifKey instanceof WSError) {
  385. $this->handleError($verifKey);
  386. } else {
  387. $results = array();
  388. foreach ($users as $user) {
  389. $result_tmp = array();
  390. // re-initialize variables just in case
  391. $firstname = $lastname = $status = $login = $password = $encrypt_method = $user_id_field_name = $user_id_value = $visibility = $email = $language = $phone = $expiration_date = $extras = null;
  392. extract($user);
  393. $result = $this->createUserHelper(
  394. $firstname,
  395. $lastname,
  396. $status,
  397. $login,
  398. $password,
  399. $encrypt_method,
  400. $user_id_field_name,
  401. $user_id_value,
  402. $visibility,
  403. $email,
  404. $language,
  405. $phone,
  406. $expiration_date,
  407. $extras
  408. );
  409. if ($result instanceof WSError) {
  410. $result_tmp['result'] = $result->toArray();
  411. $result_tmp['user_id_value'] = $user_id_value;
  412. $result_tmp['user_id_generated'] = 0;
  413. } else {
  414. $result_tmp['result'] = $this->getSuccessfulResult();
  415. $result_tmp['user_id_value'] = $user_id_value;
  416. $result_tmp['user_id_generated'] = $result;
  417. }
  418. $results[] = $result_tmp;
  419. }
  420. return $results;
  421. }
  422. }
  423. /**
  424. * Edits user info (helper method)
  425. *
  426. * @param string User id field name. Use "chamilo_user_id" in order to use internal system id
  427. * @param string User id value
  428. * @param string First name
  429. * @param string Last name
  430. * @param int User status
  431. * @param string Login name
  432. * @param string Password. Leave blank if you don't want to update it
  433. * @param string Encrypt method
  434. * @param string User email
  435. * @param string Language. Set by default to english
  436. * @param string Phone. Set by default to an empty string
  437. * @param string Expiration date. Set to null by default
  438. * @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
  439. * @return mixed True if user was successfully updated, WSError otherwise
  440. */
  441. protected function editUserHelper(
  442. $user_id_field_name,
  443. $user_id_value,
  444. $firstname,
  445. $lastname,
  446. $status,
  447. $loginname,
  448. $password,
  449. $encrypt_method,
  450. $email,
  451. $language,
  452. $phone,
  453. $expiration_date,
  454. $extras
  455. ) {
  456. $user_id = $this->getUserId($user_id_field_name, $user_id_value);
  457. if ($user_id instanceof WSError) {
  458. return $user_id;
  459. } else {
  460. if ($password == '') {
  461. $password = null;
  462. }
  463. $user_info = api_get_user_info($user_id);
  464. if (count($extras) == 0) {
  465. $extras = null;
  466. }
  467. $result = UserManager::update_user(
  468. $user_id,
  469. $firstname,
  470. $lastname,
  471. $loginname,
  472. $password,
  473. PLATFORM_AUTH_SOURCE,
  474. $email,
  475. $status,
  476. '',
  477. $phone,
  478. $user_info['picture_uri'],
  479. $expiration_date,
  480. $user_info['active'],
  481. null,
  482. $user_info['hr_dept_id'],
  483. $extras,
  484. $encrypt_method
  485. );
  486. if (!$result) {
  487. /*if($failure == 'encrypt_method invalid') {
  488. return new WSError(103, 'The encryption of the password is invalid');
  489. } else {
  490. return new WSError(105, 'There was an error updating the user');
  491. }*/
  492. return new WSError(105, 'There was an error updating the user');
  493. } else {
  494. return $result;
  495. }
  496. }
  497. }
  498. /**
  499. * Edits user info
  500. *
  501. * @param string API secret key
  502. * @param string User id field name. Use "chamilo_user_id" in order to use internal system id
  503. * @param string User id value
  504. * @param string First name
  505. * @param string Last name
  506. * @param int User status
  507. * @param string Login name
  508. * @param string Password. Leave blank if you don't want to update it
  509. * @param string Encrypt method
  510. * @param string User email
  511. * @param string Language. Set by default to english
  512. * @param string Phone. Set by default to an empty string
  513. * @param string Expiration date. Set to null by default
  514. * @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
  515. */
  516. public function EditUser(
  517. $secret_key,
  518. $user_id_field_name,
  519. $user_id_value,
  520. $firstname,
  521. $lastname,
  522. $status,
  523. $loginname,
  524. $password,
  525. $encrypt_method,
  526. $email,
  527. $language,
  528. $phone,
  529. $expiration_date,
  530. $extras
  531. ) {
  532. // First, verify the secret key
  533. $verifKey = $this->verifyKey($secret_key);
  534. if ($verifKey instanceof WSError) {
  535. $this->handleError($verifKey);
  536. } else {
  537. $extras_associative = array();
  538. if (!empty($extras)) {
  539. foreach ($extras as $extra) {
  540. $extras_associative[$extra['field_name']] = $extra['field_value'];
  541. }
  542. }
  543. $result = $this->editUserHelper(
  544. $user_id_field_name,
  545. $user_id_value,
  546. $firstname,
  547. $lastname,
  548. $status,
  549. $loginname,
  550. $password,
  551. $encrypt_method,
  552. $email,
  553. $language,
  554. $phone,
  555. $expiration_date,
  556. $extras_associative
  557. );
  558. if ($result instanceof WSError) {
  559. $this->handleError($result);
  560. }
  561. }
  562. }
  563. /**
  564. * Edits multiple users
  565. *
  566. * @param string API secret key
  567. * @param array Users array. Each member of this array must follow the structure imposed by the EditUser method
  568. * @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
  569. * than 0, an error occured
  570. */
  571. public function EditUsers($secret_key, $users)
  572. {
  573. $verifKey = $this->verifyKey($secret_key);
  574. if ($verifKey instanceof WSError) {
  575. $this->handleError($verifKey);
  576. } else {
  577. $results = array();
  578. foreach ($users as $user) {
  579. $result_tmp = array();
  580. // re-initialize variables just in case
  581. $user_id_field_name = $user_id_value = $firstname = $lastname = $status = $loginname = $password = $encrypt_method = $email = $language = $phone = $expiration_date = $extras = null;
  582. extract($user);
  583. $result_op = $this->editUserHelper(
  584. $user_id_field_name,
  585. $user_id_value,
  586. $firstname,
  587. $lastname,
  588. $status,
  589. $loginname,
  590. $password,
  591. $encrypt_method,
  592. $email,
  593. $language,
  594. $phone,
  595. $expiration_date,
  596. $extras
  597. );
  598. $result_tmp['user_id_value'] = $user['user_id_value'];
  599. if ($result_op instanceof WSError) {
  600. // Return the error in the results
  601. $result_tmp['result'] = $result_op->toArray();
  602. } else {
  603. $result_tmp['result'] = $this->getSuccessfulResult();
  604. }
  605. $results[] = $result_tmp;
  606. }
  607. return $results;
  608. }
  609. }
  610. }