ws.lib.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /* Copyright (C) 2011 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. * or see https://www.gnu.org/
  17. */
  18. /**
  19. * \file htdocs/core/lib/ws.lib.php
  20. * \ingroup webservices
  21. * \brief Set of function for manipulating web services
  22. */
  23. /**
  24. * Check authentication array and set error, errorcode, errorlabel
  25. *
  26. * @param array $authentication Array with authentication informations ('login'=>,'password'=>,'entity'=>,'dolibarrkey'=>)
  27. * @param int $error Number of errors
  28. * @param string $errorcode Error string code
  29. * @param string $errorlabel Error string label
  30. * @return User Return user object identified by login/pass/entity into authentication array
  31. */
  32. function check_authentication($authentication, &$error, &$errorcode, &$errorlabel)
  33. {
  34. global $db, $conf, $langs;
  35. global $dolibarr_main_authentication, $dolibarr_auto_user;
  36. $fuser = new User($db);
  37. if (!$error && ($authentication['dolibarrkey'] != $conf->global->WEBSERVICES_KEY)) {
  38. $error++;
  39. $errorcode = 'BAD_VALUE_FOR_SECURITY_KEY';
  40. $errorlabel = 'Value provided into dolibarrkey entry field does not match security key defined in Webservice module setup';
  41. }
  42. if (!$error && !empty($authentication['entity']) && !is_numeric($authentication['entity'])) {
  43. $error++;
  44. $errorcode = 'BAD_PARAMETERS';
  45. $errorlabel = "The entity parameter must be empty (or filled with numeric id of instance if multicompany module is used).";
  46. }
  47. if (!$error) {
  48. $result = $fuser->fetch('', $authentication['login'], '', 0);
  49. if ($result < 0) {
  50. $error++;
  51. $errorcode = 'ERROR_FETCH_USER';
  52. $errorlabel = 'A technical error occurred during fetch of user';
  53. } elseif ($result == 0) {
  54. $error++;
  55. $errorcode = 'BAD_CREDENTIALS';
  56. $errorlabel = 'Bad value for login or password';
  57. }
  58. if (!$error && $fuser->statut == 0) {
  59. $error++;
  60. $errorcode = 'ERROR_USER_DISABLED';
  61. $errorlabel = 'This user has been locked or disabled';
  62. }
  63. // Validation of login
  64. if (!$error) {
  65. $fuser->getrights(); // Load permission of user
  66. // Authentication mode
  67. if (empty($dolibarr_main_authentication)) {
  68. $dolibarr_main_authentication = 'http,dolibarr';
  69. }
  70. // Authentication mode: forceuser
  71. if ($dolibarr_main_authentication == 'forceuser' && empty($dolibarr_auto_user)) {
  72. $dolibarr_auto_user = 'auto';
  73. }
  74. // Set authmode
  75. $authmode = explode(',', $dolibarr_main_authentication);
  76. include_once DOL_DOCUMENT_ROOT.'/core/lib/security2.lib.php';
  77. $login = checkLoginPassEntity($authentication['login'], $authentication['password'], $authentication['entity'], $authmode, 'ws');
  78. if (empty($login)) {
  79. $error++;
  80. $errorcode = 'BAD_CREDENTIALS';
  81. $errorlabel = 'Bad value for login or password';
  82. }
  83. }
  84. }
  85. return $fuser;
  86. }