dde authorization rules and authorized album.
[photos] / src / main / java / org / wamblee / photos / security / PageAuthorizationRule.java
diff --git a/src/main/java/org/wamblee/photos/security/PageAuthorizationRule.java b/src/main/java/org/wamblee/photos/security/PageAuthorizationRule.java
new file mode 100644 (file)
index 0000000..b32f5df
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2005 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.wamblee.photos.security;
+
+import javax.persistence.DiscriminatorValue;
+import javax.persistence.Entity;
+
+import org.wamblee.photos.wicket.BasePage;
+import org.wamblee.security.authorization.AllOperation;
+import org.wamblee.security.authorization.AuthorizationResult;
+import org.wamblee.security.authorization.IsaOperationCondition;
+import org.wamblee.security.authorization.RegexpPathCondition;
+import org.wamblee.security.authorization.UrlAuthorizationRule;
+import org.wamblee.security.authorization.UserCondition;
+
+/**
+ * AUthorization rule for pages.
+ */
+@Entity
+@DiscriminatorValue("PAGE")
+public class PageAuthorizationRule extends UrlAuthorizationRule {
+
+    /**
+     * Type-safe construction of page authorization rule.
+     * 
+     * @param aResult
+     *            Result.
+     * @param aUserCondition
+     *            User condition.
+     * @param aPageList
+     *            A list of page names.
+     */
+    public PageAuthorizationRule(AuthorizationResult aResult,
+        UserCondition aUserCondition, Class<? extends BasePage>... aPageList) {
+        super(aResult, aUserCondition, new RegexpPathCondition(
+            getPageRegex(aPageList)), BasePage.class,
+            new IsaOperationCondition(AllOperation.class));
+    }
+
+    /**
+     * Converts a list of page names into a regular expression for the pages.
+     * 
+     * @param aPageList
+     *            List of pages.
+     * @return Regexp matching any of the given pagenames.
+     */
+    private static String getPageRegex(Class<? extends BasePage>[] aPageList) {
+        String result = "";
+        for (int i = 0; i < aPageList.length; i++) {
+            if (result.length() == 0) {
+                result = "/" + aPageList[i].getSimpleName();
+            } else {
+                result = result + "|/" + aPageList[i].getSimpleName();
+            }
+        }
+        return result;
+    }
+
+    /**
+     * Constructor for OR mapping.
+     */
+    protected PageAuthorizationRule() {
+        super();
+    }
+
+    /* (non-Javadoc)
+     * @see org.wamblee.security.authorization.UrlAuthorizationRule#getResourcePath(java.lang.Object)
+     */
+    @Override
+    protected String getResourcePath(Object aResource) {
+        BasePage page = (BasePage) aResource;
+        return "/" + page.getClass().getSimpleName();
+    }
+}