From 9e954a509f3e69fbe9d06ba26975e581af0dbd20 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 7 Dec 2021 16:14:19 +0100 Subject: [passthrough] Add location assignment page --- modules-available/passthrough/page.inc.php | 49 ++++++++++++++++++++++ .../passthrough/permissions/permissions.json | 4 +- .../passthrough/templates/hardware-list.html | 2 +- .../passthrough/templates/location-assign.html | 28 +++++++++++++ 4 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 modules-available/passthrough/templates/location-assign.html diff --git a/modules-available/passthrough/page.inc.php b/modules-available/passthrough/page.inc.php index 7dcc8215..b0c830ef 100644 --- a/modules-available/passthrough/page.inc.php +++ b/modules-available/passthrough/page.inc.php @@ -10,6 +10,8 @@ class Page_Passthrough extends Page $action = Request::post('action'); if ($action === 'save-hwlist') { $this->saveHwList(); + } elseif ($action === 'save-location') { + $this->saveLocation(); } if (Request::isPost()) { Util::redirect('?do=passthrough'); @@ -18,6 +20,7 @@ class Page_Passthrough extends Page private function saveHwList() { + User::assertPermission('edit.group'); $newgroups = Request::post('newgroup', [], 'array'); foreach ($newgroups as $id => $title) { $id = strtoupper(preg_replace('/[^a-z0-9_\-]/i', '', $id)); @@ -49,6 +52,30 @@ class Page_Passthrough extends Page Util::redirect('?do=passthrough&show=hwlist'); } + private function saveLocation() + { + $locationId = Request::post('locationid', Request::REQUIRED, 'int'); + User::assertPermission('edit.location', $locationId); + $list = []; + $groups = []; + foreach (Request::post('enabled', [], 'array') as $groupId) { + $groupId = (int)$groupId; + $list[] = [$groupId, $locationId]; + $groups[] = $groupId; + } + if (!empty($list)) { + Database::exec("INSERT IGNORE INTO passthrough_group_x_location (groupid, locationid) + VALUES :list", ['list' => $list]); + Database::exec("DELETE FROM passthrough_group_x_location + WHERE locationid = :lid AND groupid NOT IN (:groups)", ['lid' => $locationId, 'groups' => $groups]); + } else { + Database::exec("DELETE FROM passthrough_group_x_location + WHERE locationid = :lid", ['lid' => $locationId]); + } + Message::addSuccess('location-updated', Location::getName($locationId)); + Util::redirect('?do=passthrough&show=assignlocation&locationid=' . $locationId); + } + /* * */ @@ -58,11 +85,16 @@ class Page_Passthrough extends Page $show = Request::get('show'); if ($show === 'hwlist') { $this->showHardwareList(); + } elseif ($show === 'assignlocation') { + $this->showLocationMapping(); } else { Util::redirect('?do=passthrough&show=hwlist'); } } + /** + * Show all the hardware that is known. Start with video adapters. + */ private function showHardwareList() { $q = new HardwareQuery(HardwareInfo::PCI_DEVICE, null, false); @@ -118,6 +150,23 @@ class Page_Passthrough extends Page } } + /** + * Show mapping between specific location and passthrough groups. + * @return void + */ + private function showLocationMapping() + { + $locationId = Request::get('locationid', Request::REQUIRED, 'int'); + $res = Database::queryAll("SELECT g.groupid, g.title, gxl.locationid AS enabled FROM passthrough_group g + LEFT JOIN passthrough_group_x_location gxl ON (g.groupid = gxl.groupid AND gxl.locationid = :locationid) + ORDER BY gxl.locationid ASC", + ['locationid' => $locationId]); + Render::addTemplate('location-assign', [ + 'list' => array_reverse($res), + 'locationid' => $locationId, + ]); + } + /* * */ diff --git a/modules-available/passthrough/permissions/permissions.json b/modules-available/passthrough/permissions/permissions.json index d0932f1e..bf3095c4 100644 --- a/modules-available/passthrough/permissions/permissions.json +++ b/modules-available/passthrough/permissions/permissions.json @@ -1,3 +1,5 @@ { - "view": false + "view": false, + "edit.group": false, + "edit.location": true } \ No newline at end of file diff --git a/modules-available/passthrough/templates/hardware-list.html b/modules-available/passthrough/templates/hardware-list.html index d3424b7a..720427fd 100644 --- a/modules-available/passthrough/templates/hardware-list.html +++ b/modules-available/passthrough/templates/hardware-list.html @@ -1,4 +1,4 @@ -
+ diff --git a/modules-available/passthrough/templates/location-assign.html b/modules-available/passthrough/templates/location-assign.html new file mode 100644 index 00000000..c2a8f805 --- /dev/null +++ b/modules-available/passthrough/templates/location-assign.html @@ -0,0 +1,28 @@ + + + +
+
{{lang_group}}
+
{{lang_enabled}}
+
+ {{#list}} +
+
+ {{groupid}} + {{title}} +
+
+
+ + +
+
+
+ {{/list}} +
+ +
+ \ No newline at end of file -- cgit v1.2.3-55-g7522