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