Replace delete image page with Kotlin version
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / LoginPage.kt
1 package net.pterodactylus.sone.web
2
3 import freenet.clients.http.ToadletContext
4 import net.pterodactylus.sone.data.Sone
5 import net.pterodactylus.sone.web.page.FreenetRequest
6 import net.pterodactylus.util.template.Template
7 import net.pterodactylus.util.template.TemplateContext
8 import net.pterodactylus.util.web.Method.POST
9
10 /**
11  * The login page lets the user log in.
12  */
13 class LoginPage(template: Template, webInterface: WebInterface):
14                 SoneTemplatePage("login.html", template, "Page.Login.Title", webInterface) {
15
16         override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) {
17                 if (request.method == POST) {
18                         val soneId = request.httpRequest.getPartAsStringFailsafe("sone-id", 43)
19                         webInterface.core.getLocalSone(soneId)?.let { sone ->
20                                 setCurrentSone(request.toadletContext, sone)
21                                 val target = if (request.httpRequest.isParameterSet("target")) request.httpRequest.getPartAsStringFailsafe("target", 256) else "index.html"
22                                 throw RedirectException(target)
23                         }
24                 }
25                 templateContext["sones"] = webInterface.core.localSones.sortedWith(Sone.NICE_NAME_COMPARATOR)
26                 templateContext["identitiesWithoutSone"] = webInterface.core.identityManager.allOwnIdentities.filterNot { "Sone" in it.contexts }.sortedBy { "${it.nickname}@${it.id}" }
27         }
28
29         override public fun getRedirectTarget(request: FreenetRequest) =
30                         getCurrentSone(request.toadletContext)?.let { "index.html" }
31
32         override fun isEnabled(toadletContext: ToadletContext) = when {
33                 webInterface.core.preferences.isRequireFullAccess && !toadletContext.isAllowedFullAccess -> false
34                 else -> getCurrentSoneWithoutCreatingSession(toadletContext) == null
35         }
36
37 }