webservice_user.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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($user_id_field_name, $user_id_value, $firstname, $lastname, $status, $loginname, $password, $encrypt_method, $email, $language, $phone, $expiration_date, $extras) {
  324. global $api_failureList;
  325. $user_id = $this->getUserId($user_id_field_name, $user_id_value);
  326. if($user_id instanceof WSError) {
  327. return $user_id;
  328. } else {
  329. if($password == '') {
  330. $password = null;
  331. }
  332. $user_info = UserManager::get_user_info_by_id($user_id);
  333. if(count($extras) == 0) {
  334. $extras = null;
  335. }
  336. $result = UserManager::update_user($user_id, $firstname, $lastname, $loginname, $password, PLATFORM_AUTH_SOURCE, $email, $status, '', $phone, $user_info['picture_uri'], $expiration_date, $user_info['active'], null, $user_info['hr_dept_id'], $extras, $encrypt_method);
  337. if (!$result) {
  338. $failure = $api_failureList[0];
  339. if($failure == 'encrypt_method invalid') {
  340. return new WSError(103, 'The encryption of the password is invalid');
  341. } else {
  342. return new WSError(105, 'There was an error updating the user');
  343. }
  344. } else {
  345. return $result;
  346. }
  347. }
  348. }
  349. /**
  350. * Edits user info
  351. *
  352. * @param string API secret key
  353. * @param string User id field name. Use "chamilo_user_id" in order to use internal system id
  354. * @param string User id value
  355. * @param string First name
  356. * @param string Last name
  357. * @param int User status
  358. * @param string Login name
  359. * @param string Password. Leave blank if you don't want to update it
  360. * @param string Encrypt method
  361. * @param string User email
  362. * @param string Language. Set by default to english
  363. * @param string Phone. Set by default to an empty string
  364. * @param string Expiration date. Set to null by default
  365. * @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
  366. */
  367. 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) {
  368. // First, verify the secret key
  369. $verifKey = $this->verifyKey($secret_key);
  370. if($verifKey instanceof WSError) {
  371. $this->handleError($verifKey);
  372. } else {
  373. $result = $this->editUserHelper($user_id_field_name, $user_id_value, $firstname, $lastname, $status, $loginname, $password, $encrypt_method, $email, $language, $phone, $expiration_date, $extras);
  374. if($result instanceof WSError) {
  375. $this->handleError($result);
  376. }
  377. }
  378. }
  379. /**
  380. * Edits multiple users
  381. *
  382. * @param string API secret key
  383. * @param array Users array. Each member of this array must follow the structure imposed by the EditUser method
  384. * @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
  385. * than 0, an error occured
  386. */
  387. public function EditUsers($secret_key, $users) {
  388. $verifKey = $this->verifyKey($secret_key);
  389. if($verifKey instanceof WSError) {
  390. $this->handleError($verifKey);
  391. } else {
  392. $results = array();
  393. foreach($users as $user) {
  394. $result_tmp = array();
  395. // re-initialize variables just in case
  396. $user_id_field_name = $user_id_value = $firstname = $lastname = $status = $loginname = $password = $encrypt_method = $email = $language = $phone = $expiration_date = $extras = null;
  397. extract($user);
  398. $result_op = $this->editUserHelper($user_id_field_name, $user_id_value, $firstname, $lastname, $status, $loginname, $password, $encrypt_method, $email, $language, $phone, $expiration_date, $extras);
  399. $result_tmp['user_id_value'] = $user['user_id_value'];
  400. if($result_op instanceof WSError) {
  401. // Return the error in the results
  402. $result_tmp['result'] = $result_op->toArray();
  403. } else {
  404. $result_tmp['result'] = $this->getSuccessfulResult();
  405. }
  406. $results[] = $result_tmp;
  407. }
  408. return $results;
  409. }
  410. }
  411. }