Add session provider interface
[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.isPOST
6 import net.pterodactylus.sone.web.pages.SoneTemplatePage
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
12 /**
13  * The login page lets the user log in.
14  */
15 class LoginPage(template: Template, webInterface: WebInterface):
16                 SoneTemplatePage("login.html", template, "Page.Login.Title", webInterface) {
17
18         override fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) {
19                 if (request.isPOST) {
20                         val soneId = request.httpRequest.getPartAsStringFailsafe("sone-id", 43)
21                         webInterface.core.getLocalSone(soneId)?.let { sone ->
22                                 setCurrentSone(request.toadletContext, sone)
23                                 val target = if (request.httpRequest.isParameterSet("target")) request.httpRequest.getPartAsStringFailsafe("target", 256) else "index.html"
24                                 throw RedirectException(target)
25                         }
26                 }
27                 templateContext["sones"] = webInterface.core.localSones.sortedWith(Sone.NICE_NAME_COMPARATOR)
28                 templateContext["identitiesWithoutSone"] = webInterface.core.identityManager.allOwnIdentities.filterNot { "Sone" in it.contexts }.sortedBy { "${it.nickname}@${it.id}" }
29         }
30
31         override public fun getRedirectTarget(request: FreenetRequest) =
32                         getCurrentSone(request.toadletContext)?.let { "index.html" }
33
34         override fun isEnabled(toadletContext: ToadletContext) = when {
35                 webInterface.core.preferences.isRequireFullAccess && !toadletContext.isAllowedFullAccess -> false
36                 else -> getCurrentSone(toadletContext, false) == null
37         }
38
39 }