1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
47
48
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
57
58
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
136
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
163
164
165
166 public Subject createUser(SubjectTemplate user) throws AuthenticationException {
167 return authenticationManager.createUser(user, this);
168 }
169
170
171
172
173
174
175
176 public Subject createUser(Subject user) throws AuthenticationException {
177 return authenticationManager.createUser(user, this);
178 }
179
180
181
182
183
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
195
196
197
198 public void deleteUser(Subject user) throws AuthenticationException {
199 authenticationManager.deleteUser(user);
200 }
201
202
203
204
205
206
207
208 public void addPrincipalToUser(Subject user, String roleName) throws AuthenticationException {
209 }
210
211
212
213
214
215
216
217
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
232
233
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 }