-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentView.swift
More file actions
303 lines (275 loc) · 10.4 KB
/
ContentView.swift
File metadata and controls
303 lines (275 loc) · 10.4 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
//
// [ContentView].swift
// Aman - [Aman]
//
// Created by Aman Team on [08/11/25].
//
import SwiftUI
import AppKit
import UniformTypeIdentifiers
private enum SortKey: String, CaseIterable, Identifiable {
case verdict
case severity
case title
var id: String { rawValue }
var label: String {
switch self {
case .verdict: return "Status"
case .severity: return "Severity"
case .title: return "Name"
}
}
}
struct ContentView: View {
@StateObject private var coordinator = AuditCoordinator()
@State private var selectedDomain: AuditDomain = .all
@State private var selection: UUID?
@State private var searchText: String = ""
@State private var sortKey: SortKey = .verdict
@State private var ascending: Bool = true
@State private var pendingAutoSelection = false
private var displayedFindings: [AuditFinding] {
let base: [AuditFinding]
if selectedDomain == .all {
base = coordinator.findings
} else {
base = coordinator.findings.filter { $0.categories.contains(selectedDomain.title) }
}
let searched = searchText.trimmingCharacters(in: .whitespacesAndNewlines)
let filtered: [AuditFinding]
if searched.isEmpty {
filtered = base
} else {
filtered = base.filter { finding in
let haystack = """
\(finding.title) \(finding.categoryDisplay) \(finding.synopsis) \
\(finding.statusSummary ?? "") \(finding.remediation) \
\(finding.rationale)
""".lowercased()
return haystack.contains(searched.lowercased())
}
}
let sorted = applySorting(to: filtered)
return sorted
}
private var selectedFinding: AuditFinding? {
guard let selection else { return nil }
return coordinator.findings.first { $0.id == selection }
}
var body: some View {
NavigationSplitView {
AuditSidebarView(
coordinator: coordinator,
selectedDomain: $selectedDomain,
startAudit: runAudit,
resetAudit: resetAudit,
exportAction: exportFindings
)
} content: {
FindingListView(
findings: displayedFindings,
selection: $selection,
selectedDomain: selectedDomain
)
.frame(minWidth: 360)
} detail: {
if let selectedFinding {
FindingDetailView(finding: selectedFinding)
} else if coordinator.findings.isEmpty && !coordinator.isRunning {
LandingView(startAudit: runAudit)
} else {
FindingDetailPlaceholder(
isRunning: coordinator.isRunning,
hasResults: !coordinator.findings.isEmpty
)
}
}
.navigationSplitViewStyle(.balanced)
.navigationSplitViewColumnWidth(min: 310, ideal: 380)
.searchable(text: $searchText, placement: .toolbar, prompt: "Search findings")
.toolbar {
ToolbarItemGroup(placement: .primaryAction) {
HStack(spacing: 8) {
Picker("Sort by", selection: $sortKey) {
ForEach(SortKey.allCases) { key in
Text(key.label).tag(key)
}
}
.pickerStyle(.segmented)
Button {
ascending.toggle()
} label: {
Image(systemName: ascending ? "arrow.down.circle" : "arrow.up.circle")
}
.help(ascending ? "Ascending order" : "Descending order")
}
}
}
.frame(minWidth: 900, minHeight: 560)
.onChange(of: selectedDomain) { scheduleSafeAutoSelection(clearFirst: true) }
.onChange(of: coordinator.findings) { scheduleSafeAutoSelection(clearFirst: false) }
.onChange(of: searchText) { scheduleSafeAutoSelection(clearFirst: true) }
.onChange(of: sortKey) { scheduleSafeAutoSelection(clearFirst: false) }
.onChange(of: ascending) { scheduleSafeAutoSelection(clearFirst: false) }
}
private func runAudit() {
selection = nil
if selectedDomain != .all {
selectedDomain = .all
}
coordinator.start(domain: nil)
}
private func resetAudit() {
selection = nil
coordinator.reset()
}
private func scheduleSafeAutoSelection(clearFirst: Bool) {
guard !coordinator.isRunning else { return }
if clearFirst {
selection = nil
}
guard !pendingAutoSelection else { return }
pendingAutoSelection = true
DispatchQueue.main.async {
pendingAutoSelection = false
safeAutoSelectFirstIfNeeded()
}
}
private func safeAutoSelectFirstIfNeeded() {
guard !displayedFindings.isEmpty else {
selection = nil
return
}
if let sel = selection, displayedFindings.contains(where: { $0.id == sel }) {
return
}
selection = displayedFindings.first?.id
}
private func applySorting(to findings: [AuditFinding]) -> [AuditFinding] {
let sorted: [AuditFinding]
switch sortKey {
case .verdict:
let priority: [AuditFinding.Verdict: Int] = [
.actionRequired: 0,
.investigate: 1,
.pass: 2,
.unknown: 3
]
sorted = findings.sorted { lhs, rhs in
let lp = priority[lhs.verdict, default: 3]
let rp = priority[rhs.verdict, default: 3]
if lp == rp { return lhs.title.localizedCaseInsensitiveCompare(rhs.title) == .orderedAscending }
return lp < rp
}
case .severity:
let priority: [AuditFinding.Severity: Int] = [
.critical: 0,
.high: 1,
.medium: 2,
.low: 3,
.informational: 4,
.unknown: 5
]
sorted = findings.sorted { lhs, rhs in
let lp = priority[lhs.severity, default: 5]
let rp = priority[rhs.severity, default: 5]
if lp == rp { return lhs.title.localizedCaseInsensitiveCompare(rhs.title) == .orderedAscending }
return lp < rp
}
case .title:
sorted = findings.sorted {
$0.title.localizedCaseInsensitiveCompare($1.title) == .orderedAscending
}
}
return ascending ? sorted : Array(sorted.reversed())
}
private func exportFindings() {
let savePanel = NSSavePanel()
if #available(macOS 12.0, *) {
savePanel.allowedContentTypes = [.html]
} else {
savePanel.allowedFileTypes = ["html"]
}
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
let timestamp = formatter.string(from: Date()).replacingOccurrences(of: ":", with: "-")
savePanel.nameFieldStringValue = "Aman-Audit-\(timestamp).html"
savePanel.begin { response in
guard response == .OK, let url = savePanel.url else { return }
do {
let html = buildHTMLReport()
try html.write(to: url, atomically: true, encoding: .utf8)
} catch {
NSSound.beep()
NSLog("HTML export failed: \(error.localizedDescription)")
}
}
}
private func buildHTMLReport() -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .short
let generated = dateFormatter.string(from: Date())
let rows = coordinator.findings.map { finding -> String in
"""
<tr>
<td>\(htmlEscape(finding.categoryDisplay))</td>
<td>\(htmlEscape(finding.title))</td>
<td>\(htmlEscape(finding.verdict.displayLabel))</td>
<td>\(htmlEscape(finding.severityDisplay))</td>
<td>\(htmlEscape(finding.statusSummary ?? ""))</td>
<td>\(htmlEscape(finding.remediation))</td>
</tr>
"""
}.joined(separator: "\n")
return """
<!DOCTYPE html>
<html lang=\"en\">
<head>
<meta charset=\"utf-8\">
<title>Aman Security Report</title>
<style>
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; background-color: #f5f5f7; color: #111; margin: 24px; }
h1 { font-size: 28px; margin-bottom: 4px; }
h2 { font-size: 16px; font-weight: 500; color: #666; margin-top: 0; }
table { width: 100%; border-collapse: collapse; margin-top: 24px; }
th, td { padding: 12px; border: 1px solid rgba(0,0,0,0.1); text-align: left; vertical-align: top; }
th { background-color: rgba(0,0,0,0.04); font-weight: 600; }
tr:nth-child(even) { background-color: rgba(0,0,0,0.02); }
</style>
</head>
<body>
<h1>Aman Security Report</h1>
<h2>Generated on \(htmlEscape(generated)) · Total checks: \(coordinator.findings.count)</h2>
<table>
<thead>
<tr>
<th>Category</th>
<th>Check</th>
<th>Status</th>
<th>Severity</th>
<th>Current State</th>
<th>Recommended Action</th>
</tr>
</thead>
<tbody>
\(rows)
</tbody>
</table>
</body>
</html>
"""
}
private func htmlEscape(_ string: String) -> String {
string
.replacingOccurrences(of: "&", with: "&")
.replacingOccurrences(of: "<", with: "<")
.replacingOccurrences(of: ">", with: ">")
.replacingOccurrences(of: "\"", with: """)
.replacingOccurrences(of: "'", with: "'")
}
}
#Preview {
ContentView()
.frame(width: 1100, height: 620)
}