94 global
$conf, $db, $langs, $user;
99 $userClass = Defaults::$userIdentifierClass;
108 if (isset($_GET[
'api_key'])) {
110 $api_key = $_GET[
'api_key'];
112 if (isset($_GET[
'DOLAPIKEY'])) {
114 $api_key = $_GET[
'DOLAPIKEY'];
120 if (isset($_SERVER[
'HTTP_DOLAPIKEY'])) {
121 $api_key = $_SERVER[
'HTTP_DOLAPIKEY'];
122 } elseif (empty($api_key)) {
123 $headers = getallheaders();
124 $api_key = preg_replace(
'/^Bearer\s+/i',
'', empty($headers[
'Authorization']) ?
'' : $headers[
'Authorization']);
129 if (preg_match(
'/^dolcrypt:/i', $api_key)) {
130 throw new RestException(503,
'Bad value for the API key. An API key should not start with dolcrypt:');
136 $sql =
"SELECT u.login, u.datec, u.api_key,";
137 $sql .=
" u.tms as date_modification, u.entity";
138 $sql .=
" FROM ".MAIN_DB_PREFIX.
"user as u";
139 $sql .=
" WHERE u.api_key = '".$this->db->escape($api_key).
"' OR u.api_key = '".$this->db->escape(
dolEncrypt($api_key,
'',
'',
'dolibarr')).
"'";
141 $result = $this->db->query($sql);
143 $nbrows = $this->db->num_rows($result);
145 $obj = $this->db->fetch_object($result);
146 $login = $obj->login;
148 $userentity = $obj->entity;
150 if (!defined(
"DOLENTITY") &&
$conf->entity != ($obj->entity ? $obj->entity : 1)) {
151 $conf->entity = ($obj->entity ? $obj->entity : 1);
153 dol_syslog(
"Entity was not set on http header with HTTP_DOLAPIENTITY (recommended for performance purpose), so we switch now on entity of user (".
$conf->entity.
") and we have to reload configuration.", LOG_WARNING);
154 $conf->setValues($this->db);
158 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
161 $fmysoc->setMysoc(
$conf);
164 if ($fmysoc->country_code ==
'DE' && !isset(
$conf->global->MAIN_INVERT_SENDER_RECIPIENT)) {
166 $conf->global->MAIN_INVERT_SENDER_RECIPIENT = 1;
168 if ($fmysoc->country_code ==
'FR' && !isset(
$conf->global->INVOICE_CATEGORY_OF_OPERATION)) {
170 $conf->global->INVOICE_CATEGORY_OF_OPERATION = 1;
172 if ($fmysoc->country_code ==
'FR' && !isset(
$conf->global->INVOICE_DISABLE_REPLACEMENT)) {
176 $conf->global->INVOICE_DISABLE_REPLACEMENT = 1;
178 if ($fmysoc->country_code ==
'GR' && !isset(
$conf->global->INVOICE_DISABLE_REPLACEMENT)) {
180 $conf->global->INVOICE_DISABLE_REPLACEMENT = 1;
182 if ($fmysoc->country_code ==
'GR' && !isset(
$conf->global->INVOICE_DISABLE_DEPOSIT)) {
184 $conf->global->INVOICE_DISABLE_DEPOSIT = 1;
187 if (($fmysoc->localtax1_assuj || $fmysoc->localtax2_assuj) && !isset(
$conf->global->MAIN_NO_INPUT_PRICE_WITH_TAX)) {
190 $conf->global->MAIN_NO_INPUT_PRICE_WITH_TAX = 1;
197 $langcode = (empty(
$conf->global->MAIN_LANG_DEFAULT) ?
'auto' :
$conf->global->MAIN_LANG_DEFAULT);
198 if (!empty($user->conf->MAIN_LANG_DEFAULT)) {
199 $langcode = $user->conf->MAIN_LANG_DEFAULT;
201 if ($langs->getDefaultLang() != $langcode) {
202 $langs->setDefaultLang($langcode);
203 $langs->tab_translate = array();
204 $langs->loadLangs(array(
'main'));
207 } elseif ($nbrows > 1) {
208 throw new RestException(503,
'Error when fetching user api_key : More than 1 user with this apikey');
211 throw new RestException(503,
'Error when fetching user api_key :'.$this->db->error);
214 if ($login && $stored_key != $api_key) {
215 $userClass::setCacheIdentifier($api_key);
219 $genericmessageerroruser =
'Error user not valid (not found with api key or bad status or bad validity dates) (conf->entity='.$conf->entity.
')';
222 dol_syslog(
"functions_isallowed::check_user_api_key Authentication KO for api key: Error when searching login user from api key", LOG_NOTICE);
224 throw new RestException(401, $genericmessageerroruser);
227 $fuser =
new User($this->db);
228 $result = $fuser->fetch(0, $login,
'', 0, (empty($userentity) ? -1 :
$conf->entity));
230 dol_syslog(
"functions_isallowed::check_user_api_key Authentication KO for '".$login.
"': Failed to fetch on entity", LOG_NOTICE);
232 throw new RestException(401, $genericmessageerroruser);
236 if ($fuser->status != $fuser::STATUS_ENABLED) {
238 dol_syslog(
"functions_isallowed::check_user_api_key Authentication KO for '".$login.
"': The user has been disabled", LOG_NOTICE);
240 throw new RestException(401, $genericmessageerroruser);
244 if (($fuser->flagdelsessionsbefore && !empty($_SESSION[
"dol_logindate"]) && $fuser->flagdelsessionsbefore > $_SESSION[
"dol_logindate"])) {
246 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.");
248 throw new RestException(401, $genericmessageerroruser);
252 if ($fuser->isNotIntoValidityDateRange()) {
254 dol_syslog(
"functions_isallowed::check_user_api_key Authentication KO for '".$login.
"': The user login has a validity between [".$fuser->datestartvalidity.
" and ".$fuser->dateendvalidity.
"], current date is ".
dol_now());
256 throw new RestException(401, $genericmessageerroruser);
262 include DOL_DOCUMENT_ROOT.
'/core/lib/admin.lib.php';
268 $fuser->loadRights();
271 static::$user = $fuser;
277 static::$role =
'external';
281 static::$role =
'admin';
284 throw new RestException(401,
"Failed to login to API. Neither parameter 'HTTP_DOLAPIKEY' nor 'Authentication: Bearer' found on HTTP header (and no parameter DOLAPIKEY in URL).");
287 $userClass::setCacheIdentifier(static::$role);
288 Resources::$accessControlFunction =
'DolibarrApiAccess::verifyAccess';
289 $requirefortest = static::$requires;
290 if (!is_array($requirefortest)) {
291 $requirefortest = explode(
',', $requirefortest);
293 return in_array(static::$role, (array) $requirefortest) || static::$role ==
'admin';