Implement full access requirement in all pages.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / page / FreenetTemplatePage.java
index 97c8bf3..5831a1b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * shortener - TemplatePage.java - Copyright © 2010 David Roden
+ * Sone - FreenetTemplatePage.java - Copyright © 2010 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
@@ -20,6 +20,9 @@ package net.pterodactylus.sone.web.page;
 import java.io.StringWriter;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -32,6 +35,7 @@ import freenet.clients.http.LinkEnabledCallback;
 import freenet.clients.http.PageMaker;
 import freenet.clients.http.PageNode;
 import freenet.clients.http.ToadletContext;
+import freenet.support.HTMLNode;
 
 /**
  * Base class for all {@link Page}s that are rendered with {@link Template}s and
@@ -105,6 +109,9 @@ public class FreenetTemplatePage implements Page, LinkEnabledCallback {
                        return new RedirectResponse(redirectTarget);
                }
 
+               if (isFullAccessOnly() && !request.getToadletContext().isAllowedFullAccess()) {
+                       return new Response(401, "Not authorized", "text/html", "Not authorized");
+               }
                ToadletContext toadletContext = request.getToadletContext();
                if (request.getMethod() == Method.POST) {
                        /* require form password. */
@@ -118,6 +125,12 @@ public class FreenetTemplatePage implements Page, LinkEnabledCallback {
                for (String styleSheet : getStyleSheets()) {
                        pageNode.addCustomStyleSheet(styleSheet);
                }
+               for (Map<String, String> linkNodeParameters : getAdditionalLinkNodes(request)) {
+                       HTMLNode linkNode = pageNode.headNode.addChild("link");
+                       for (Entry<String, String> parameter : linkNodeParameters.entrySet()) {
+                               linkNode.addAttribute(parameter.getKey(), parameter.getValue());
+                       }
+               }
                String shortcutIcon = getShortcutIcon();
                if (shortcutIcon != null) {
                        pageNode.addForwardLink("icon", shortcutIcon);
@@ -206,6 +219,28 @@ public class FreenetTemplatePage implements Page, LinkEnabledCallback {
                return null;
        }
 
+       /**
+        * Returns additional &lt;link&gt; nodes for the HTML’s &lt;head&gt; node.
+        *
+        * @param request
+        *            The request for which to return the link nodes
+        * @return All link nodes that should be added to the HTML head
+        */
+       protected List<Map<String, String>> getAdditionalLinkNodes(Request request) {
+               return Collections.emptyList();
+       }
+
+       /**
+        * Returns whether this page should only be allowed for requests from hosts
+        * with full access.
+        *
+        * @return {@code true} if this page should only be allowed for hosts with
+        *         full access, {@code false} to allow this page for any host
+        */
+       protected boolean isFullAccessOnly() {
+               return false;
+       }
+
        //
        // INTERFACE LinkEnabledCallback
        //
@@ -215,7 +250,7 @@ public class FreenetTemplatePage implements Page, LinkEnabledCallback {
         */
        @Override
        public boolean isEnabled(ToadletContext toadletContext) {
-               return true;
+               return !isFullAccessOnly();
        }
 
        /**