webservice_user.php 22 KB

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