19 use Luracast\Restler\RestException;
21 require_once DOL_DOCUMENT_ROOT.
'/user/class/user.class.php';
57 public function index($login, $password, $entity =
'', $reset = 0)
59 global $conf, $dolibarr_main_authentication, $dolibarr_auto_user;
64 if (empty($dolibarr_main_authentication)) $dolibarr_main_authentication =
'dolibarr';
67 if ($dolibarr_main_authentication ==
'forceuser')
69 if (empty($dolibarr_auto_user)) $dolibarr_auto_user =
'auto';
70 if ($dolibarr_auto_user != $login)
72 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.");
73 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.");
78 $authmode = explode(
',', $dolibarr_main_authentication);
80 if ($entity !=
'' && !is_numeric($entity))
82 throw new RestException(403,
"Bad value for entity, must be the numeric ID of company.");
84 if ($entity ==
'') $entity = 1;
86 include_once DOL_DOCUMENT_ROOT.
'/core/lib/security2.lib.php';
90 throw new RestException(403,
'Access denied');
93 $token =
'failedtogenerateorgettoken';
95 $tmpuser =
new User($this->
db);
96 $tmpuser->fetch(0, $login, 0, 0, $entity);
97 if (empty($tmpuser->id))
99 throw new RestException(500,
'Failed to load user');
103 if (empty($tmpuser->api_key) || $reset)
105 $tmpuser->getrights();
106 if (empty($tmpuser->rights->user->self->creer))
108 throw new RestException(403,
'User need write permission on itself to reset its API token');
112 $token =
dol_hash($login.uniqid().(empty($conf->global->MAIN_API_KEY)?
'':$conf->global->MAIN_API_KEY), 1);
115 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"user";
116 $sql .=
" SET api_key = '".$this->db->escape($token).
"'";
117 $sql .=
" WHERE login = '".$this->db->escape($login).
"'";
119 dol_syslog(get_class($this).
"::login", LOG_DEBUG);
120 $result = $this->
db->query($sql);
123 throw new RestException(500,
'Error when updating api_key for user :'.$this->
db->lasterror());
126 $token = $tmpuser->api_key;
134 'entity' => $tmpuser->entity,
135 '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.')
dol_hash($chain, $type= '0')
Returns a hash of a string.
Class to manage Dolibarr users.
index($login, $password, $entity= '', $reset=0)
Login.
API that allows to log in with an user account.
$conf db
API class for accounts.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
checkLoginPassEntity($usertotest, $passwordtotest, $entitytotest, $authmode, $context= '')
Return a login if login/pass was successfull.
__construct()
Constructor of the class.