dde authorization rules and authorized album.
[photos] / src / main / java / org / wamblee / photos / security / PageAuthorizationRule.java
1 /*
2  * Copyright 2005 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.wamblee.photos.security;
18
19 import javax.persistence.DiscriminatorValue;
20 import javax.persistence.Entity;
21
22 import org.wamblee.photos.wicket.BasePage;
23 import org.wamblee.security.authorization.AllOperation;
24 import org.wamblee.security.authorization.AuthorizationResult;
25 import org.wamblee.security.authorization.IsaOperationCondition;
26 import org.wamblee.security.authorization.RegexpPathCondition;
27 import org.wamblee.security.authorization.UrlAuthorizationRule;
28 import org.wamblee.security.authorization.UserCondition;
29
30 /**
31  * AUthorization rule for pages.
32  */
33 @Entity
34 @DiscriminatorValue("PAGE")
35 public class PageAuthorizationRule extends UrlAuthorizationRule {
36
37     /**
38      * Type-safe construction of page authorization rule.
39      * 
40      * @param aResult
41      *            Result.
42      * @param aUserCondition
43      *            User condition.
44      * @param aPageList
45      *            A list of page names.
46      */
47     public PageAuthorizationRule(AuthorizationResult aResult,
48         UserCondition aUserCondition, Class<? extends BasePage>... aPageList) {
49         super(aResult, aUserCondition, new RegexpPathCondition(
50             getPageRegex(aPageList)), BasePage.class,
51             new IsaOperationCondition(AllOperation.class));
52     }
53
54     /**
55      * Converts a list of page names into a regular expression for the pages.
56      * 
57      * @param aPageList
58      *            List of pages.
59      * @return Regexp matching any of the given pagenames.
60      */
61     private static String getPageRegex(Class<? extends BasePage>[] aPageList) {
62         String result = "";
63         for (int i = 0; i < aPageList.length; i++) {
64             if (result.length() == 0) {
65                 result = "/" + aPageList[i].getSimpleName();
66             } else {
67                 result = result + "|/" + aPageList[i].getSimpleName();
68             }
69         }
70         return result;
71     }
72
73     /**
74      * Constructor for OR mapping.
75      */
76     protected PageAuthorizationRule() {
77         super();
78     }
79
80     /* (non-Javadoc)
81      * @see org.wamblee.security.authorization.UrlAuthorizationRule#getResourcePath(java.lang.Object)
82      */
83     @Override
84     protected String getResourcePath(Object aResource) {
85         BasePage page = (BasePage) aResource;
86         return "/" + page.getClass().getSimpleName();
87     }
88 }