webservice_user.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. $libpath = api_get_path(LIBRARY_PATH);
  8. require_once $libpath.'usermanager.lib.php';
  9. require_once(dirname(__FILE__).'/webservice.php');
  10. /**
  11. * Web services available for the User module. This class extends the WS class
  12. */
  13. class WSUser extends WS {
  14. /**
  15. * Enables or disables a user
  16. *
  17. * @param string User id field name
  18. * @param string User id value
  19. * @param int Set to 1 to enable and to 0 to disable
  20. */
  21. protected function changeUserActiveState($user_id_field_name, $user_id_value, $state) {
  22. $user_id = $this->getUserId($user_id_field_name, $user_id_value);
  23. if($user_id instanceof WSError) {
  24. return $user_id;
  25. } else {
  26. if($state == 0) {
  27. UserManager::disable($user_id);
  28. } else if($state == 1) {
  29. UserManager::enable($user_id);
  30. }
  31. }
  32. }
  33. /**
  34. * Enables or disables multiple users
  35. *
  36. * @param array Users
  37. * @param int Set to 1 to enable and to 0 to disable
  38. * @return array Array of results
  39. */
  40. protected function changeUsersActiveState($users, $state) {
  41. $results = array();
  42. foreach($users as $user) {
  43. $result_tmp = array();
  44. $result_op = $this->changeUserActiveState($user['user_id_field_name'], $user['user_id_value'], $state);
  45. $result_tmp['user_id_value'] = $user['user_id_value'];
  46. if($result_op instanceof WSError) {
  47. // Return the error in the results
  48. $result_tmp['result'] = $result_op->toArray();
  49. } else {
  50. $result_tmp['result'] = $this->getSuccessfulResult();
  51. }
  52. $results[] = $result_tmp;
  53. }
  54. return $results;
  55. }
  56. /**
  57. * Disables a user
  58. *
  59. * @param string API secret key
  60. * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
  61. * @param string User id value
  62. */
  63. public function DisableUser($secret_key, $user_id_field_name, $user_id_value) {
  64. $verifKey = $this->verifyKey($secret_key);
  65. if($verifKey instanceof WSError) {
  66. // Let the implementation handle it
  67. $this->handleError($verifKey);
  68. } else {
  69. $result = $this->changeUserActiveState($user_id_field_name, $user_id_value, 0);
  70. if($result instanceof WSError) {
  71. $this->handleError($result);
  72. }
  73. }
  74. }
  75. /**
  76. * Disables multiple users
  77. *
  78. * @param string API secret key
  79. * @param array Array of users with elements of the form array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value')
  80. * @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
  81. * than 0, an error occured
  82. */
  83. public function DisableUsers($secret_key, $users) {
  84. $verifKey = $this->verifyKey($secret_key);
  85. if($verifKey instanceof WSError) {
  86. // Let the implementation handle it
  87. $this->handleError($verifKey);
  88. } else {
  89. return $this->changeUsersActiveState($users, 0);
  90. }
  91. }
  92. /**
  93. * Enables a user
  94. *
  95. * @param string API secret key
  96. * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
  97. * @param string User id value
  98. */
  99. public function EnableUser($secret_key, $user_id_field_name, $user_id_value) {
  100. $verifKey = $this->verifyKey($secret_key);
  101. if($verifKey instanceof WSError) {
  102. $this->handleError($verifKey);
  103. } else {
  104. $result = $this->changeUserActiveState($user_id_field_name, $user_id_value, 1);
  105. if($result instanceof WSError) {
  106. $this->handleError($result);
  107. }
  108. }
  109. }
  110. /**
  111. * Enables multiple users
  112. *
  113. * @param string API secret key
  114. * @param array Array of users with elements of the form array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value')
  115. * @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
  116. * than 0, an error occured
  117. */
  118. public function EnableUsers($secret_key, $users) {
  119. $verifKey = $this->verifyKey($secret_key);
  120. if($verifKey instanceof WSError) {
  121. // Let the implementation handle it
  122. $this->handleError($verifKey);
  123. } else {
  124. return $this->changeUsersActiveState($users, 1);
  125. }
  126. }
  127. /**
  128. * Deletes a user (helper method)
  129. *
  130. * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
  131. * @param string User id value
  132. * @return mixed True if user was successfully deleted, WSError otherwise
  133. */
  134. protected function deleteUserHelper($user_id_field_name, $user_id_value) {
  135. $user_id = $this->getUserId($user_id_field_name, $user_id_value);
  136. if($user_id instanceof WSError) {
  137. return $user_id;
  138. } else {
  139. if(!UserManager::delete_user($user_id)) {
  140. return new WSError(101, "There was a problem while deleting this user");
  141. } else {
  142. return true;
  143. }
  144. }
  145. }
  146. /**
  147. * Deletes a user
  148. *
  149. * @param string API secret key
  150. * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
  151. * @param string User id value
  152. */
  153. public function DeleteUser($secret_key, $user_id_field_name, $user_id_value) {
  154. $verifKey = $this->verifyKey($secret_key);
  155. if($verifKey instanceof WSError) {
  156. $this->handleError($verifKey);
  157. } else {
  158. $result = $this->deleteUserHelper($user_id_field_name, $user_id_value);
  159. if($result instanceof WSError) {
  160. $this->handleError($result);
  161. }
  162. }
  163. }
  164. /**
  165. * Deletes multiple users
  166. *
  167. * @param string API secret key
  168. * @param array Array of users with elements of the form array('user_id_field_name' => 'name_of_field', 'user_id_value' => 'value')
  169. * @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
  170. * than 0, an error occured
  171. */
  172. public function DeleteUsers($secret_key, $users) {
  173. $verifKey = $this->verifyKey($secret_key);
  174. if($verifKey instanceof WSError) {
  175. $this->handleError($verifKey);
  176. } else {
  177. $results = array();
  178. foreach($users as $user) {
  179. $result_tmp = array();
  180. $result_op = $this->deleteUserHelper($user['user_id_field_name'], $user['user_id_value']);
  181. $result_tmp['user_id_value'] = $user['user_id_value'];
  182. if($result_op instanceof WSError) {
  183. // Return the error in the results
  184. $result_tmp['result'] = $result_op->toArray();
  185. } else {
  186. $result_tmp['result'] = $this->getSuccessfulResult();
  187. }
  188. $results[] = $result_tmp;
  189. }
  190. return $results;
  191. }
  192. }
  193. /**
  194. * Creates a user (helper method)
  195. *
  196. * @param string User first name
  197. * @param string User last name
  198. * @param int User status
  199. * @param string Login name
  200. * @param string Password (encrypted or not)
  201. * @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
  202. * to include the salt in the extra fields if you are encrypting the password
  203. * @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
  204. * @param string User id value. Leave blank if you are using the internal user_id
  205. * @param int Visibility.
  206. * @param string User email.
  207. * @param string Language.
  208. * @param string Phone.
  209. * @param string Expiration date
  210. * @param array Extra fields. An array with elements of the form ('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field').
  211. * @return mixed New user id generated by the system, WSError otherwise
  212. */
  213. protected function createUserHelper($firstname, $lastname, $status, $login, $password, $encrypt_method, $user_id_field_name, $user_id_value, $visibility, $email, $language, $phone, $expiration_date, $extras) {
  214. global $api_failureList;
  215. // Add the original user id field name and value to the extra fields if needed
  216. $extras_associative = array();
  217. if($user_id_field_name != "chamilo_user_id") {
  218. $extras_associative[$user_id_field_name] = $user_id_value;
  219. }
  220. foreach($extras as $extra) {
  221. $extras_associative[$extra['field_name']] = $extra['field_value'];
  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. extract($user);
  288. $result = $this->createUserHelper($firstname, $lastname, $status, $login, $password, $encrypt_method, $user_id_field_name, $user_id_value, $visibility, $email, $language, $phone, $expiration_date, $extras);
  289. if($result instanceof WSError) {
  290. $result_tmp['result'] = $result->toArray();
  291. $result_tmp['user_id_value'] = $user_id_value;
  292. $result_tmp['user_id_generated'] = 0;
  293. } else {
  294. $result_tmp['result'] = $this->getSuccessfulResult();
  295. $result_tmp['user_id_value'] = $user_id_value;
  296. $result_tmp['user_id_generated'] = $result;
  297. }
  298. $results[] = $result_tmp;
  299. }
  300. return $results;
  301. }
  302. }
  303. /**
  304. * Edits user info (helper method)
  305. *
  306. * @param string User id field name. Use "chamilo_user_id" in order to use internal system id
  307. * @param string User id value
  308. * @param string First name
  309. * @param string Last name
  310. * @param int User status
  311. * @param string Login name
  312. * @param string Password. Leave blank if you don't want to update it
  313. * @param string Encrypt method
  314. * @param string User email
  315. * @param string Language. Set by default to english
  316. * @param string Phone. Set by default to an empty string
  317. * @param string Expiration date. Set to null by default
  318. * @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
  319. * @return mixed True if user was successfully updated, WSError otherwise
  320. */
  321. protected function editUserHelper($user_id_field_name, $user_id_value, $firstname, $lastname, $status, $loginname, $password, $encrypt_method, $email, $language, $phone, $expiration_date, $extras) {
  322. global $api_failureList;
  323. $user_id = $this->getUserId($user_id_field_name, $user_id_value);
  324. if($user_id instanceof WSError) {
  325. return $user_id;
  326. } else {
  327. if($password == '') {
  328. $password = null;
  329. }
  330. $user_info = UserManager::get_user_info_by_id($user_id);
  331. if(count($extras) == 0) {
  332. $extras = null;
  333. }
  334. $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);
  335. if (!$result) {
  336. $failure = $api_failureList[0];
  337. if($failure == 'encrypt_method invalid') {
  338. return new WSError(103, 'The encryption of the password is invalid');
  339. } else {
  340. return new WSError(105, 'There was an error updating the user');
  341. }
  342. } else {
  343. return $result;
  344. }
  345. }
  346. }
  347. /**
  348. * Edits user info
  349. *
  350. * @param string API secret key
  351. * @param string User id field name. Use "chamilo_user_id" in order to use internal system id
  352. * @param string User id value
  353. * @param string First name
  354. * @param string Last name
  355. * @param int User status
  356. * @param string Login name
  357. * @param string Password. Leave blank if you don't want to update it
  358. * @param string Encrypt method
  359. * @param string User email
  360. * @param string Language. Set by default to english
  361. * @param string Phone. Set by default to an empty string
  362. * @param string Expiration date. Set to null by default
  363. * @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
  364. */
  365. 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) {
  366. // First, verify the secret key
  367. $verifKey = $this->verifyKey($secret_key);
  368. if($verifKey instanceof WSError) {
  369. $this->handleError($verifKey);
  370. } else {
  371. $result = $this->editUserHelper($user_id_field_name, $user_id_value, $firstname, $lastname, $status, $loginname, $password, $encrypt_method, $user_id_field_name, $email, $language, $phone, $expiration_date, $extras);
  372. if($result instanceof WSError) {
  373. $this->handleError($result);
  374. }
  375. }
  376. }
  377. /**
  378. * Edits multiple users
  379. *
  380. * @param string API secret key
  381. * @param array Users array. Each member of this array must follow the structure imposed by the EditUser method
  382. * @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
  383. * than 0, an error occured
  384. */
  385. public function EditUsers($secret_key, $users) {
  386. $verifKey = $this->verifyKey($secret_key);
  387. if($verifKey instanceof WSError) {
  388. $this->handleError($verifKey);
  389. } else {
  390. $results = array();
  391. foreach($users as $user) {
  392. $result_tmp = array();
  393. extract($user);
  394. $result_op = $this->editUserHelper($user_id_field_name, $user_id_value, $firstname, $lastname, $status, $loginname, $password, $encrypt_method, $email, $language, $phone, $expiration_date, $extras);
  395. $result_tmp['user_id_value'] = $user['user_id_value'];
  396. if($result_op instanceof WSError) {
  397. // Return the error in the results
  398. $result_tmp['result'] = $result_op->toArray();
  399. } else {
  400. $result_tmp['result'] = $this->getSuccessfulResult();
  401. }
  402. $results[] = $result_tmp;
  403. }
  404. return $results;
  405. }
  406. }
  407. }