90 global $conf, $db, $user;
95 $userClass = Defaults::$userIdentifierClass;
104 if (isset($_GET[
'api_key'])) {
106 $api_key = $_GET[
'api_key'];
108 if (isset($_GET[
'DOLAPIKEY'])) {
110 $api_key = $_GET[
'DOLAPIKEY'];
112 if (isset($_SERVER[
'HTTP_DOLAPIKEY'])) {
113 $api_key = $_SERVER[
'HTTP_DOLAPIKEY'];
115 if (preg_match(
'/^dolcrypt:/i', $api_key)) {
116 throw new RestException(503,
'Bad value for the API key. An API key should not start with dolcrypt:');
122 $sql =
"SELECT u.login, u.datec, u.api_key,";
123 $sql .=
" u.tms as date_modification, u.entity";
124 $sql .=
" FROM ".MAIN_DB_PREFIX.
"user as u";
125 $sql .=
" WHERE u.api_key = '".$this->db->escape($api_key).
"' OR u.api_key = '".$this->db->escape(
dolEncrypt($api_key,
'',
'',
'dolibarr')).
"'";
127 $result = $this->db->query($sql);
129 $nbrows = $this->db->num_rows($result);
131 $obj = $this->db->fetch_object($result);
132 $login = $obj->login;
134 $userentity = $obj->entity;
136 if (!defined(
"DOLENTITY") && $conf->entity != ($obj->entity ? $obj->entity : 1)) {
137 $conf->entity = ($obj->entity ? $obj->entity : 1);
139 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);
140 $conf->setValues($this->db);
142 } elseif ($nbrows > 1) {
143 throw new RestException(503,
'Error when fetching user api_key : More than 1 user with this apikey');
146 throw new RestException(503,
'Error when fetching user api_key :'.$this->db->error_msg);
149 if ($login && $stored_key != $api_key) {
150 $userClass::setCacheIdentifier($api_key);
154 $genericmessageerroruser =
'Error user not valid (not found with api key or bad status or bad validity dates) (conf->entity='.$conf->entity.
')';
157 dol_syslog(
"functions_isallowed::check_user_api_key Authentication KO for api key: Error when searching login user from api key", LOG_NOTICE);
159 throw new RestException(401, $genericmessageerroruser);
162 $fuser =
new User($this->db);
163 $result = $fuser->fetch(
'', $login,
'', 0, (empty($userentity) ? -1 : $conf->entity));
165 dol_syslog(
"functions_isallowed::check_user_api_key Authentication KO for '".$login.
"': Failed to fetch on entity", LOG_NOTICE);
167 throw new RestException(401, $genericmessageerroruser);
171 if ($fuser->statut != $fuser::STATUS_ENABLED) {
173 dol_syslog(
"functions_isallowed::check_user_api_key Authentication KO for '".$login.
"': The user has been disabled", LOG_NOTICE);
175 throw new RestException(401, $genericmessageerroruser);
179 if (($fuser->flagdelsessionsbefore && !empty($_SESSION[
"dol_logindate"]) && $fuser->flagdelsessionsbefore > $_SESSION[
"dol_logindate"])) {
181 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.");
183 throw new RestException(401, $genericmessageerroruser);
187 if ($fuser->isNotIntoValidityDateRange()) {
189 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());
191 throw new RestException(401, $genericmessageerroruser);
198 static::$user = $fuser;
204 static::$role =
'external';
208 static::$role =
'admin';
211 throw new RestException(401,
"Failed to login to API. No parameter 'HTTP_DOLAPIKEY' on HTTP header (and no parameter DOLAPIKEY in URL).");
214 $userClass::setCacheIdentifier(static::$role);
215 Resources::$accessControlFunction =
'DolibarrApiAccess::verifyAccess';
216 $requirefortest = static::$requires;
217 if (!is_array($requirefortest)) {
218 $requirefortest = explode(
',', $requirefortest);
220 return in_array(static::$role, (array) $requirefortest) || static::$role ==
'admin';