21 require_once DOL_DOCUMENT_ROOT.
'/includes/restler/framework/Luracast/Restler/AutoLoader.php';
22 call_user_func(
function () {
23 $loader = Luracast\Restler\AutoLoader::instance();
24 spl_autoload_register($loader);
28 require_once DOL_DOCUMENT_ROOT.
'/includes/restler/framework/Luracast/Restler/iAuthenticate.php';
29 require_once DOL_DOCUMENT_ROOT.
'/includes/restler/framework/Luracast/Restler/iUseAuthentication.php';
30 require_once DOL_DOCUMENT_ROOT.
'/includes/restler/framework/Luracast/Restler/Resources.php';
31 require_once DOL_DOCUMENT_ROOT.
'/includes/restler/framework/Luracast/Restler/Defaults.php';
32 require_once DOL_DOCUMENT_ROOT.
'/includes/restler/framework/Luracast/Restler/RestException.php';
33 use \Luracast\Restler\iAuthenticate;
34 use \Luracast\Restler\iUseAuthentication;
35 use \Luracast\Restler\Resources;
36 use \Luracast\Restler\Defaults;
37 use \Luracast\Restler\RestException;
45 const REALM =
'Restricted Dolibarr API';
50 public static $requires = array(
'user',
'external',
'admin');
55 public static $role =
'user';
60 public static $user =
'';
84 global $conf, $db, $user;
89 $userClass = Defaults::$userIdentifierClass;
98 if (isset($_GET[
'api_key'])) {
100 $api_key = $_GET[
'api_key'];
102 if (isset($_GET[
'DOLAPIKEY'])) {
104 $api_key = $_GET[
'DOLAPIKEY'];
106 if (isset($_SERVER[
'HTTP_DOLAPIKEY'])) {
107 $api_key = $_SERVER[
'HTTP_DOLAPIKEY'];
113 $sql =
"SELECT u.login, u.datec, u.api_key, ";
114 $sql .=
" u.tms as date_modification, u.entity";
115 $sql .=
" FROM ".MAIN_DB_PREFIX.
"user as u";
116 $sql .=
" WHERE u.api_key = '".$this->db->escape($api_key).
"' OR u.api_key = '".$this->
db->escape(
dolEncrypt($api_key,
'',
'',
'dolibarr')).
"'";
118 $result = $this->
db->query($sql);
120 $nbrows = $this->
db->num_rows($result);
122 $obj = $this->
db->fetch_object($result);
123 $login = $obj->login;
125 $userentity = $obj->entity;
127 if (!defined(
"DOLENTITY") && $conf->entity != ($obj->entity ? $obj->entity : 1)) {
128 $conf->entity = ($obj->entity ? $obj->entity : 1);
130 dol_syslog(
"Entity was not set on http header with HTTP_DOLAPIENTITY (recommanded for performance purpose), so we switch now on entity of user (".$conf->entity.
") and we have to reload configuration.", LOG_WARNING);
131 $conf->setValues($this->
db);
133 } elseif ($nbrows > 1) {
134 throw new RestException(503,
'Error when fetching user api_key : More than 1 user with this apikey');
137 throw new RestException(503,
'Error when fetching user api_key :'.$this->
db->error_msg);
140 if ($stored_key != $api_key) {
141 $userClass::setCacheIdentifier($api_key);
146 throw new RestException(503,
'Error when searching login user from api key');
148 $fuser =
new User($this->
db);
149 $result = $fuser->fetch(
'', $login,
'', 0, (empty($userentity) ? -1 : $conf->entity));
151 throw new RestException(503,
'Error when fetching user :'.$fuser->error.
' (conf->entity='.$conf->entity.
')');
153 if ($fuser->statut == 0) {
154 throw new RestException(503,
'Error when fetching user. This user has been locked or disabled');
160 static::$user = $fuser;
166 static::$role =
'external';
170 static::$role =
'admin';
173 throw new RestException(401,
"Failed to login to API. No parameter 'HTTP_DOLAPIKEY' on HTTP header (and no parameter DOLAPIKEY in URL).");
176 $userClass::setCacheIdentifier(static::$role);
177 Resources::$accessControlFunction =
'DolibarrApiAccess::verifyAccess';
178 $requirefortest = static::$requires;
179 if (!is_array($requirefortest)) {
180 $requirefortest = explode(
',', $requirefortest);
182 return in_array(static::$role, (array) $requirefortest) || static::$role ==
'admin';
205 $requires = isset($m[
'class'][
'DolibarrApiAccess'][
'properties'][
'requires'])
206 ? $m[
'class'][
'DolibarrApiAccess'][
'properties'][
'requires']
211 ? static::$role ==
'admin' || in_array(static::$role, (array) $requires)