-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanage_groups.php
More file actions
90 lines (74 loc) · 3.24 KB
/
Copy pathmanage_groups.php
File metadata and controls
90 lines (74 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
require_once __DIR__ . '/inc/bootstrap.php';
requireLogin();
requireAdmin();
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!csrf_verify($_POST['csrf'] ?? '')) {
echo json_encode(['success' => false, 'message' => 'CSRF verification failed']);
exit;
}
$action = $_POST['action'] ?? '';
if ($action === 'add_group') {
$groupName = trim($_POST['group_name'] ?? '');
if (empty($groupName)) {
echo json_encode(['success' => false, 'message' => 'Group name is required']);
exit;
}
try {
// Check if group already exists
$stmt = db()->prepare('SELECT COUNT(*) FROM firewalls WHERE customer_group = ?');
$stmt->execute([$groupName]);
$count = $stmt->fetchColumn();
if ($count > 0) {
echo json_encode(['success' => false, 'message' => 'Group already exists']);
exit;
}
// Add a dummy firewall with this group to create the group
$stmt = db()->prepare('INSERT INTO firewalls (hostname, ip_address, customer_group, status) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE id=id');
$stmt->execute(['temp-' . time(), '127.0.0.1', $groupName, 'temp']);
// Delete the temp firewall
$stmt = db()->prepare('DELETE FROM firewalls WHERE hostname LIKE ?');
$stmt->execute(['temp-%']);
echo json_encode([
'success' => true,
'message' => 'Group added successfully',
'group_name' => $groupName,
'group_id' => md5($groupName)
]);
} catch (Exception $e) {
error_log("manage_groups.php error: " . $e->getMessage());
echo json_encode(['success' => false, 'message' => 'Internal server error']);
}
} elseif ($action === 'delete_group') {
$groupName = trim($_POST['group_name'] ?? '');
if (empty($groupName)) {
echo json_encode(['success' => false, 'message' => 'Group name is required']);
exit;
}
try {
// Check if group has firewalls
$stmt = db()->prepare('SELECT COUNT(*) FROM firewalls WHERE customer_group = ?');
$stmt->execute([$groupName]);
$count = $stmt->fetchColumn();
if ($count > 0) {
echo json_encode(['success' => false, 'message' => 'Cannot delete group with existing firewalls']);
exit;
}
// Group deletion is implicit - just return success
echo json_encode([
'success' => true,
'message' => 'Group deleted successfully',
'group_id' => md5($groupName)
]);
} catch (Exception $e) {
error_log("manage_groups.php error: " . $e->getMessage());
echo json_encode(['success' => false, 'message' => 'Internal server error']);
}
} else {
echo json_encode(['success' => false, 'message' => 'Invalid action']);
}
} else {
echo json_encode(['success' => false, 'message' => 'Method not allowed']);
}
?>