From b497d4a7200591e0b7b91948120cbfa395f5c92f Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 10 Dec 2019 18:18:28 +0100 Subject: [statistics/rebootcontrol] Add remote exec UI --- .../rebootcontrol/inc/rebootutils.inc.php | 75 ++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 modules-available/rebootcontrol/inc/rebootutils.inc.php (limited to 'modules-available/rebootcontrol/inc/rebootutils.inc.php') diff --git a/modules-available/rebootcontrol/inc/rebootutils.inc.php b/modules-available/rebootcontrol/inc/rebootutils.inc.php new file mode 100644 index 00000000..99235e8a --- /dev/null +++ b/modules-available/rebootcontrol/inc/rebootutils.inc.php @@ -0,0 +1,75 @@ +fetchAll(PDO::FETCH_ASSOC); + $ret = []; + while ($row = $res->fetch(PDO::FETCH_ASSOC)) { + $ret[$row['machineuuid']] = $row; + } + return $ret; + } + + /** + * Sort list of clients so that machines that are up and running come first. + * Requires the array elements to have key "state" from machine table. + * @param array $clients list of clients + */ + public static function sortRunningFirst(&$clients) + { + usort($clients, function($a, $b) { + $a = ($a['state'] === 'IDLE' || $a['state'] === 'OCCUPIED'); + $b = ($b['state'] === 'IDLE' || $b['state'] === 'OCCUPIED'); + if ($a === $b) + return 0; + return $a ? -1 : 1; + }); + } + + /** + * Query list of clients (by uuid), taking user context into account, by filtering + * by given $permission. + * @param array $requestedClients list of uuids + * @param string $permission name of location-aware permission to check + * @return array|false List of clients the user has access to. + */ + public static function getFilteredMachineList($requestedClients, $permission) + { + $actualClients = RebootUtils::getMachinesByUuid($requestedClients); + if (count($actualClients) !== count($requestedClients)) { + // We could go ahead an see which ones were not found in DB but this should not happen anyways unless the + // user manipulated the request + Message::addWarning('some-machine-not-found'); + } + // Filter ones with no permission + foreach (array_keys($actualClients) as $idx) { + if (!User::hasPermission($permission, $actualClients[$idx]['locationid'])) { + Message::addWarning('locations.no-permission-location', $actualClients[$idx]['locationid']); + unset($actualClients[$idx]); + } + } + // See if anything is left + if (!is_array($actualClients) || empty($actualClients)) { + Message::addError('no-clients-selected'); + return false; + } + return $actualClients; + } + +} \ No newline at end of file -- cgit v1.2.3-55-g7522