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'];
109 if (preg_match(
'/^dolcrypt:/i', $api_key)) {
110 throw new RestException(503,
'Bad value for the API key. An API key should not start with dolcrypt:');
116 $sql =
"SELECT u.login, u.datec, u.api_key,";
117 $sql .=
" u.tms as date_modification, u.entity";
118 $sql .=
" FROM ".MAIN_DB_PREFIX.
"user as u";
119 $sql .=
" WHERE u.api_key = '".$this->db->escape($api_key).
"' OR u.api_key = '".$this->db->escape(
dolEncrypt($api_key,
'',
'',
'dolibarr')).
"'";
121 $result = $this->db->query($sql);
123 $nbrows = $this->db->num_rows($result);
125 $obj = $this->db->fetch_object($result);
126 $login = $obj->login;
128 $userentity = $obj->entity;
130 if (!defined(
"DOLENTITY") && $conf->entity != ($obj->entity ? $obj->entity : 1)) {
131 $conf->entity = ($obj->entity ? $obj->entity : 1);
133 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);
134 $conf->setValues($this->db);
136 } elseif ($nbrows > 1) {
137 throw new RestException(503,
'Error when fetching user api_key : More than 1 user with this apikey');
140 throw new RestException(503,
'Error when fetching user api_key :'.$this->db->error_msg);
143 if ($login && $stored_key != $api_key) {
144 $userClass::setCacheIdentifier($api_key);
148 $genericmessageerroruser =
'Error user not valid (not found with api key or bad status or bad validity dates) (conf->entity='.$conf->entity.
')';
151 dol_syslog(
"functions_isallowed::check_user_api_key Authentication KO for api key: Error when searching login user from api key", LOG_NOTICE);
153 throw new RestException(401, $genericmessageerroruser);
156 $fuser =
new User($this->db);
157 $result = $fuser->fetch(
'', $login,
'', 0, (empty($userentity) ? -1 : $conf->entity));
159 dol_syslog(
"functions_isallowed::check_user_api_key Authentication KO for '".$login.
"': Failed to fetch on entity", LOG_NOTICE);
161 throw new RestException(401, $genericmessageerroruser);
165 if ($fuser->statut != $fuser::STATUS_ENABLED) {
167 dol_syslog(
"functions_isallowed::check_user_api_key Authentication KO for '".$login.
"': The user has been disabled", LOG_NOTICE);
169 throw new RestException(401, $genericmessageerroruser);
173 if (($fuser->flagdelsessionsbefore && !empty($_SESSION[
"dol_logindate"]) && $fuser->flagdelsessionsbefore > $_SESSION[
"dol_logindate"])) {
175 dol_syslog(
"functions_isallowed::check_user_api_key Authentication KO for '".$login.
"': The user has a date for session invalidation = ".$fuser->flagdelsessionsbefore.
" and a session date = ".$_SESSION[
"dol_logindate"].
". We must invalidate its sessions.");
177 throw new RestException(401, $genericmessageerroruser);
181 if ($fuser->isNotIntoValidityDateRange()) {
183 dol_syslog(
"functions_isallowed::check_user_api_key Authentication KO for '".$login.
"': The user login has a validity between [".$fuser->datestartvalidity.
" and ".$fuser->dateendvalidity.
"], curren date is ".
dol_now());
185 throw new RestException(401, $genericmessageerroruser);
192 static::$user = $fuser;
198 static::$role =
'external';
202 static::$role =
'admin';
205 throw new RestException(401,
"Failed to login to API. No parameter 'HTTP_DOLAPIKEY' on HTTP header (and no parameter DOLAPIKEY in URL).");
208 $userClass::setCacheIdentifier(static::$role);
209 Resources::$accessControlFunction =
'DolibarrApiAccess::verifyAccess';
210 $requirefortest = static::$requires;
211 if (!is_array($requirefortest)) {
212 $requirefortest = explode(
',', $requirefortest);
214 return in_array(static::$role, (array) $requirefortest) || static::$role ==
'admin';