From: David ‘Bombe’ Roden Date: Thu, 26 Apr 2012 13:40:33 +0000 (+0200) Subject: Expose “god” and “user” privileges in templates. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=d81a6b35796d897f33ca3b8298daf6328c930645;p=demoscenemusic.git Expose “god” and “user” privileges in templates. --- 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); + } + +}