X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fpages%2FSoneTemplatePage.kt;h=ac1e15f11677eba2a9ec3785ac23151b5e417982;hp=3f04236ac871b113de94e06cc19d2c47ecd241bd;hb=c28013c8a4bcb9776a1e2d82ffd6c4c8297ffb62;hpb=ffd92ca2374c0b2218e583d02e0bdd24b8c110ae diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt index 3f04236..ac1e15f 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt @@ -1,38 +1,32 @@ package net.pterodactylus.sone.web.pages -import freenet.clients.http.ToadletContext +import freenet.clients.http.* import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.main.SonePlugin import net.pterodactylus.sone.utils.emptyToNull import net.pterodactylus.sone.web.SessionProvider import net.pterodactylus.sone.web.WebInterface -import net.pterodactylus.sone.web.page.FreenetRequest -import net.pterodactylus.sone.web.page.FreenetTemplatePage +import net.pterodactylus.sone.web.page.* import net.pterodactylus.util.notify.Notification import net.pterodactylus.util.template.Template import net.pterodactylus.util.template.TemplateContext +import net.pterodactylus.util.web.* import java.net.URLEncoder /** * Base page for the Sone web interface. */ -open class SoneTemplatePage( +open class SoneTemplatePage @JvmOverloads constructor( path: String, - protected val webInterface: WebInterface, + private val webInterface: WebInterface, template: Template, private val pageTitleKey: String? = null, - private val requiresLogin: Boolean = true + private val requiresLogin: Boolean = false, + private val pageTitle: (FreenetRequest) -> String = { pageTitleKey?.let(webInterface.l10n::getString) ?: "" } ) : FreenetTemplatePage(path, webInterface.templateContextFactory, template, "noPermission.html") { - @JvmOverloads - constructor(path: String, template: Template, pageTitleKey: String?, webInterface: WebInterface, requireLogin: Boolean = false) : - this(path, webInterface, template, pageTitleKey, requireLogin) - - constructor(path: String, template: Template, webInterface: WebInterface, requireLogin: Boolean = true) : - this(path, webInterface, template, null, requireLogin) - private val core = webInterface.core - protected val sessionProvider: SessionProvider = webInterface + private val sessionProvider: SessionProvider = webInterface protected fun getCurrentSone(toadletContext: ToadletContext, createSession: Boolean = true) = sessionProvider.getCurrentSone(toadletContext, createSession) @@ -42,8 +36,9 @@ open class SoneTemplatePage( fun requiresLogin() = requiresLogin - override public fun getPageTitle(freenetRequest: FreenetRequest) = - pageTitleKey?.let(webInterface.l10n::getString) ?: "" + override public fun getPageTitle(freenetRequest: FreenetRequest) = getPageTitle(freenetRequest.toSoneRequest(core, webInterface)) + + open fun getPageTitle(soneRequest: SoneRequest) = pageTitle(soneRequest) override public fun getStyleSheets() = listOf("css/sone.css") @@ -76,7 +71,11 @@ open class SoneTemplatePage( handleRequest(freenetRequest, templateContext) } - internal open fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { + open fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { + handleRequest(freenetRequest.toSoneRequest(core, webInterface), templateContext) + } + + open fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) { } override public fun getRedirectTarget(freenetRequest: FreenetRequest): String? { @@ -92,9 +91,12 @@ open class SoneTemplatePage( private val String.urlEncode: String get() = URLEncoder.encode(this, "UTF-8") - override fun isEnabled(toadletContext: ToadletContext) = when { - requiresLogin && getCurrentSone(toadletContext) == null -> false - core.preferences.isRequireFullAccess && !toadletContext.isAllowedFullAccess -> false + override fun isEnabled(toadletContext: ToadletContext) = + isEnabled(SoneRequest(toadletContext.uri, Method.GET, HTTPRequestImpl(toadletContext.uri, "GET"), toadletContext, webInterface.l10n, webInterface.sessionManager, core, webInterface)) + + open fun isEnabled(soneRequest: SoneRequest) = when { + requiresLogin && getCurrentSone(soneRequest.toadletContext) == null -> false + core.preferences.requireFullAccess && !soneRequest.toadletContext.isAllowedFullAccess -> false else -> true }