๐Ÿ› Donโ€™t show notification for remote posts
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / LoginPage.kt
1 package net.pterodactylus.sone.web.pages
2
3 import net.pterodactylus.sone.data.*
4 import net.pterodactylus.sone.main.*
5 import net.pterodactylus.sone.utils.*
6 import net.pterodactylus.sone.web.*
7 import net.pterodactylus.sone.web.page.*
8 import net.pterodactylus.util.template.*
9 import javax.inject.*
10
11 /**
12  * The login page lets the user log in.
13  */
14 @MenuName("Login")
15 @TemplatePath("/templates/login.html")
16 @ToadletPath("login.html")
17 class LoginPage @Inject constructor(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer) :
18                 SoneTemplatePage(webInterface, loaders, templateRenderer, pageTitleKey = "Page.Login.Title") {
19
20         override fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) {
21                 if (soneRequest.isPOST) {
22                         val soneId = soneRequest.httpRequest.getPartAsStringFailsafe("sone-id", 43)
23                         soneRequest.core.getLocalSone(soneId)?.let { sone ->
24                                 setCurrentSone(soneRequest.toadletContext, sone)
25                                 val target = soneRequest.httpRequest.getParam("target").emptyToNull ?: "index.html"
26                                 redirectTo(target)
27                         }
28                 }
29                 templateContext["sones"] = soneRequest.core.localSones.sortedWith(Sone.NICE_NAME_COMPARATOR)
30                 templateContext["identitiesWithoutSone"] = soneRequest.core.identityManager.allOwnIdentities.filterNot { "Sone" in it.contexts }.sortedBy { "${it.nickname}@${it.id}" }
31         }
32
33         override fun getRedirectTarget(request: FreenetRequest) =
34                         getCurrentSone(request.toadletContext)?.let { "index.html" }
35
36         override fun isEnabled(soneRequest: SoneRequest) = when {
37                 soneRequest.core.preferences.requireFullAccess && !soneRequest.toadletContext.isAllowedFullAccess -> false
38                 else -> getCurrentSone(soneRequest.toadletContext, false) == null
39         }
40
41 }