fix(server): accept null permissions in getStreamingSimple/getProxySimple

Admin-API boot runs ResellerAPI::init() on the login page (pre-auth), where the
$rPermissions global is still null. It passes that null straight into
ServerRepository::getStreamingSimple()/getProxySimple(), whose strict
`array $rPermissions` hint turned it into a fatal TypeError at boot:

  ServerRepository::getStreamingSimple(): Argument #1 ($rPermissions) must be of
  type array, null given, called in .../ResellerAPI.php on line 72

Both methods only read $rPermissions via isset($rPermissions['is_reseller']), so
null is functionally equivalent to "no reseller restriction". Make the param
`?array $rPermissions = null` on both (restoring pre-typing tolerance) rather
than patching all ~28 call sites that feed the global. The strict `array` type
stays on every other repository method that runs post-auth with a real array.

Add ServerRepositorySimpleTest as a regression (null perms, online filter,
reseller name masking, proxy list) against the SQLite TestDb.
This commit is contained in:
Divarion_D
2026-09-13 16:10:40 +03:00
parent 284798ede7
commit a788a450a5
2 changed files with 67 additions and 6 deletions
+6 -6
View File
@@ -134,11 +134,11 @@ class ServerRepository {
/**
* Fetch streaming servers visible to the user, filtered by state.
*
* @param array $rPermissions Effective permissions.
* @param string $type State filter (e.g. 'online').
* @param array|null $rPermissions Effective permissions (null pre-auth, e.g. on the login page).
* @param string $type State filter (e.g. 'online').
* @return array Streaming server rows.
*/
public static function getStreamingSimple(array $rPermissions, string $type = 'online') {
public static function getStreamingSimple(?array $rPermissions = null, string $type = 'online') {
$db = self::db();
$rReturn = [];
$db->query('SELECT * FROM `servers` WHERE `server_type` = 0 ORDER BY `id` ASC;');
@@ -165,11 +165,11 @@ class ServerRepository {
/**
* Fetch proxy servers visible to the user.
*
* @param array $rPermissions Effective permissions.
* @param bool $rOnline Restrict to online proxies.
* @param array|null $rPermissions Effective permissions (null pre-auth, e.g. on the login page).
* @param bool $rOnline Restrict to online proxies.
* @return array Proxy server rows.
*/
public static function getProxySimple(array $rPermissions, bool $rOnline = false) {
public static function getProxySimple(?array $rPermissions = null, bool $rOnline = false) {
$db = self::db();
$rReturn = [];
$db->query('SELECT * FROM `servers` WHERE `server_type` = 1 ORDER BY `id` ASC;');
+61
View File
@@ -0,0 +1,61 @@
<?php
use XcVm\Domain\Server\ServerRepository;
use PHPUnit\Framework\TestCase;
/**
* ServerRepository::getStreamingSimple / getProxySimple — server lists for the
* admin/reseller UI, filtered by permissions.
*
* Regression: these run during admin-API boot on the login page, where the
* $rPermissions global is still null. A strict `array $rPermissions` hint made
* that a fatal TypeError at boot; the parameter must accept null (the methods
* only read it via isset()). Also covers the online filter and reseller name
* masking. Driven against the SQLite TestDb via setDb().
*/
final class ServerRepositorySimpleTest extends TestCase {
private TestDb $db;
protected function setUp(): void {
$this->db = new TestDb();
$now = time();
$this->db->exec(
'CREATE TABLE servers (id INTEGER PRIMARY KEY, server_type INTEGER, status INTEGER, last_check_ago INTEGER, is_main INTEGER, `order` INTEGER, server_name TEXT);'
);
$this->db->query(
'INSERT INTO servers (id, server_type, status, last_check_ago, is_main, `order`, server_name) VALUES
(1, 0, 1, ?, 0, 0, "Stream-1"),
(2, 0, 2, ?, 0, 0, "Stream-2"),
(3, 1, 1, ?, 0, 0, "Proxy-1");',
$now,
$now - 100000,
$now
);
ServerRepository::setDb($this->db);
}
public function testGetStreamingSimpleAcceptsNullPermissions(): void {
// Regression: null perms (login-page boot) must not throw.
$servers = ServerRepository::getStreamingSimple(null);
$this->assertSame([1], array_keys($servers), 'only the online streaming server');
$this->assertSame('Stream-1', $servers[1]['server_name'], 'name not masked without reseller perms');
}
public function testGetStreamingSimpleAllTypeReturnsOfflineToo(): void {
$servers = ServerRepository::getStreamingSimple(null, 'all');
$this->assertSame([1, 2], array_keys($servers), 'both streaming servers, proxy excluded');
}
public function testGetStreamingSimpleMasksNamesForResellers(): void {
$servers = ServerRepository::getStreamingSimple(['is_reseller' => 1], 'all');
$this->assertSame('Server #1', $servers[1]['server_name']);
$this->assertSame('Server #2', $servers[2]['server_name']);
}
public function testGetProxySimpleAcceptsNullPermissions(): void {
$proxies = ServerRepository::getProxySimple(null);
$this->assertSame([3], array_keys($proxies), 'only proxy-type servers');
}
}