webservice_user.php 22 KB

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