Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
18 changes: 16 additions & 2 deletions src/DbUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
*
* @since 9.2
*/
final class DbUtils
class DbUtils
Comment thread
RomainLvr marked this conversation as resolved.
Outdated
{
/**
* Return foreign key field name for a table
Expand Down Expand Up @@ -890,6 +890,16 @@ public function getEntitiesRestrictRequest(
return $query;
}

/**
* Check whether the current execution context is privileged (CLI or cron).
*
* @return bool
*/
protected function isPrivilegedContext(): bool
{
return isCommandLine() || Session::isCron();
}

/**
* Get criteria to restrict to current entities of the user
*
Expand Down Expand Up @@ -942,8 +952,12 @@ public function getEntitiesRestrictCriteria(
$value = $_SESSION['glpiactiveentities'];
} elseif (Session::isRightChecksDisabled()) {
return [new QueryExpression('true')];
} elseif (isCommandLine() || Session::isCron()) {
} elseif ($this->isPrivilegedContext()) {
$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
23 changes: 23 additions & 0 deletions tests/functional/DbUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,29 @@ public function testGetEntityRestrict()
);
}

public function testGetEntitiesRestrictCriteriaWithNoSession()
{
// Use a subclass that overrides isPrivilegedContext() to simulate a
// non-CLI, non-cron context (unreachable in PHPUnit because PHP_SAPI === 'cli').
$instance = new class extends \DbUtils {
protected function isPrivilegedContext(): bool
{
return false;
}
};

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

$criteria = $instance->getEntitiesRestrictCriteria('glpi_computers');

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

/**
* Run getAncestorsOf tests
*
Expand Down