Implement full access requirement in all pages.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / JsonPage.java
index 605afaf..893b7ae 100644 (file)
@@ -137,6 +137,16 @@ public abstract class JsonPage implements Page {
                return true;
        }
 
+       /**
+        * Returns whether this page requires the user to be logged in.
+        *
+        * @return {@code true} if the user needs to be logged in to use this page,
+        *         {@code false} otherwise
+        */
+       protected boolean requiresLogin() {
+               return true;
+       }
+
        //
        // PROTECTED METHODS
        //
@@ -178,12 +188,20 @@ public abstract class JsonPage implements Page {
         */
        @Override
        public Response handleRequest(Request request) {
+               if (webInterface.getCore().getPreferences().isRequireFullAccess() && !request.getToadletContext().isAllowedFullAccess()) {
+                       return new Response(401, "Not authorized", "application/json", JsonUtils.format(new JsonObject().put("success", false).put("error", "auth-required")));
+               }
                if (needsFormPassword()) {
                        String formPassword = request.getHttpRequest().getParam("formPassword");
                        if (!webInterface.getFormPassword().equals(formPassword)) {
                                return new Response(401, "Not authorized", "application/json", JsonUtils.format(new JsonObject().put("success", false).put("error", "auth-required")));
                        }
                }
+               if (requiresLogin()) {
+                       if (getCurrentSone(request.getToadletContext(), false) == null) {
+                               return new Response(401, "Not authorized", "application/json", JsonUtils.format(createErrorJsonObject("auth-required")));
+                       }
+               }
                JsonObject jsonObject = createJsonObject(request);
                return new Response(200, "OK", "application/json", JsonUtils.format(jsonObject));
        }