95 global $conf, $db, $langs, $mysoc, $user;
100 $userClass = Defaults::$userIdentifierClass;
109 if (isset($_GET[
'api_key'])) {
111 $api_key = $_GET[
'api_key'];
113 if (isset($_GET[
'DOLAPIKEY'])) {
115 $api_key = $_GET[
'DOLAPIKEY'];
117 if (isset($_SERVER[
'HTTP_DOLAPIKEY'])) {
118 $api_key = $_SERVER[
'HTTP_DOLAPIKEY'];
123 if (preg_match(
'/^dolcrypt:/i', $api_key)) {
124 throw new RestException(503,
'Bad value for the API key. An API key should not start with dolcrypt:');
130 $sql =
"SELECT u.login, u.datec, u.api_key,";
131 $sql .=
" u.tms as date_modification, u.entity";
132 $sql .=
" FROM ".MAIN_DB_PREFIX.
"user as u";
133 $sql .=
" WHERE u.api_key = '".$this->db->escape($api_key).
"' OR u.api_key = '".$this->db->escape(
dolEncrypt($api_key,
'',
'',
'dolibarr')).
"'";
135 $result = $this->db->query($sql);
137 $nbrows = $this->db->num_rows($result);
139 $obj = $this->db->fetch_object($result);
140 $login = $obj->login;
142 $userentity = $obj->entity;
144 if (!defined(
"DOLENTITY") && $conf->entity != ($obj->entity ? $obj->entity : 1)) {
145 $conf->entity = ($obj->entity ? $obj->entity : 1);
147 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);
148 $conf->setValues($this->db);
152 require_once DOL_DOCUMENT_ROOT.
'/societe/class/societe.class.php';
155 $fmysoc->setMysoc($conf);
158 if ($fmysoc->country_code ==
'DE' && !isset($conf->global->MAIN_INVERT_SENDER_RECIPIENT)) {
160 $conf->global->MAIN_INVERT_SENDER_RECIPIENT = 1;
162 if ($fmysoc->country_code ==
'FR' && !isset($conf->global->INVOICE_CATEGORY_OF_OPERATION)) {
164 $conf->global->INVOICE_CATEGORY_OF_OPERATION = 1;
166 if ($fmysoc->country_code ==
'FR' && !isset($conf->global->INVOICE_DISABLE_REPLACEMENT)) {
170 $conf->global->INVOICE_DISABLE_REPLACEMENT = 1;
172 if ($fmysoc->country_code ==
'GR' && !isset($conf->global->INVOICE_DISABLE_REPLACEMENT)) {
174 $conf->global->INVOICE_DISABLE_REPLACEMENT = 1;
176 if ($fmysoc->country_code ==
'GR' && !isset($conf->global->INVOICE_DISABLE_DEPOSIT)) {
178 $conf->global->INVOICE_DISABLE_DEPOSIT = 1;
181 if (($fmysoc->localtax1_assuj || $fmysoc->localtax2_assuj) && !isset($conf->global->MAIN_NO_INPUT_PRICE_WITH_TAX)) {
184 $conf->global->MAIN_NO_INPUT_PRICE_WITH_TAX = 1;
190 $langcode = (empty($conf->global->MAIN_LANG_DEFAULT) ?
'auto' : $conf->global->MAIN_LANG_DEFAULT);
191 if (!empty($user->conf->MAIN_LANG_DEFAULT)) {
192 $langcode = $user->conf->MAIN_LANG_DEFAULT;
194 if ($langs->getDefaultLang() != $langcode) {
195 $langs->setDefaultLang($langcode);
196 $langs->tab_translate = array();
197 $langs->loadLangs(array(
'main'));
200 } elseif ($nbrows > 1) {
201 throw new RestException(503,
'Error when fetching user api_key : More than 1 user with this apikey');
204 throw new RestException(503,
'Error when fetching user api_key :'.$this->db->error_msg);
207 if ($login && $stored_key != $api_key) {
208 $userClass::setCacheIdentifier($api_key);
212 $genericmessageerroruser =
'Error user not valid (not found with api key or bad status or bad validity dates) (conf->entity='.$conf->entity.
')';
215 dol_syslog(
"functions_isallowed::check_user_api_key Authentication KO for api key: Error when searching login user from api key", LOG_NOTICE);
217 throw new RestException(401, $genericmessageerroruser);
220 $fuser =
new User($this->db);
221 $result = $fuser->fetch(
'', $login,
'', 0, (empty($userentity) ? -1 : $conf->entity));
223 dol_syslog(
"functions_isallowed::check_user_api_key Authentication KO for '".$login.
"': Failed to fetch on entity", LOG_NOTICE);
225 throw new RestException(401, $genericmessageerroruser);
229 if ($fuser->status != $fuser::STATUS_ENABLED) {
231 dol_syslog(
"functions_isallowed::check_user_api_key Authentication KO for '".$login.
"': The user has been disabled", LOG_NOTICE);
233 throw new RestException(401, $genericmessageerroruser);
237 if (($fuser->flagdelsessionsbefore && !empty($_SESSION[
"dol_logindate"]) && $fuser->flagdelsessionsbefore > $_SESSION[
"dol_logindate"])) {
239 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.");
241 throw new RestException(401, $genericmessageerroruser);
245 if ($fuser->isNotIntoValidityDateRange()) {
247 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());
249 throw new RestException(401, $genericmessageerroruser);
255 include DOL_DOCUMENT_ROOT.
'/core/lib/admin.lib.php';
264 static::$user = $fuser;
270 static::$role =
'external';
274 static::$role =
'admin';
277 throw new RestException(401,
"Failed to login to API. No parameter 'HTTP_DOLAPIKEY' on HTTP header (and no parameter DOLAPIKEY in URL).");
280 $userClass::setCacheIdentifier(static::$role);
281 Resources::$accessControlFunction =
'DolibarrApiAccess::verifyAccess';
282 $requirefortest = static::$requires;
283 if (!is_array($requirefortest)) {
284 $requirefortest = explode(
',', $requirefortest);
286 return in_array(static::$role, (array) $requirefortest) || static::$role ==
'admin';