Skip to content

Commit

Permalink
Fix init of environment for a dedicated entity in API
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Oct 19, 2017
1 parent 83440f5 commit 8da7f78
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 5 additions & 2 deletions htdocs/api/class/api_access.class.php
Expand Up @@ -107,7 +107,7 @@ public function __isAllowed()
$sql.= " FROM ".MAIN_DB_PREFIX."user as u";
$sql.= " WHERE u.api_key = '".$db->escape($api_key)."'";
// TODO Check if 2 users has same API key.

$result = $db->query($sql);
if ($result)
{
Expand All @@ -118,9 +118,12 @@ public function __isAllowed()
$stored_key = $obj->api_key;
$userentity = $obj->entity;

if (! defined("DOLENTITY")) // If API was not forced with HTTP_DOLENTITY, we set entity to entity of user
if (! defined("DOLENTITY") && $conf->entity != $obj->entity) // If API was not forced with HTTP_DOLENTITY, and user is on another entity, so we reset entity to entity of user
{
$conf->entity = ($obj->entity?$obj->entity:1);
// We must also reload global conf to get params from the entity
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);
$conf->setValues($db);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions htdocs/api/index.php
Expand Up @@ -37,6 +37,10 @@
if (! defined("NOLOGIN")) define("NOLOGIN",'1'); // If this page is public (can be called outside logged session)


// Force entity if a value is provided into HTTP header. Otherwise, will use the entity of user of token used.
if (! empty($_SERVER['HTTP_DOLAPIENTITY'])) define("DOLENTITY", (int) $_SERVER['HTTP_DOLAPIENTITY']);


$res=0;
if (! $res && file_exists("../main.inc.php")) $res=include '../main.inc.php';
if (! $res) die("Include of main fails");
Expand Down

0 comments on commit 8da7f78

Please sign in to comment.