X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=inline;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fdemoscenemusic%2Fpage%2FBasePage.java;h=f6f211e3f578c27fa106b1f3e2b4bef0f9eea75f;hb=773eb9791a6a77cfaf7a5f8ee1f103dd21277d36;hp=988fee78151a6e28d1d6cd6186e9033e4288ba47;hpb=80f8e65ec0bf6a02ef20a1e89e708cecefe76ad3;p=demoscenemusic.git diff --git a/src/main/java/net/pterodactylus/demoscenemusic/page/BasePage.java b/src/main/java/net/pterodactylus/demoscenemusic/page/BasePage.java index 988fee7..f6f211e 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/page/BasePage.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/page/BasePage.java @@ -18,8 +18,11 @@ package net.pterodactylus.demoscenemusic.page; import net.pterodactylus.demoscenemusic.core.Core; +import net.pterodactylus.demoscenemusic.data.User; import net.pterodactylus.util.template.Template; +import net.pterodactylus.util.template.TemplateContext; import net.pterodactylus.util.template.TemplateContextFactory; +import net.pterodactylus.util.web.RedirectException; import net.pterodactylus.util.web.TemplatePage; /** @@ -29,6 +32,8 @@ import net.pterodactylus.util.web.TemplatePage; */ public class BasePage extends TemplatePage { + private final Core core; + /** * @param path * @param contentType @@ -36,8 +41,42 @@ public class BasePage extends TemplatePage { * @param template */ public BasePage(Core core, TemplateContextFactory templateContextFactory, Template template, String pageName) { - super(pageName, "text/html", templateContextFactory, template); + super(pageName, "text/html; charset=utf-8", templateContextFactory, template); + this.core = core; } -} + /** + * @return the core + */ + public Core getCore() { + return core; + } + + /** + * {@inheritDoc} + */ + @Override + protected void processTemplate(TemplateContext templateContext, ServletRequest request) throws RedirectException { + super.processTemplate(templateContext, request); + User currentUser = (User) request.getServletRequest().getSession().getAttribute("currentUser"); + templateContext.set("currentUser", currentUser); + templateContext.set("core", getCore()); + templateContext.set("dataManager", getCore().getDataManager()); + int requiredUserLevel = getRequiredUserLevel(); + if (((currentUser == null) && (requiredUserLevel > 0)) || ((currentUser != null) && (requiredUserLevel > currentUser.getLevel()))) { + throw new RedirectException("login"); + } + } + /** + * Returns the {@link User#getLevel() user level} that is at least required + * to access the given page. If the returned level is {@code 0} (or + * smaller), users don’t have to be logged in. + * + * @return The lowest user level required to view this page + */ + protected int getRequiredUserLevel() { + return 0; + } + +}