From 3c82c1e1b0fe344f9bb290701e7c5797fb9c9925 Mon Sep 17 00:00:00 2001 From: Luca Haid Date: Thu, 25 Jul 2024 12:42:09 +0200 Subject: [PATCH] added delete and update functions to ipnetwork --- application/IpNetwork/IpNetworkController.php | 34 +++++++++++ application/IpNetwork/IpNetworkModel.php | 36 ++++++++++++ public/js/pages/IpNetwork/IpNetwork.js | 56 ++++++++++++++++++- 3 files changed, 123 insertions(+), 3 deletions(-) diff --git a/application/IpNetwork/IpNetworkController.php b/application/IpNetwork/IpNetworkController.php index 5477a943d..2c66a2edd 100644 --- a/application/IpNetwork/IpNetworkController.php +++ b/application/IpNetwork/IpNetworkController.php @@ -180,5 +180,39 @@ class IpNetworkController extends mfBaseController { } } + private function update(): array { + $json = json_decode(file_get_contents('php://input'), true); + + try { + IpNetworkModel::updateIpNetwork($json); + return [ + "status" => "success", + "message" => "IP Network updated." + ]; + } catch (Exception $e) { + return [ + "status" => "error", + "message" => $e->getMessage() + ]; + } + } + + private function delete(): array { + $json = json_decode(file_get_contents('php://input'), true); + + try { + IpNetworkModel::deleteIpNetwork($json['id']); + return [ + "status" => "success", + "message" => "IP Network deleted." + ]; + } catch (Exception $e) { + return [ + "status" => "error", + "message" => $e->getMessage() + ]; + } + } + } \ No newline at end of file diff --git a/application/IpNetwork/IpNetworkModel.php b/application/IpNetwork/IpNetworkModel.php index ee19afbfb..6f5811861 100644 --- a/application/IpNetwork/IpNetworkModel.php +++ b/application/IpNetwork/IpNetworkModel.php @@ -200,6 +200,24 @@ class IpNetworkModel { } } + public static function updateIpNetwork($data): void { + $db = FronkDB::singleton(); + + $sqlSetStr = ""; + $sqlSetStr .= isset($data['status']) ? "`status` = '" . $data['status'] . "', " : ""; + $sqlSetStr .= isset($data['name']) ? "`name` = '" . $data['name'] . "', " : ""; + $sqlSetStr .= isset($data['description']) ? "`description` = '" . $data['description'] . "', " : ""; + $sqlSetStr .= isset($data['location']) ? "`location` = '" . $data['location'] . "', " : ""; + $sqlSetStr .= "`edit` = UNIX_TIMESTAMP()"; + + $sql = "UPDATE `IpNetwork` SET $sqlSetStr WHERE `id` = " . $data['id']; + $result = $db->query($sql); + + if (!$result) { + throw new Exception("Failed to update network"); + } + } + public static function getById($id) { $db = FronkDB::singleton(); @@ -209,4 +227,22 @@ class IpNetworkModel { return $row ? new IpNetworkModel($row) : null; } + public static function deleteIpNetwork($id) { + // delete this id and all children and children of children until no more children + $db = FronkDB::singleton(); + $sql = "SELECT `id` FROM `IpNetwork` WHERE `parent_network_id` = $id"; + $result = $db->query($sql); + + while ($row = $result->fetch_assoc()) { + self::deleteIpNetwork($row['id']); + } + + $sql = "DELETE FROM `IpNetwork` WHERE `id` = $id"; + $result = $db->query($sql); + if (!$result) { + throw new Exception("Failed to delete network"); + } + + } + } \ No newline at end of file diff --git a/public/js/pages/IpNetwork/IpNetwork.js b/public/js/pages/IpNetwork/IpNetwork.js index cd9ec1205..dee1a0f00 100644 --- a/public/js/pages/IpNetwork/IpNetwork.js +++ b/public/js/pages/IpNetwork/IpNetwork.js @@ -17,7 +17,7 @@ Vue.component('IpNetwork', { Go Back - @@ -28,6 +28,13 @@ Vue.component('IpNetwork', { No description + + @@ -47,11 +54,12 @@ Vue.component('IpNetwork', {
- +
@@ -83,6 +91,7 @@ Vue.component('IpNetwork', {
@@ -112,6 +121,7 @@ Vue.component('IpNetwork', { {value: 'reserved', text: 'Reserved', icon: 'fas fa-lock text-warning'}] }, {text: 'Children', key: 'children', filter: 'numberRange'}, + {text: 'Aktionen', key: 'actions', sortable: false}, ], tableHeader: 'IPAM', key: 'IpNetwork' @@ -140,6 +150,34 @@ Vue.component('IpNetwork', { }, methods: { + openModal(row = false) { + if (row) { + const data = JSON.parse(JSON.stringify(row)); + + + this.addModalData = { + id: data.id, + network_address: data.network_address_str.split('/')[0], + cidr: data.cidr, + parent_network_id: this.currentNetworkData ? this.currentNetworkData.id : '', + status: data.status, + name: data.name, + description: data.description, + location: data.location, + } + } else { + this.addModalData = { + network_address: '', + cidr: '', + parent_network_id: this.currentNetworkData ? this.currentNetworkData.id : '', + status: 'active', + name: '', + description: '', + location: '', + }; + } + this.addModal = true; + }, async switchCurrentNetwork(networkId = null) { if (!networkId) { this.$refs.table.$set(this.$refs.table.filters, 'parent_network_id', undefined); @@ -159,18 +197,30 @@ Vue.component('IpNetwork', { await this.$refs.table.fetchData(); }, async addSubmit() { - const response = await axios.post(`${this.apiUrl}?do=create`, + const response = await axios.post(`${this.apiUrl}?do=${this.addModalData.id ? 'update' : 'create'}`, { ...this.addModalData, parent_network_id: this.currentNetworkData ? this.currentNetworkData.id : null }); if (response.data.status === 'success') { this.addModal = false; + this.addModalData = {}; window.notify('success', 'Network space created successfully'); await this.$refs.table.fetchData(); } else { window.notify('error', response.data.message); } }, + async deleteSubmit() { + const response = await axios.post(`${this.apiUrl}?do=delete`, {id: this.addModalData.id}); + if (response.data.status === 'success') { + this.addModal = false; + this.addModalData = {}; + window.notify('success', 'Network space deleted successfully'); + await this.$refs.table.fetchData(); + } else { + window.notify('error', response.data.message); + } + } }, })