Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/DbUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,10 @@ public function getEntitiesRestrictCriteria(
return [new QueryExpression('true')];
} elseif (isCommandLine() || Session::isCron()) {
$value = '0'; // If value is not set, fallback to root entity in cron / command line
} else {
// No active session and no privileged context: deny all access to prevent
// invalid SQL criterion (entities_id = '' on integer column → MySQL warning 1292).
return [new QueryExpression('false')];
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/autoload/misc-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@
*
* @return bool
*/
function isCommandLine()
function isCommandLine(): bool
{
return (PHP_SAPI == 'cli');
/** @var bool|null $GLPI_IS_COMMAND_LINE */
global $GLPI_IS_COMMAND_LINE;
return $GLPI_IS_COMMAND_LINE ?? (PHP_SAPI === 'cli');
}

/**
Expand Down
25 changes: 25 additions & 0 deletions tests/functional/DbUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,31 @@ public function testGetEntityRestrict()
);
}

public function testGetEntitiesRestrictCriteriaWithNoSession(): void
{
// PHP_SAPI is always 'cli' in PHPUnit and cannot be changed at runtime.
// Override via the global read by isCommandLine() to simulate a web context.
$GLOBALS['GLPI_IS_COMMAND_LINE'] = false;

// Ensure no active session entities and no right-check bypass.
unset($_SESSION['glpiactiveentities']);
unset($_SESSION['glpishowallentities']);

$this->assertFalse(isCommandLine());
$this->assertFalse(\Session::isCron());

$criteria = getEntitiesRestrictCriteria('glpi_computers');
$first = reset($criteria);

$this->assertCount(1, $criteria);
$this->assertIsArray($first);
$this->assertCount(1, $first);
$this->assertInstanceOf(QueryExpression::class, $first[0]);
$this->assertSame('false', (string) $first[0]);

unset($GLOBALS['GLPI_IS_COMMAND_LINE']);
}

/**
* Run getAncestorsOf tests
*
Expand Down