api_access.class.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /* Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
  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. // Create the autoloader for Luracast
  18. require_once DOL_DOCUMENT_ROOT.'/includes/restler/framework/Luracast/Restler/AutoLoader.php';
  19. call_user_func(function () {
  20. $loader = Luracast\Restler\AutoLoader::instance();
  21. spl_autoload_register($loader);
  22. return $loader;
  23. });
  24. require_once DOL_DOCUMENT_ROOT.'/includes/restler/framework/Luracast/Restler/iAuthenticate.php';
  25. require_once DOL_DOCUMENT_ROOT.'/includes/restler/framework/Luracast/Restler/iUseAuthentication.php';
  26. require_once DOL_DOCUMENT_ROOT.'/includes/restler/framework/Luracast/Restler/Resources.php';
  27. require_once DOL_DOCUMENT_ROOT.'/includes/restler/framework/Luracast/Restler/Defaults.php';
  28. require_once DOL_DOCUMENT_ROOT.'/includes/restler/framework/Luracast/Restler/RestException.php';
  29. use \Luracast\Restler\iAuthenticate;
  30. use \Luracast\Restler\iUseAuthentication;
  31. use \Luracast\Restler\Resources;
  32. use \Luracast\Restler\Defaults;
  33. use \Luracast\Restler\RestException;
  34. /**
  35. * Dolibarr API access class
  36. *
  37. */
  38. class DolibarrApiAccess implements iAuthenticate
  39. {
  40. const REALM = 'Restricted Dolibarr API';
  41. /**
  42. * @var array $requires role required by API method user / external / admin
  43. */
  44. public static $requires = array('user','external','admin');
  45. /**
  46. * @var string $role user role
  47. */
  48. public static $role = 'user';
  49. /**
  50. * @var User $user Loggued user
  51. */
  52. public static $user = '';
  53. // @codingStandardsIgnoreStart
  54. /**
  55. * Check access
  56. *
  57. * @return bool
  58. * @throws RestException
  59. */
  60. public function __isAllowed()
  61. {
  62. global $db;
  63. $stored_key = '';
  64. $userClass = Defaults::$userIdentifierClass;
  65. if (isset($_GET['api_key']))
  66. {
  67. $sql = "SELECT u.login, u.datec, u.api_key, ";
  68. $sql.= " u.tms as date_modification, u.entity";
  69. $sql.= " FROM ".MAIN_DB_PREFIX."user as u";
  70. $sql.= " WHERE u.api_key = '".$db->escape($_GET['api_key'])."'";
  71. $result = $db->query($sql);
  72. if ($result)
  73. {
  74. if ($db->num_rows($result))
  75. {
  76. $obj = $db->fetch_object($result);
  77. $login = $obj->login;
  78. $stored_key = $obj->api_key;
  79. }
  80. }
  81. else {
  82. throw new RestException(503, 'Error when fetching user api_key :'.$db->error_msg);
  83. }
  84. if ( $stored_key != $_GET['api_key']) {
  85. $userClass::setCacheIdentifier($_GET['api_key']);
  86. return false;
  87. }
  88. $fuser = new User($db);
  89. if(! $fuser->fetch('',$login)) {
  90. throw new RestException(503, 'Error when fetching user :'.$fuser->error);
  91. }
  92. $fuser->getrights();
  93. static::$user = $fuser;
  94. if($fuser->societe_id)
  95. static::$role = 'external';
  96. if($fuser->admin)
  97. static::$role = 'admin';
  98. }
  99. else
  100. {
  101. throw new RestException(401, "Failed to login to API. No parameter 'api_key' provided");
  102. }
  103. $userClass::setCacheIdentifier(static::$role);
  104. Resources::$accessControlFunction = 'DolibarrApiAccess::verifyAccess';
  105. $requirefortest = static::$requires;
  106. if (! is_array($requirefortest)) $requirefortest=explode(',',$requirefortest);
  107. return in_array(static::$role, (array) $requirefortest) || static::$role == 'admin';
  108. }
  109. /**
  110. * @return string string to be used with WWW-Authenticate header
  111. * @example Basic
  112. * @example Digest
  113. * @example OAuth
  114. */
  115. public function __getWWWAuthenticateString()
  116. {
  117. return '';
  118. }
  119. // @codingStandardsIgnoreEnd
  120. /**
  121. * Verify access
  122. *
  123. * @param array $m Properties of method
  124. *
  125. * @access private
  126. * @return bool
  127. */
  128. public static function verifyAccess(array $m)
  129. {
  130. $requires = isset($m['class']['DolibarrApiAccess']['properties']['requires'])
  131. ? $m['class']['DolibarrApiAccess']['properties']['requires']
  132. : false;
  133. return $requires
  134. ? static::$role == 'admin' || in_array(static::$role, (array) $requires)
  135. : true;
  136. }
  137. }