functions_mc.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /* Copyright (C) 2014-2022 Regis Houssin <regis.houssin@inodbox.com>
  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 <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file multicompany/core/login/functions_mc.php
  19. * \ingroup multicompany
  20. * \brief Authentication functions for Multicompany mode when combobox in login page is disabled
  21. */
  22. /**
  23. * Check validity of user/password/entity
  24. * If test is ko, reason must be filled into $_SESSION["dol_loginmesg"]
  25. *
  26. * @param string $usertotest Login
  27. * @param string $passwordtotest Password
  28. * @param int $entitytotest Number of instance (always 1 if module multicompany not enabled)
  29. * @return string Login if OK, '' if KO
  30. */
  31. function check_user_password_mc($usertotest, $passwordtotest, $entitytotest=1)
  32. {
  33. global $db,$conf,$langs;
  34. global $mc;
  35. dol_syslog("functions_mc::check_user_password_mc usertotest=".$usertotest);
  36. $login='';
  37. if (!empty($conf->multicompany->enabled)) {
  38. $langs->loadLangs(array('main','errors','multicompany@multicompany'));
  39. if (!empty($conf->global->MULTICOMPANY_HIDE_LOGIN_COMBOBOX)) {
  40. if (!empty($usertotest)) {
  41. // If test username/password asked, we define $test=false and $login var if ok, set $_SESSION["dol_loginmesg"] if ko
  42. $table = MAIN_DB_PREFIX."user";
  43. $usernamecol = 'login';
  44. $sql ='SELECT rowid, entity, pass, pass_crypted';
  45. $sql.=' FROM '.$table;
  46. $sql.=' WHERE '.$usernamecol." = '".$db->escape($usertotest)."'";
  47. $sql.=' AND statut = 1';
  48. dol_syslog("functions_mc::check_user_password_mc sql=" . $sql);
  49. $resql=$db->query($sql);
  50. if (!empty($resql)) {
  51. $obj=$db->fetch_object($resql);
  52. if (!empty($obj)) {
  53. $passclear=$obj->pass;
  54. $passcrypted=$obj->pass_crypted;
  55. $passtyped=$passwordtotest;
  56. $passok=false;
  57. // Check crypted password
  58. $cryptType='';
  59. if (!empty($conf->global->DATABASE_PWD_ENCRYPTED)) {
  60. $cryptType=$conf->global->DATABASE_PWD_ENCRYPTED;
  61. }
  62. // By default, we used MD5
  63. if (! in_array($cryptType,array('md5'))) {
  64. $cryptType='md5';
  65. }
  66. // Check crypted password according to crypt algorithm
  67. if ($cryptType == 'md5') {
  68. if (dol_verifyHash($passtyped, $passcrypted)) {
  69. $passok=true;
  70. dol_syslog("functions_mc::check_user_password_mc Authentification ok - " . $cryptType . " of pass is ok");
  71. }
  72. }
  73. // For compatibility with old versions
  74. if (empty($passok)) {
  75. if ((empty($passcrypted) || !empty($passtyped)) && (!empty($passclear) && ($passtyped == $passclear))) {
  76. $passok=true;
  77. dol_syslog("functions_mc::check_user_password_mc Authentification ok - found pass in database");
  78. }
  79. }
  80. if (!empty($passok) && !empty($obj->entity)) {
  81. global $entitytotest;
  82. $entitytotest = $obj->entity;
  83. if (!empty($conf->global->MULTICOMPANY_TRANSVERSE_MODE)) {
  84. $sql = "SELECT uu.entity";
  85. $sql.= " FROM " . MAIN_DB_PREFIX . "usergroup_user as uu";
  86. $sql.= ", " . MAIN_DB_PREFIX . "entity as e";
  87. $sql.= " WHERE uu.entity = e.rowid AND e.visible < 2"; // Remove template of entity
  88. $sql.= " AND uu.fk_user = " . $obj->rowid;
  89. dol_syslog("functions_mc::check_user_password_mc sql=" . $sql, LOG_DEBUG);
  90. $result = $db->query($sql);
  91. if (!empty($result)) {
  92. while($array = $db->fetch_array($result)) { // user allowed if at least in one group
  93. $entitytotest = $array['entity'];
  94. break; // stop in first entity
  95. }
  96. }
  97. }
  98. $ret=$mc->switchEntity($entitytotest, $obj->rowid);
  99. if ($ret < 0) {
  100. $passok=false;
  101. }
  102. }
  103. // Password ok ?
  104. if (!empty($passok)) {
  105. $login=$usertotest;
  106. } else {
  107. dol_syslog("functions_mc::check_user_password_mc Authentification ko bad password pour '".$usertotest."'", LOG_ERR);
  108. $_SESSION["dol_loginmesg"]=$langs->trans("ErrorBadLoginPassword");
  109. }
  110. } else {
  111. dol_syslog("functions_mc::check_user_password_mc Authentification ko user not found for '".$usertotest."'", LOG_ERR);
  112. $_SESSION["dol_loginmesg"]=$langs->trans("ErrorBadLoginPassword");
  113. }
  114. } else {
  115. dol_syslog("functions_mc::check_user_password_mc Authentification ko db error for '".$usertotest."' error=".$db->lasterror(), LOG_ERR);
  116. $_SESSION["dol_loginmesg"]=$db->lasterror();
  117. }
  118. }
  119. } else {
  120. dol_syslog("functions_mc::check_user_password_mc Authentification ko, the drop-down list of entities on the login page must be hidden", LOG_ERR);
  121. $_SESSION["dol_loginmesg"]=$langs->trans("ErrorDropDownListOfEntitiesMustBeHidden");
  122. }
  123. }
  124. return $login;
  125. }