1 17 18 19 20 package org.apache.lenya.ac.impl; 21 22 import java.util.Arrays ; 23 import java.util.Collection ; 24 import java.util.HashMap ; 25 import java.util.HashSet ; 26 import java.util.Map ; 27 import java.util.Set ; 28 29 import org.apache.lenya.ac.AccessControlException; 30 import org.apache.lenya.ac.Accreditable; 31 import org.apache.lenya.ac.Identity; 32 import org.apache.lenya.ac.Policy; 33 import org.apache.lenya.ac.Role; 34 35 38 public class DefaultPolicy implements Policy { 39 40 private Map accreditableToCredential = new HashMap (); 41 42 47 public void addCredential(Credential credential) { 48 assert credential != null; 49 assert !accreditableToCredential.containsKey(credential.getAccreditable()); 50 accreditableToCredential.put(credential.getAccreditable(), credential); 51 } 52 53 61 public void addRole(Accreditable accreditable, Role role) { 62 assert accreditable != null; 63 assert role != null; 64 65 Credential credential = getCredential(accreditable); 66 if (credential == null) { 67 credential = new Credential(accreditable); 68 addCredential(credential); 69 } 70 if (!credential.contains(role)) { 71 credential.addRole(role); 72 } 73 } 74 75 82 public void removeRole(Accreditable accreditable, Role role) throws AccessControlException { 83 assert accreditable != null; 84 assert role != null; 85 Credential credential = getCredential(accreditable); 86 if (credential == null) { 87 throw new AccessControlException( 88 "No credential for accreditable [" 89 + accreditable 90 + "] [" 91 + accreditableToCredential.keySet().size() 92 + "]"); 93 } 94 if (!credential.contains(role)) { 95 throw new AccessControlException( 96 "Credential for accreditable [" 97 + accreditable 98 + "] does not contain role [" 99 + role 100 + "]"); 101 } 102 credential.removeRole(role); 103 104 if (credential.isEmpty()) { 105 removeCredential(credential); 106 } 107 } 108 109 114 public Credential[] getCredentials() { 115 Collection values = accreditableToCredential.values(); 116 return (Credential[]) values.toArray(new Credential[values.size()]); 117 } 118 119 122 public Role[] getRoles(Identity identity) { 123 Accreditable[] accreditables = identity.getAccreditables(); 124 Credential[] credentials = getCredentials(); 125 126 Set roles = new HashSet (); 127 128 for (int credIndex = 0; credIndex < credentials.length; credIndex++) { 129 for (int accrIndex = 0; accrIndex < accreditables.length; accrIndex++) { 130 Credential credential = credentials[credIndex]; 131 Accreditable accreditable = accreditables[accrIndex]; 132 133 if (credential.getAccreditable().equals(accreditable)) { 134 roles.addAll(Arrays.asList(credential.getRoles())); 135 } 136 } 137 } 138 139 return (Role[]) roles.toArray(new Role[roles.size()]); 140 } 141 142 148 public Credential getCredential(Accreditable accreditable) { 149 return (Credential) accreditableToCredential.get(accreditable); 150 } 151 152 private boolean isSSL; 153 154 157 public boolean isSSLProtected() throws AccessControlException { 158 return isSSL; 159 } 160 161 166 public void setSSL(boolean ssl) { 167 this.isSSL = ssl; 168 } 169 170 173 public boolean isEmpty() throws AccessControlException { 174 return getCredentials().length == 0; 175 } 176 177 183 protected void removeCredential(Credential credential) throws AccessControlException { 184 if (!accreditableToCredential.containsValue(credential)) { 185 throw new AccessControlException("Credential [" + credential + "] does not exist!"); 186 } 187 accreditableToCredential.remove(credential.getAccreditable()); 188 } 189 190 196 public void removeRoles(Accreditable accreditable) throws AccessControlException { 197 if (accreditableToCredential.containsKey(accreditable)) { 198 Credential credential = getCredential(accreditable); 199 removeCredential(credential); 200 } 201 } 202 203 } 204 | Popular Tags |