Skip to content

Commit 31539e4

Browse files
committed
Add maintenance component
1 parent 6628b07 commit 31539e4

File tree

2 files changed

+106
-1
lines changed

2 files changed

+106
-1
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package hu.bme.sch.cmsch.component.app
2+
3+
import hu.bme.sch.cmsch.admin.dashboard.DashboardComponent
4+
import hu.bme.sch.cmsch.admin.dashboard.DashboardFormCard
5+
import hu.bme.sch.cmsch.admin.dashboard.DashboardPage
6+
import hu.bme.sch.cmsch.admin.dashboard.DashboardPermissionCard
7+
import hu.bme.sch.cmsch.component.form.FormElement
8+
import hu.bme.sch.cmsch.component.form.FormElementType
9+
import hu.bme.sch.cmsch.component.login.CmschUser
10+
import hu.bme.sch.cmsch.service.AdminMenuService
11+
import hu.bme.sch.cmsch.service.AuditLogService
12+
import hu.bme.sch.cmsch.service.ImplicitPermissions
13+
import hu.bme.sch.cmsch.util.getUser
14+
import org.slf4j.LoggerFactory
15+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean
16+
import org.springframework.context.ConfigurableApplicationContext
17+
import org.springframework.security.core.Authentication
18+
import org.springframework.stereotype.Controller
19+
import org.springframework.web.bind.annotation.PostMapping
20+
import org.springframework.web.bind.annotation.RequestMapping
21+
import org.springframework.web.bind.annotation.RequestParam
22+
23+
private const val SURE = "sure"
24+
25+
@Controller
26+
@RequestMapping("/admin/control/${MaintenanceDashboard.VIEW}")
27+
@ConditionalOnBean(ApplicationComponent::class)
28+
class MaintenanceDashboard(
29+
private val adminMenuService: AdminMenuService,
30+
appComponent: ApplicationComponent,
31+
private val auditLogService: AuditLogService,
32+
private val context: ConfigurableApplicationContext,
33+
) : DashboardPage(
34+
view = VIEW,
35+
title = "Karbantartás",
36+
description = "Itt tudod pl. redeployolni. Ha nem tudod mi ez és ide kerültél, hagyd el az oldalt és ne egyre vissza!",
37+
wide = false,
38+
adminMenuService = adminMenuService,
39+
component = appComponent,
40+
auditLog = auditLogService,
41+
showPermission = ImplicitPermissions.PERMISSION_SUPERUSER_ONLY,
42+
adminMenuCategory = ApplicationComponent.DEVELOPER_CATEGORY,
43+
adminMenuIcon = "power_settings_new",
44+
adminMenuPriority = 10
45+
) {
46+
47+
private val log = LoggerFactory.getLogger(javaClass)
48+
49+
companion object {
50+
const val VIEW = "superuser-maintenance"
51+
}
52+
53+
private val permissionCard = DashboardPermissionCard(
54+
1,
55+
permission = showPermission.permissionString,
56+
description = "Ez a jog szükséges ennek az oldalnak az olvasásához.",
57+
wide = wide
58+
)
59+
60+
override fun getComponents(user: CmschUser, requestParams: Map<String, String>): List<DashboardComponent> {
61+
return listOf(
62+
getShutdownComponent(user),
63+
permissionCard,
64+
)
65+
}
66+
67+
fun getShutdownComponent(user: CmschUser): DashboardFormCard {
68+
return DashboardFormCard(
69+
id = 2,
70+
wide = false,
71+
title = "Szerver leállítása",
72+
description = "Redeploy miatt kényelmes lehet innen leállítani a szervert.",
73+
content = listOf(
74+
FormElement(
75+
fieldName = SURE, label = "Biztos vagy benne?", type = FormElementType.CHECKBOX,
76+
formatRegex = ".*", invalidFormatMessage = "", values = "",
77+
note = "Ha ezt bepipálod, akkor le fog állni a szerver.",
78+
required = true, permanent = false, defaultValue = "false"
79+
),
80+
),
81+
buttonCaption = "LEÁLLÍTÁS",
82+
buttonIcon = "power_settings_new",
83+
action = "shutdown",
84+
method = "post",
85+
)
86+
}
87+
88+
@PostMapping("/shutdown")
89+
fun shutdown(auth: Authentication, @RequestParam allRequestParams: Map<String, String>): String {
90+
val user = auth.getUser()
91+
if (!showPermission.validate(user)) {
92+
throw IllegalStateException("Insufficient permissions")
93+
}
94+
95+
auditLogService.system("admin", "${user.userName} #${user.id} scheduled a shutdown!")
96+
97+
if (allRequestParams.getOrDefault(SURE, "off").equals("on", ignoreCase = true)) {
98+
log.warn("${user.userName} #${user.id} scheduled a shutdown!")
99+
context.close()
100+
}
101+
102+
return dashboardPage(view = VIEW, card = 2, message = "Távesz baktalé!")
103+
}
104+
105+
}

backend/src/main/kotlin/hu/bme/sch/cmsch/service/AuditLogService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class AuditLogService(
2828
private val timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS")
2929

3030
@Scheduled(cron = "0 1 0 * * ?", zone = "Europe/Budapest")
31-
fun myScheduledFunction() {
31+
fun updateFile() {
3232
targetFile = getTargetFile()
3333
}
3434

0 commit comments

Comments
 (0)