From d81a6b35796d897f33ca3b8298daf6328c930645 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 26 Apr 2012 15:40:33 +0200 Subject: [PATCH] =?utf8?q?Expose=20=E2=80=9Cgod=E2=80=9D=20and=20=E2=80=9C?= =?utf8?q?user=E2=80=9D=20privileges=20in=20templates.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../demoscenemusic/core/TemplateServlet.java | 3 ++ .../demoscenemusic/template/UserAccessor.java | 53 ++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/main/java/net/pterodactylus/demoscenemusic/template/UserAccessor.java diff --git a/src/main/java/net/pterodactylus/demoscenemusic/core/TemplateServlet.java b/src/main/java/net/pterodactylus/demoscenemusic/core/TemplateServlet.java index c0edf44..a4f3aa2 100644 --- a/src/main/java/net/pterodactylus/demoscenemusic/core/TemplateServlet.java +++ b/src/main/java/net/pterodactylus/demoscenemusic/core/TemplateServlet.java @@ -40,7 +40,9 @@ import javax.servlet.http.HttpServletResponse; import net.pterodactylus.demoscenemusic.data.Artist; import net.pterodactylus.demoscenemusic.data.Style; import net.pterodactylus.demoscenemusic.data.Track; +import net.pterodactylus.demoscenemusic.data.User; import net.pterodactylus.demoscenemusic.page.ServletRequest; +import net.pterodactylus.demoscenemusic.template.UserAccessor; import net.pterodactylus.util.io.Closer; import net.pterodactylus.util.io.StreamCopier; import net.pterodactylus.util.template.ClassPathTemplateProvider; @@ -79,6 +81,7 @@ public class TemplateServlet extends HttpServlet { core = (Core) config.getServletContext().getAttribute("core"); templateContextFactory.addAccessor(Object.class, new ReflectionAccessor()); + templateContextFactory.addAccessor(User.class, new UserAccessor()); templateContextFactory.addFilter("html", new HtmlFilter()); CollectionSortFilter sortFilter = new CollectionSortFilter(); diff --git a/src/main/java/net/pterodactylus/demoscenemusic/template/UserAccessor.java b/src/main/java/net/pterodactylus/demoscenemusic/template/UserAccessor.java new file mode 100644 index 0000000..59eabb1 --- /dev/null +++ b/src/main/java/net/pterodactylus/demoscenemusic/template/UserAccessor.java @@ -0,0 +1,53 @@ +/* + * DemosceneMusic - UserAccessor.java - Copyright © 2012 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.demoscenemusic.template; + +import net.pterodactylus.demoscenemusic.data.User; +import net.pterodactylus.util.template.Accessor; +import net.pterodactylus.util.template.ReflectionAccessor; +import net.pterodactylus.util.template.TemplateContext; + +/** + * {@link Accessor} implementation that extends the {@link User} by the + * following members: + * + * + * @author David ‘Bombe’ Roden + */ +public class UserAccessor extends ReflectionAccessor { + + /** + * {@inheritDoc} + */ + @Override + public Object get(TemplateContext templateContext, Object object, String member) { + User user = (User) object; + if ("god".equals(member)) { + return user.getLevel() >= User.LEVEL_GOD; + } else if ("user".equals(member)) { + return user.getLevel() >= User.LEVEL_USER; + } + return super.get(templateContext, object, member); + } + +} -- 2.7.4