webservice_user.php 17 KB

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