api_access.class.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  3. * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. // Create the autoloader for Luracast
  19. require_once DOL_DOCUMENT_ROOT.'/includes/restler/framework/Luracast/Restler/AutoLoader.php';
  20. call_user_func(function () {
  21. $loader = Luracast\Restler\AutoLoader::instance();
  22. spl_autoload_register($loader);
  23. return $loader;
  24. });
  25. require_once DOL_DOCUMENT_ROOT.'/includes/restler/framework/Luracast/Restler/iAuthenticate.php';
  26. require_once DOL_DOCUMENT_ROOT.'/includes/restler/framework/Luracast/Restler/iUseAuthentication.php';
  27. require_once DOL_DOCUMENT_ROOT.'/includes/restler/framework/Luracast/Restler/Resources.php';
  28. require_once DOL_DOCUMENT_ROOT.'/includes/restler/framework/Luracast/Restler/Defaults.php';
  29. require_once DOL_DOCUMENT_ROOT.'/includes/restler/framework/Luracast/Restler/RestException.php';
  30. use \Luracast\Restler\iAuthenticate;
  31. use \Luracast\Restler\iUseAuthentication;
  32. use \Luracast\Restler\Resources;
  33. use \Luracast\Restler\Defaults;
  34. use \Luracast\Restler\RestException;
  35. /**
  36. * Dolibarr API access class
  37. *
  38. */
  39. class DolibarrApiAccess implements iAuthenticate
  40. {
  41. const REALM = 'Restricted Dolibarr API';
  42. /**
  43. * @var array $requires role required by API method user / external / admin
  44. */
  45. public static $requires = array('user','external','admin');
  46. /**
  47. * @var string $role user role
  48. */
  49. public static $role = 'user';
  50. /**
  51. * @var User $user Loggued user
  52. */
  53. public static $user = '';
  54. // @codingStandardsIgnoreStart
  55. /**
  56. * Check access
  57. *
  58. * @return bool
  59. * @throws RestException
  60. */
  61. public function __isAllowed()
  62. {
  63. global $db;
  64. $login = '';
  65. $stored_key = '';
  66. $userClass = Defaults::$userIdentifierClass;
  67. /*foreach ($_SERVER as $key => $val)
  68. {
  69. dol_syslog($key.' - '.$val);
  70. }*/
  71. // api key can be provided in url with parameter api_key=xxx or ni header with header DOLAPIKEY:xxx
  72. $api_key = '';
  73. if (isset($_GET['api_key']))
  74. {
  75. // TODO Add option to disable use of api key on url. Return errors if used.
  76. $api_key = $_GET['api_key']; // For backward compatibility
  77. }
  78. if (isset($_GET['DOLAPIKEY']))
  79. {
  80. // TODO Add option to disable use of api key on url. Return errors if used.
  81. $api_key = $_GET['DOLAPIKEY']; // With GET method
  82. }
  83. if (isset($_SERVER['HTTP_DOLAPIKEY']))
  84. {
  85. $api_key = $_SERVER['HTTP_DOLAPIKEY']; // With header method (recommanded)
  86. }
  87. if ($api_key)
  88. {
  89. $sql = "SELECT u.login, u.datec, u.api_key, ";
  90. $sql.= " u.tms as date_modification, u.entity";
  91. $sql.= " FROM ".MAIN_DB_PREFIX."user as u";
  92. $sql.= " WHERE u.api_key = '".$db->escape($api_key)."'";
  93. $result = $db->query($sql);
  94. if ($result)
  95. {
  96. if ($db->num_rows($result))
  97. {
  98. $obj = $db->fetch_object($result);
  99. $login = $obj->login;
  100. $stored_key = $obj->api_key;
  101. }
  102. }
  103. else {
  104. throw new RestException(503, 'Error when fetching user api_key :'.$db->error_msg);
  105. }
  106. if ($stored_key != $api_key) {
  107. $userClass::setCacheIdentifier($api_key);
  108. return false;
  109. }
  110. if (! $login)
  111. {
  112. throw new RestException(503, 'Error when searching logn user fro mapi key');
  113. }
  114. $fuser = new User($db);
  115. if(! $fuser->fetch('',$login)) {
  116. throw new RestException(503, 'Error when fetching user :'.$fuser->error);
  117. }
  118. $fuser->getrights();
  119. static::$user = $fuser;
  120. if($fuser->societe_id)
  121. static::$role = 'external';
  122. if($fuser->admin)
  123. static::$role = 'admin';
  124. }
  125. else
  126. {
  127. throw new RestException(401, "Failed to login to API. No parameter 'DOLAPIKEY' on HTTP header (neither in URL).");
  128. }
  129. $userClass::setCacheIdentifier(static::$role);
  130. Resources::$accessControlFunction = 'DolibarrApiAccess::verifyAccess';
  131. $requirefortest = static::$requires;
  132. if (! is_array($requirefortest)) $requirefortest=explode(',',$requirefortest);
  133. return in_array(static::$role, (array) $requirefortest) || static::$role == 'admin';
  134. }
  135. /**
  136. * @return string string to be used with WWW-Authenticate header
  137. * @example Basic
  138. * @example Digest
  139. * @example OAuth
  140. */
  141. public function __getWWWAuthenticateString()
  142. {
  143. return '';
  144. }
  145. // @codingStandardsIgnoreEnd
  146. /**
  147. * Verify access
  148. *
  149. * @param array $m Properties of method
  150. *
  151. * @access private
  152. * @return bool
  153. */
  154. public static function verifyAccess(array $m)
  155. {
  156. $requires = isset($m['class']['DolibarrApiAccess']['properties']['requires'])
  157. ? $m['class']['DolibarrApiAccess']['properties']['requires']
  158. : false;
  159. return $requires
  160. ? static::$role == 'admin' || in_array(static::$role, (array) $requires)
  161. : true;
  162. }
  163. }