From 8502cb24d28777f25f91af84c77c6d250c40716a Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 25 Jul 2012 22:18:51 +0200 Subject: [PATCH] Add minimum user level required to view pages. --- .../pterodactylus/demoscenemusic/page/BasePage.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) 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; } } -- 2.7.4