20 require_once DOL_DOCUMENT_ROOT.
'/includes/restler/framework/Luracast/Restler/AutoLoader.php';
21 call_user_func(
function () {
22 $loader = Luracast\Restler\AutoLoader::instance();
23 spl_autoload_register($loader);
27 require_once DOL_DOCUMENT_ROOT.
'/includes/restler/framework/Luracast/Restler/iAuthenticate.php';
28 require_once DOL_DOCUMENT_ROOT.
'/includes/restler/framework/Luracast/Restler/iUseAuthentication.php';
29 require_once DOL_DOCUMENT_ROOT.
'/includes/restler/framework/Luracast/Restler/Resources.php';
30 require_once DOL_DOCUMENT_ROOT.
'/includes/restler/framework/Luracast/Restler/Defaults.php';
31 require_once DOL_DOCUMENT_ROOT.
'/includes/restler/framework/Luracast/Restler/RestException.php';
32 use \Luracast\Restler\iAuthenticate;
33 use \Luracast\Restler\iUseAuthentication;
34 use \Luracast\Restler\Resources;
35 use \Luracast\Restler\Defaults;
36 use \Luracast\Restler\RestException;
44 const REALM =
'Restricted Dolibarr API';
49 public static $requires = array(
'user',
'external',
'admin');
54 public static $role =
'user';
59 public static $user =
'';
83 global $conf, $db, $user;
88 $userClass = Defaults::$userIdentifierClass;
97 if (isset($_GET[
'api_key'])) {
99 $api_key = $_GET[
'api_key'];
101 if (isset($_GET[
'DOLAPIKEY'])) {
103 $api_key = $_GET[
'DOLAPIKEY'];
105 if (isset($_SERVER[
'HTTP_DOLAPIKEY'])) {
106 $api_key = $_SERVER[
'HTTP_DOLAPIKEY'];
112 $sql =
"SELECT u.login, u.datec, u.api_key, ";
113 $sql .=
" u.tms as date_modification, u.entity";
114 $sql .=
" FROM ".MAIN_DB_PREFIX.
"user as u";
115 $sql .=
" WHERE u.api_key = '".$this->db->escape($api_key).
"'";
118 $result = $this->
db->query($sql);
120 if ($this->
db->num_rows($result)) {
121 $obj = $this->
db->fetch_object($result);
122 $login = $obj->login;
123 $stored_key = $obj->api_key;
124 $userentity = $obj->entity;
126 if (!defined(
"DOLENTITY") && $conf->entity != ($obj->entity ? $obj->entity : 1)) {
127 $conf->entity = ($obj->entity ? $obj->entity : 1);
129 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);
130 $conf->setValues($this->
db);
134 throw new RestException(503,
'Error when fetching user api_key :'.$this->
db->error_msg);
137 if ($stored_key != $api_key) {
138 $userClass::setCacheIdentifier($api_key);
143 throw new RestException(503,
'Error when searching login user from api key');
145 $fuser =
new User($this->
db);
146 $result = $fuser->fetch(
'', $login,
'', 0, (empty($userentity) ? -1 : $conf->entity));
148 throw new RestException(503,
'Error when fetching user :'.$fuser->error.
' (conf->entity='.$conf->entity.
')');
154 static::$user = $fuser;
160 static::$role =
'external';
164 static::$role =
'admin';
167 throw new RestException(401,
"Failed to login to API. No parameter 'HTTP_DOLAPIKEY' on HTTP header (and no parameter DOLAPIKEY in URL).");
170 $userClass::setCacheIdentifier(static::$role);
171 Resources::$accessControlFunction =
'DolibarrApiAccess::verifyAccess';
172 $requirefortest = static::$requires;
173 if (!is_array($requirefortest)) {
174 $requirefortest = explode(
',', $requirefortest);
176 return in_array(static::$role, (array) $requirefortest) || static::$role ==
'admin';
199 $requires = isset($m[
'class'][
'DolibarrApiAccess'][
'properties'][
'requires'])
200 ? $m[
'class'][
'DolibarrApiAccess'][
'properties'][
'requires']
205 ? static::$role ==
'admin' || in_array(static::$role, (array) $requires)
Dolibarr API access class.
__construct()
Constructor.
__isAllowed()
Check access.
static verifyAccess(array $m)
Verify access.
__getWWWAuthenticateString()
Class to manage Dolibarr users.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db
API class for accounts.