Added IPAM IpNetwork Module
This commit is contained in:
@@ -0,0 +1,184 @@
|
||||
<?php
|
||||
|
||||
class IpNetworkController extends mfBaseController {
|
||||
private User $me;
|
||||
|
||||
|
||||
protected function init(): void {
|
||||
$me = new User();
|
||||
$me->loadMe();
|
||||
$this->layout()->set("me", $me);
|
||||
$this->me = $me;
|
||||
|
||||
if (!$this->me->isAdmin()) {
|
||||
$this->redirect("dashboard");
|
||||
}
|
||||
}
|
||||
|
||||
protected function indexAction(): void {
|
||||
$JSGlobals = ["BASE_URL" => self::getUrl("IpNetwork"),
|
||||
"DASHBOARD_URL" => self::getUrl("Dashboard"),
|
||||
"MFAPPNAME" => MFAPPNAME_SLUG,
|
||||
"PAGE_TITLE" => "IPAM",
|
||||
"PATH" => [
|
||||
["text" => MFAPPNAME_SLUG, "href" => self::getUrl("Dashboard")],
|
||||
["text" => "IPAM", "href" => self::getUrl("IpNetwork")]
|
||||
],
|
||||
"IPNETWORK_API_URL" => self::getUrl("IpNetwork/api"),
|
||||
];
|
||||
|
||||
$this->layout()->set("vueViewName", "IpNetwork");
|
||||
$this->layout()->set("JSGlobals", $JSGlobals);
|
||||
$this->layout()->setTemplate("VueViews/Vue");
|
||||
}
|
||||
|
||||
protected function apiAction() {
|
||||
$do = $this->request->do;
|
||||
|
||||
if (!$this->me->isAdmin()) {
|
||||
$this->redirect("dashboard");
|
||||
}
|
||||
|
||||
switch ($do) {
|
||||
case "get":
|
||||
$return = $this->get();
|
||||
break;
|
||||
case "getById":
|
||||
$return = $this->getById();
|
||||
break;
|
||||
case "create":
|
||||
$return = $this->create();
|
||||
break;
|
||||
case "update":
|
||||
$return = $this->update();
|
||||
break;
|
||||
case "delete":
|
||||
$return = $this->delete();
|
||||
break;
|
||||
default:
|
||||
$return = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!$return) {
|
||||
$return = [
|
||||
"status" => "error",
|
||||
"message" => "Invalid request."
|
||||
];
|
||||
}
|
||||
|
||||
header('Content-Type: application/json');
|
||||
die(json_encode($return));
|
||||
}
|
||||
|
||||
private function aggregateChildren($network, &$childrenCount) {
|
||||
$children = IpNetworkModel::getChildren($network->id);
|
||||
|
||||
foreach ($children as $child) {
|
||||
$childrenCount++;
|
||||
if ($child->cidr !== 32) {
|
||||
$this->aggregateChildren($child, $childrenCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function get(): array {
|
||||
$json = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
$filters = $json['filters'] ?? [];
|
||||
$order = $json['order'] ?? [];
|
||||
$page = $json['pagination']['page'] ?? 1;
|
||||
$perPage = $json['pagination']['per_page'] ?? 10;
|
||||
|
||||
$orderChildren = false;
|
||||
|
||||
if ($order['key'] === 'network_address_str') {
|
||||
$order['key'] = 'network_address';
|
||||
} else if ($order['key'] === 'children') {
|
||||
$orderChildren = $order['order'];
|
||||
$order = null;
|
||||
}
|
||||
|
||||
$networks = IpNetworkModel::getIpNetworks($filters, null, $perPage * $page - $perPage, $order);
|
||||
$total_rows = IpNetworkModel::countIpNetworks([
|
||||
"parent_network_id" => $filters['parent_network_id'] ?? ''
|
||||
]);
|
||||
|
||||
$processedNetworks = [];
|
||||
|
||||
foreach ($networks as $network) {
|
||||
$childrenCount = 0;
|
||||
$this->aggregateChildren($network, $childrenCount);
|
||||
|
||||
$from = $filters['children']['from'] ?? null;
|
||||
$to = $filters['children']['to'] ?? null;
|
||||
|
||||
if (($from !== null && $childrenCount < $from) || ($to !== null && $childrenCount > $to)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$network->children = $childrenCount;
|
||||
$processedNetworks[] = $network;
|
||||
}
|
||||
|
||||
if ($orderChildren) {
|
||||
usort($processedNetworks, function ($a, $b) use ($orderChildren) {
|
||||
if ($orderChildren === 'asc') {
|
||||
return $a->children <=> $b->children;
|
||||
} else {
|
||||
return $b->children <=> $a->children;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return [
|
||||
"rows" => array_slice($processedNetworks, $perPage * $page - $perPage, $perPage),
|
||||
"pagination" => [
|
||||
"page" => $page,
|
||||
"total_pages" => ceil(count($processedNetworks) / $perPage),
|
||||
"filtered_available" => count($processedNetworks),
|
||||
"per_page" => $perPage,
|
||||
"total_rows" => intval($total_rows)
|
||||
]
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
private function getById(): array {
|
||||
$json = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
$network = IpNetworkModel::getById($json['id']);
|
||||
|
||||
if ($network === null) {
|
||||
return [
|
||||
"status" => "error",
|
||||
"message" => "Network not found."
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
"status" => "success",
|
||||
"network" => $network
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
private function create(): array {
|
||||
$json = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
try {
|
||||
IpNetworkModel::createIpNetwork($json);
|
||||
return [
|
||||
"status" => "success",
|
||||
"message" => "IP Network created."
|
||||
];
|
||||
} catch (Exception $e) {
|
||||
return [
|
||||
"status" => "error",
|
||||
"message" => $e->getMessage()
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user