From: David ‘Bombe’ Roden Date: Wed, 25 Jul 2012 20:18:51 +0000 (+0200) Subject: Add minimum user level required to view pages. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=8502cb24d28777f25f91af84c77c6d250c40716a;p=demoscenemusic.git Add minimum user level required to view pages. --- diff --git a/src/main/java/net/pterodactylus/demoscenemusic/page/BasePage.java b/src/main/java/net/pterodactylus/demoscenemusic/page/BasePage.java index c24e6af..65505fa 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/page/BasePage.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/page/BasePage.java @@ -18,6 +18,7 @@ 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; @@ -57,7 +58,23 @@ public class BasePage extends TemplatePage { @Override protected void processTemplate(TemplateContext templateContext, ServletRequest request) throws RedirectException { super.processTemplate(templateContext, request); - templateContext.set("currentUser", request.getServletRequest().getSession().getAttribute("currentUser")); + User currentUser = (User) request.getServletRequest().getSession().getAttribute("currentUser"); + templateContext.set("currentUser", currentUser); + 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; } }