Corrected page numbering.
[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 package org.wamblee.photos.security;
17
18 import javax.persistence.DiscriminatorValue;
19 import javax.persistence.Entity;
20
21 import org.wamblee.photos.wicket.BasePage;
22 import org.wamblee.security.authorization.AllOperation;
23 import org.wamblee.security.authorization.AuthorizationResult;
24 import org.wamblee.security.authorization.IsaOperationCondition;
25 import org.wamblee.security.authorization.RegexpPathCondition;
26 import org.wamblee.security.authorization.UrlAuthorizationRule;
27 import org.wamblee.security.authorization.UserCondition;
28
29 /**
30  * AUthorization rule for pages.
31  */
32 @Entity
33 @DiscriminatorValue("PAGE")
34 public class PageAuthorizationRule extends UrlAuthorizationRule {
35
36     /**
37      * Type-safe construction of page authorization rule.
38      *
39      * @param aResult        Result.
40      * @param aUserCondition User condition.
41      * @param aPageList      A list of page names.
42      */
43     public PageAuthorizationRule(AuthorizationResult aResult, UserCondition aUserCondition,
44             Class<? extends BasePage>... aPageList) {
45         super(aResult, aUserCondition, new RegexpPathCondition(getPageRegex(aPageList)), BasePage.class,
46                 new IsaOperationCondition(AllOperation.class));
47     }
48
49     /**
50      * Converts a list of page names into a regular expression for the pages.
51      *
52      * @param aPageList List of pages.
53      * @return Regexp matching any of the given pagenames.
54      */
55     private static String getPageRegex(Class<? extends BasePage>[] aPageList) {
56         String result = "";
57         for (int i = 0; i < aPageList.length; i++) {
58             if (result.length() == 0) {
59                 result = "/" + aPageList[i].getSimpleName();
60             } else {
61                 result = result + "|/" + aPageList[i].getSimpleName();
62             }
63         }
64         return result;
65     }
66
67     /**
68      * Constructor for OR mapping.
69      */
70     protected PageAuthorizationRule() {
71         super();
72     }
73
74     /*
75      * (non-Javadoc)
76      *
77      * @see
78      * org.wamblee.security.authorization.UrlAuthorizationRule#getResourcePath
79      * (java.lang.Object)
80      */
81     @Override
82     protected String getResourcePath(Object aResource) {
83         BasePage page = (BasePage) aResource;
84         return "/" + page.getClass().getSimpleName();
85     }
86 }