1 28 package net.sf.jguard.ext.organization; 29 30 import java.security.Principal ; 31 import java.util.ArrayList ; 32 import java.util.Collection ; 33 import java.util.HashSet ; 34 import java.util.Iterator ; 35 import java.util.List ; 36 import java.util.Map ; 37 import java.util.Set ; 38 import java.util.logging.Logger ; 39 40 import javax.security.auth.Subject ; 41 42 import net.sf.jguard.core.authentication.credentials.JGuardCredential; 43 import net.sf.jguard.core.principals.RolePrincipal; 44 import net.sf.jguard.ext.authentication.AuthenticationException; 45 import net.sf.jguard.ext.authentication.manager.AuthenticationManager; 46 import net.sf.jguard.ext.authentication.manager.AuthenticationManagerFactory; 47 import net.sf.jguard.ext.registration.SubjectTemplate; 48 49 50 55 public class Organization { 56 57 private static final Logger logger = Logger.getLogger(Organization.class.getName()); 58 private AuthenticationManager authenticationManager; 59 60 private Map subjectTemplates; 61 62 65 private Set principals; 66 67 70 private JGuardCredential credentialIdentity; 71 72 75 public Organization(){ 76 super(); 77 authenticationManager = AuthenticationManagerFactory.getAuthenticationManager(); 78 } 79 80 81 82 89 public void addSubjectTemplate(String stName,SubjectTemplate subjectTemplate) { 90 checkSubjectTemplatePrincipals(subjectTemplate); 91 subjectTemplates.put(stName,subjectTemplate); 92 } 93 94 public Set getPrincipals() { 95 return principals; 96 } 97 98 public void setPrincipals(Set principals) { 99 this.principals = principals; 100 } 101 102 108 private void checkSubjectTemplatePrincipals(SubjectTemplate template){ 109 Iterator itPrincipalsOwned = principals.iterator(); 110 Set globalPermissions = new HashSet (); 111 while(itPrincipalsOwned.hasNext()){ 113 RolePrincipal tempPrincipal = (RolePrincipal)itPrincipalsOwned.next(); 114 globalPermissions.addAll(tempPrincipal.getAllPermissions()); 115 } 116 117 Set genericPrincipals = template.getPrincipals(); 119 checkPrincipals(globalPermissions, genericPrincipals); 120 121 } 122 123 128 private void checkPrincipals(Set globalPermissions, Set principals) { 129 Iterator itPrincipals = principals.iterator(); 130 while(itPrincipals.hasNext()){ 131 RolePrincipal tempPrincipal = (RolePrincipal)itPrincipals.next(); 132 Set permissionsFromTemplate = tempPrincipal.getAllPermissions(); 133 if(!globalPermissions.containsAll(permissionsFromTemplate)){ 134 logger.warning(" principal called "+tempPrincipal.getLocalName()+" has been removed from the SubjectTemplate "); 136 logger.warning(" because it contains permissions not owned by this organization throw its Principals "); 137 itPrincipals.remove(); 138 } 139 140 } 141 } 142 143 150 public Subject createUser(SubjectTemplate user,String stName) throws AuthenticationException{ 151 return authenticationManager.createUser(user,(SubjectTemplate)subjectTemplates.get(stName)); 152 } 153 154 159 public void removeUser(Subject user) throws AuthenticationException{ 160 authenticationManager.deleteUser(user); 161 } 162 167 public void updateUser(JGuardCredential cred,Subject user) throws AuthenticationException{ 168 authenticationManager.updateUser(cred,user); 169 } 170 171 public Collection getUsers() throws AuthenticationException{ 172 List credentials = new ArrayList (); 173 credentials.add(credentialIdentity); 174 Collection users = authenticationManager.findUsers(credentials); 175 return users; 176 } 177 public void addPrincipal(Principal principal)throws AuthenticationException{ 178 this.principals.add(principal); 179 } 180 181 public void removePrincipal(Principal principal)throws AuthenticationException{ 182 Collection users = getUsers(); 185 Iterator itUsers = users.iterator(); 186 while(itUsers.hasNext()){ 187 Subject user = (Subject )itUsers.next(); 188 Set principals = user.getPrincipals(); 189 if(principals.contains(principal)){ 190 principals.remove(principal); 191 } 192 } 193 this.principals.remove(principal); 194 195 196 } 197 198 public JGuardCredential getCredentialIdentity() { 199 return credentialIdentity; 200 } 201 202 public void setCredentialIdentity(JGuardCredential credentialIdentity) { 203 this.credentialIdentity = credentialIdentity; 204 } 205 206 public Map getSubjectTemplates() { 207 return subjectTemplates; 208 } 209 210 public void setSubjectTemplates(Map subjectTemplates) { 211 this.subjectTemplates = subjectTemplates; 212 } 213 214 215 } 216 | Popular Tags |