1 17 18 19 20 package org.apache.lenya.ac.impl; 21 22 import java.util.Arrays ; 23 import java.util.HashSet ; 24 import java.util.Set ; 25 26 import org.apache.lenya.ac.AccessControlException; 27 import org.apache.lenya.ac.AccreditableManager; 28 import org.apache.lenya.ac.Identity; 29 import org.apache.lenya.ac.Policy; 30 import org.apache.lenya.ac.Role; 31 32 36 public class URLPolicy implements Policy { 37 38 44 public URLPolicy(AccreditableManager controller, String url, InheritingPolicyManager manager) { 45 assert url != null; 46 this.url = url; 47 48 assert manager != null; 49 policyManager = manager; 50 51 assert controller != null; 52 this.accreditableManager = controller; 53 } 54 55 private String url; 56 private InheritingPolicyManager policyManager; 57 private AccreditableManager accreditableManager; 58 private Policy[] policies = null; 59 60 65 protected void obtainPolicies() throws AccessControlException { 66 if (policies == null) { 67 policies = getPolicyManager().getPolicies(getAccreditableManager(), getUrl()); 68 } 69 } 70 71 static final String [] VISITOR_ROLES = { "visitor", "visit" }; 72 static final String [] ADMINISTRATOR_ROLES = { "administrator", "admin", "organize" }; 73 static final String [] AUTHOR_ROLES = { "author", "edit" }; 74 75 78 public Role[] getRoles(Identity identity) throws AccessControlException { 79 obtainPolicies(); 80 Set roles = new HashSet (); 81 82 if (isEmpty()) { 84 Role visitorRole = getVisitorRole(getAccreditableManager()); 85 if (visitorRole != null) { 86 roles.add(visitorRole); 87 } 88 } else { 89 for (int i = 0; i < policies.length; i++) { 90 addRoles(policies[i], identity, roles); 91 } 92 } 93 return (Role[]) roles.toArray(new Role[roles.size()]); 94 } 95 96 102 public static Role getVisitorRole(AccreditableManager manager) throws AccessControlException { 103 Role visitorRole = null; 104 for (int i = 0; i < VISITOR_ROLES.length; i++) { 105 Role role = manager.getRoleManager().getRole(VISITOR_ROLES[i]); 106 if (role != null) { 107 visitorRole = role; 108 } 109 } 110 return visitorRole; 111 } 112 113 119 public static Role getAdministratorRole(AccreditableManager manager) throws AccessControlException { 120 Role administratorRole = null; 121 for (int i = 0; i < ADMINISTRATOR_ROLES.length; i++) { 122 Role role = manager.getRoleManager().getRole(ADMINISTRATOR_ROLES[i]); 123 if (role != null) { 124 administratorRole = role; 125 } 126 } 127 return administratorRole; 128 } 129 130 136 public static Role getAuthorRole(AccreditableManager manager) throws AccessControlException { 137 Role administratorRole = null; 138 for (int i = 0; i < AUTHOR_ROLES.length; i++) { 139 Role role = manager.getRoleManager().getRole(AUTHOR_ROLES[i]); 140 if (role != null) { 141 administratorRole = role; 142 } 143 } 144 return administratorRole; 145 } 146 147 154 protected void addRoles(Policy policy, Identity identity, Set roles) 155 throws AccessControlException { 156 roles.addAll(Arrays.asList(policy.getRoles(identity))); 157 } 158 159 163 public String getUrl() { 164 return url; 165 } 166 167 171 public InheritingPolicyManager getPolicyManager() { 172 return policyManager; 173 } 174 175 179 public AccreditableManager getAccreditableManager() { 180 return accreditableManager; 181 } 182 183 188 public boolean isSSLProtected() throws AccessControlException { 189 obtainPolicies(); 190 191 boolean ssl = false; 192 193 int i = 0; 194 while (!ssl && i < policies.length) { 195 ssl = ssl || policies[i].isSSLProtected(); 196 i++; 197 } 198 199 return ssl; 200 } 201 202 205 public boolean isEmpty() throws AccessControlException { 206 boolean empty = true; 207 208 int i = 0; 209 while (empty && i < policies.length) { 210 empty = empty && policies[i].isEmpty(); 211 i++; 212 } 213 214 return empty; 215 } 216 217 } 218 | Popular Tags |