87 public function index($login, $password, $entity =
'', $reset = 0)
89 global $conf, $dolibarr_main_authentication, $dolibarr_auto_user;
92 if (!empty($conf->global->API_DISABLE_LOGIN_API)) {
93 dol_syslog(
"Warning: A try to use the login API has been done while the login API is disabled. You must generate or get the token from the backoffice.", LOG_WARNING);
94 throw new RestException(403,
"Error, the login API has been disabled for security purpose. You must generate or get the token from the backoffice.");
98 if (empty($dolibarr_main_authentication)) {
99 $dolibarr_main_authentication =
'dolibarr';
103 if ($dolibarr_main_authentication ==
'forceuser') {
104 if (empty($dolibarr_auto_user)) {
105 $dolibarr_auto_user =
'auto';
107 if ($dolibarr_auto_user != $login) {
108 dol_syslog(
"Warning: your instance is set to use the automatic forced login '".$dolibarr_auto_user.
"' that is not the requested login. API usage is forbidden in this mode.");
109 throw new RestException(403,
"Your instance is set to use the automatic login '".$dolibarr_auto_user.
"' that is not the requested login. API usage is forbidden in this mode.");
114 $authmode = explode(
',', $dolibarr_main_authentication);
116 if ($entity !=
'' && !is_numeric($entity)) {
117 throw new RestException(403,
"Bad value for entity, must be the numeric ID of company.");
123 include_once DOL_DOCUMENT_ROOT.
'/core/lib/security2.lib.php';
125 if ($login ===
'--bad-login-validity--') {
129 throw new RestException(403,
'Access denied');
132 $token =
'failedtogenerateorgettoken';
134 $tmpuser =
new User($this->db);
135 $tmpuser->fetch(0, $login, 0, 0, $entity);
136 if (empty($tmpuser->id)) {
137 throw new RestException(500,
'Failed to load user');
141 if (empty($tmpuser->api_key) || $reset) {
142 $tmpuser->getrights();
143 if (empty($tmpuser->rights->user->self->creer)) {
144 if (empty($tmpuser->api_key)) {
145 throw new RestException(403,
'No API token set for this user and user need write permission on itself to reset its API token');
147 throw new RestException(403,
'User need write permission on itself to reset its API token');
152 $token =
dol_hash($login.uniqid().(empty($conf->global->MAIN_API_KEY)?
'':$conf->global->MAIN_API_KEY), 1);
155 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"user";
156 $sql .=
" SET api_key = '".$this->db->escape(
dolEncrypt($token,
'',
'',
'dolibarr')).
"'";
157 $sql .=
" WHERE login = '".$this->db->escape($login).
"'";
159 dol_syslog(get_class($this).
"::login", LOG_DEBUG);
160 $result = $this->db->query($sql);
162 throw new RestException(500,
'Error when updating api_key for user :'.$this->db->lasterror());
165 $token = $tmpuser->api_key;
169 throw new RestException(500,
'Error the token for this user has not an hexa format. Try first to reset it.');
177 'entity' => $tmpuser->entity,
178 'message' =>
'Welcome '.$login.($reset ?
' - Token is new' :
' - This is your token (recorded for your user). You can use it to make any REST API call, or enter it into the DOLAPIKEY field to use the Dolibarr API explorer.')