View Javadoc

1   /*
2   jGuard is a security framework based on top of jaas (java authentication and authorization security).
3   it is written for web applications, to resolve simply, access control problems.
4   version $Name$
5   http://sourceforge.net/projects/jguard/
6   
7   Copyright (C) 2004  Charles GAY
8   
9   This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13  
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  Lesser General Public License for more details.
18  
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  
23  
24  jGuard project home page:
25  http://sourceforge.net/projects/jguard/
26  
27   */
28  package net.sf.jguard.core.organization;
29  
30  import java.security.Principal;
31  import java.util.Collection;
32  import java.util.HashSet;
33  import java.util.Iterator;
34  import java.util.Set;
35  
36  import javax.security.auth.Subject;
37  
38  import net.sf.jguard.core.authentication.AuthenticationException;
39  import net.sf.jguard.core.authentication.credentials.JGuardCredential;
40  import net.sf.jguard.core.authentication.manager.AuthenticationManager;
41  import net.sf.jguard.core.principals.BasePrincipal;
42  import net.sf.jguard.core.principals.PrincipalUtils;
43  import net.sf.jguard.core.provisioning.SubjectTemplate;
44  
45  /**
46   * an organization which can own one {@link net.sf.jguard.core.provisioning.SubjectTemplate} .
47   * @author <a href="mailto:diabolo512@users.sourceforge.net">Charles Gay</a>
48   * @author <a href="mailto:tandilero@users.sourceforge.net">Maximiliano Batelli</a>
49   */
50  public class Organization implements BasePrincipal, Cloneable {
51  
52      public static final String ID = "id";
53      private AuthenticationManager authenticationManager;
54      protected SubjectTemplate subjectTemplate;
55      /**
56       * these objects are some references to principals present in the AuthenticationManager.
57       * some of them can be owned by the Organization, which implies the ability to reorganize them,
58       * but without overrule the set of permissions granted via all of its roles references.
59       */
60      protected Set principals;
61      protected Set credentials;
62      protected Long id;
63      protected Set users;
64  
65      public Organization() {
66          super();
67      }
68  
69      @Override
70      public Object clone() throws CloneNotSupportedException {
71          Organization clonedOrg = new Organization();
72  
73          Set clonedPrincipals = PrincipalUtils.clonePrincipalsSet(principals);
74          clonedOrg.setPrincipals(clonedPrincipals);
75  
76          Iterator credentialsIterator = credentials.iterator();
77          Set clonedCredentials = new HashSet();
78          while (credentialsIterator.hasNext()) {
79              JGuardCredential cred = (JGuardCredential) credentialsIterator.next();
80              clonedCredentials.add(cred.clone());
81          }
82          clonedOrg.setCredentials(clonedCredentials);
83          clonedOrg.setSubjectTemplate((SubjectTemplate) subjectTemplate.clone());
84          return clonedOrg;
85      }
86  
87      public Set getPrincipals() {
88          return principals;
89      }
90  
91      public void setPrincipals(Set principals) {
92          this.principals = principals;
93      }
94  
95      @Override
96      public boolean equals(Object organization) {
97  
98          if (!(organization instanceof Organization)) {
99              return false;
100         }
101         Organization orga = (Organization) organization;
102         Iterator itCred = this.credentials.iterator();
103         JGuardCredential idCred = null;
104         while (itCred.hasNext()) {
105             JGuardCredential cred = (JGuardCredential) itCred.next();
106             if (ID.equals(cred.getName())) {
107                 idCred = cred;
108                 break;
109             }
110         }
111 
112         if (orga.getCredentials() != null && orga.getCredentials().contains(idCred)) {
113             return true;
114         }
115 
116 
117         return false;
118 
119     }
120 
121     @Override
122     public int hashCode() {
123         int i = super.hashCode();
124         if (credentials != null) {
125             i = credentials.hashCode();
126         }
127         return i;
128     }
129 
130     public void addPrincipal(Principal principal) throws AuthenticationException {
131         this.principals.add(principal);
132     }
133 
134     public void removePrincipal(Principal principal) throws AuthenticationException {
135         //remove this Principal
136         // in the users which contains the Principal
137         Collection u = getUsers();
138         Iterator itUsers = u.iterator();
139         while (itUsers.hasNext()) {
140             Subject user = (Subject) itUsers.next();
141             Set ppals = user.getPrincipals();
142             if (ppals.contains(principal)) {
143                 ppals.remove(principal);
144             }
145         }
146         this.principals.remove(principal);
147 
148 
149     }
150 
151     public SubjectTemplate getSubjectTemplate() {
152         return subjectTemplate;
153     }
154 
155     public void setSubjectTemplate(SubjectTemplate subjectTemplate) {
156         OrganizationUtils.checkSubjectTemplatePrincipals(subjectTemplate, principals);
157         this.subjectTemplate = subjectTemplate;
158     }
159 
160     /**
161      *
162      * @param user
163      * @return created subject
164      * @throws AuthenticationException
165      */
166     public Subject createUser(SubjectTemplate user) throws AuthenticationException {
167         return authenticationManager.createUser(user, this);
168     }
169 
170     /**
171      *
172      * @param user
173      * @return created subject
174      * @throws AuthenticationException
175      */
176     public Subject createUser(Subject user) throws AuthenticationException {
177         return authenticationManager.createUser(user, this);
178     }
179 
180     /**
181      * @param cred
182      * @param user
183      * @throws AuthenticationException
184      */
185     public void updateUser(JGuardCredential cred, Subject user) throws AuthenticationException {
186         authenticationManager.updateUser(cred, user);
187     }
188 
189     public Set getUsers() {
190         return users;
191     }
192 
193     /**
194      * remove user.
195      * @param user
196      * @throws AuthenticationException
197      */
198     public void deleteUser(Subject user) throws AuthenticationException {
199         authenticationManager.deleteUser(user);
200     }
201 
202     /**
203      * add role from this application to user.
204      * @param user
205      * @param roleName
206      * @throws AuthenticationException
207      */
208     public void addPrincipalToUser(Subject user, String roleName) throws AuthenticationException {
209     }
210 
211     /**
212      * add a role from <strong>any</strong> application <strong>without</strong> check
213      * to user.
214      * @param user
215      * @param roleName
216      * @param applicationName
217      * @throws AuthenticationException
218      */
219     public void addPrincipalToUser(Subject user, String roleName, String applicationName) throws AuthenticationException {
220     }
221 
222     public Set getCredentials() {
223         return credentials;
224     }
225 
226     public void setCredentials(Set credentials) {
227         this.credentials = credentials;
228     }
229 
230     /**
231      * return the <b>unique</b> name of the organization.
232      * this name is the value of the credentrial keyed by 'id'.
233      * @return
234      */
235     public String getName() {
236         Iterator it = credentials.iterator();
237         String credentialIdValue = "";
238         while (it.hasNext()) {
239             JGuardCredential cred = (JGuardCredential) it.next();
240             if (cred.getName().equals(ID)) {
241                 credentialIdValue = (String) cred.getValue();
242                 break;
243             }
244         }
245         return credentialIdValue;
246     }
247 
248     public int compareTo(Object object) {
249         if (object == null) {
250             throw new IllegalArgumentException(" object comapred in the compareTo method of Organization class is null");
251         }
252         if (!(object instanceof Organization)) {
253             throw new IllegalArgumentException("object is not an Orgnaization instance");
254         }
255         Organization o1 = (Organization) object;
256         return getName().compareTo(o1.getName());
257     }
258 
259     public Long getId() {
260         return id;
261     }
262 
263     public void setId(Long id) {
264         this.id = id;
265     }
266 
267     public void setUsers(Set users) {
268         this.users = users;
269     }
270 }