X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2FSoneTemplatePage.java;h=fa439456abe16354bb09bf5287c8f69dc6ff511b;hb=ecf753a31601e558b681daab0598009fe9eec99a;hp=496fb60a77b4104f37abe95aab782fb726d8f5aa;hpb=a9aafa22d72dd596f4686cda7fe2f17074235394;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java b/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java index 496fb60..fa43945 100644 --- a/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java +++ b/src/main/java/net/pterodactylus/sone/web/SoneTemplatePage.java @@ -19,12 +19,13 @@ package net.pterodactylus.sone.web; import java.util.Arrays; import java.util.Collection; -import java.util.UUID; import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.main.SonePlugin; import net.pterodactylus.sone.web.page.Page; import net.pterodactylus.sone.web.page.TemplatePage; import net.pterodactylus.util.template.Template; +import net.pterodactylus.util.template.TemplateContext; import freenet.clients.http.SessionManager.Session; import freenet.clients.http.ToadletContext; @@ -73,10 +74,10 @@ public class SoneTemplatePage extends TemplatePage { * Whether this page requires a login */ public SoneTemplatePage(String path, Template template, String pageTitleKey, WebInterface webInterface, boolean requireLogin) { - super(path, template, webInterface.getL10n(), pageTitleKey, "noPermission.html"); + super(path, webInterface.getTemplateContextFactory(), template, webInterface.getL10n(), pageTitleKey, "noPermission.html"); this.webInterface = webInterface; this.requireLogin = requireLogin; - template.set("webInterface", webInterface); + template.getInitialContext().set("webInterface", webInterface); } // @@ -93,7 +94,7 @@ public class SoneTemplatePage extends TemplatePage { * session */ protected Session getCurrentSession(ToadletContext toadletContenxt) { - return getCurrentSession(toadletContenxt, true); + return webInterface.getCurrentSession(toadletContenxt); } /** @@ -109,11 +110,7 @@ public class SoneTemplatePage extends TemplatePage { * session */ protected Session getCurrentSession(ToadletContext toadletContenxt, boolean create) { - Session session = webInterface.getSessionManager().useSession(toadletContenxt); - if (create && (session == null)) { - session = webInterface.getSessionManager().createSession(UUID.randomUUID().toString(), toadletContenxt); - } - return session; + return webInterface.getCurrentSession(toadletContenxt, create); } /** @@ -125,15 +122,22 @@ public class SoneTemplatePage extends TemplatePage { * currently logged in */ protected Sone getCurrentSone(ToadletContext toadletContext) { - Session session = getCurrentSession(toadletContext); - if (session == null) { - return null; - } - String soneId = (String) session.getAttribute("Sone.CurrentSone"); - if (soneId == null) { - return null; - } - return webInterface.getCore().getLocalSone(soneId, false); + return webInterface.getCurrentSone(toadletContext); + } + + /** + * Returns the currently logged in Sone. + * + * @param toadletContext + * The toadlet context + * @param create + * {@code true} to create a new session if no session exists, + * {@code false} to not create a new session + * @return The currently logged in Sone, or {@code null} if no Sone is + * currently logged in + */ + protected Sone getCurrentSone(ToadletContext toadletContext, boolean create) { + return webInterface.getCurrentSone(toadletContext, create); } /** @@ -145,12 +149,7 @@ public class SoneTemplatePage extends TemplatePage { * The Sone to set as currently logged in */ protected void setCurrentSone(ToadletContext toadletContext, Sone sone) { - Session session = getCurrentSession(toadletContext); - if (sone == null) { - session.removeAttribute("Sone.CurrentSone"); - } else { - session.setAttribute("Sone.CurrentSone", sone.getId()); - } + webInterface.setCurrentSone(toadletContext, sone); } // @@ -187,10 +186,15 @@ public class SoneTemplatePage extends TemplatePage { * {@inheritDoc} */ @Override - protected void processTemplate(Request request, Template template) throws RedirectException { - super.processTemplate(request, template); - template.set("currentSone", getCurrentSone(request.getToadletContext())); - template.set("request", request); + protected void processTemplate(Request request, TemplateContext templateContext) throws RedirectException { + super.processTemplate(request, templateContext); + templateContext.set("currentSone", getCurrentSone(request.getToadletContext(), false)); + templateContext.set("localSones", webInterface.getCore().getLocalSones()); + templateContext.set("request", request); + templateContext.set("currentVersion", SonePlugin.VERSION); + templateContext.set("hasLatestVersion", webInterface.getCore().getUpdateChecker().hasLatestVersion()); + templateContext.set("latestVersion", webInterface.getCore().getUpdateChecker().getLatestVersion()); + templateContext.set("latestVersionTime", webInterface.getCore().getUpdateChecker().getLatestVersionDate()); } /** @@ -198,7 +202,7 @@ public class SoneTemplatePage extends TemplatePage { */ @Override protected String getRedirectTarget(Page.Request request) { - if (requiresLogin() && (getCurrentSone(request.getToadletContext()) == null)) { + if (requiresLogin() && (getCurrentSone(request.getToadletContext(), false) == null)) { return "login.html"; } return null; @@ -210,7 +214,7 @@ public class SoneTemplatePage extends TemplatePage { @Override public boolean isEnabled(ToadletContext toadletContext) { if (requiresLogin()) { - return getCurrentSone(toadletContext) != null; + return getCurrentSone(toadletContext, false) != null; } return true; }