1 | 0 | |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
package pt.digitalis.dif.controller.security.managers.impl; |
7 | |
|
8 | |
import java.util.HashMap; |
9 | |
import java.util.HashSet; |
10 | |
import java.util.List; |
11 | |
import java.util.Map; |
12 | |
import java.util.Set; |
13 | |
|
14 | |
import pt.digitalis.dif.controller.security.managers.IIdentityManagerPrivate; |
15 | |
import pt.digitalis.dif.controller.security.objects.IDIFGroup; |
16 | |
import pt.digitalis.dif.exception.security.IdentityManagerException; |
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | 0 | abstract public class AbstractIdentityManager implements IIdentityManagerPrivate { |
23 | |
|
24 | |
|
25 | |
final static public String N_A = "N/A"; |
26 | |
|
27 | |
|
28 | |
private List<String> managedAttributes; |
29 | |
|
30 | |
|
31 | 0 | private Map<String, Map<String, Object>> parameters = new HashMap<String, Map<String, Object>>(); |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
public void addManagedAttribute(String attribute) |
37 | |
{ |
38 | 0 | getManagedAttributes().add(attribute); |
39 | 0 | } |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
public void addManagedAttributes(String attributes) |
45 | |
{ |
46 | 0 | String[] separatedAttributes = attributes.split(",\\s*"); |
47 | 0 | for (String attribute: separatedAttributes) |
48 | |
{ |
49 | 0 | this.addManagedAttribute(attribute); |
50 | |
} |
51 | 0 | } |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
public boolean containsUserParameter(String userID, String parameterID) |
58 | |
{ |
59 | 0 | return this.parameters.containsKey(userID) ? this.parameters.get(userID).containsKey(parameterID) : false; |
60 | |
} |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public int countAllGroupsOfUser(String userId, boolean parseGroupHierarchy) throws IdentityManagerException |
67 | |
{ |
68 | 0 | int result = 0; |
69 | 0 | if (parseGroupHierarchy) |
70 | |
{ |
71 | 0 | Set<String> groups = new HashSet<String>(); |
72 | 0 | for (IDIFGroup group: this.getUserGroups(userId).values()) |
73 | |
{ |
74 | 0 | groups.add(group.getID()); |
75 | 0 | IDIFGroup parentGroup = group.getParentGroup(); |
76 | 0 | while (parentGroup != null) |
77 | |
{ |
78 | 0 | groups.add(parentGroup.getID()); |
79 | 0 | parentGroup = parentGroup.getParentGroup(); |
80 | |
} |
81 | |
} |
82 | 0 | result = groups.size(); |
83 | |
} |
84 | |
else |
85 | |
{ |
86 | 0 | result = countAllGroupsOfUser(userId); |
87 | |
} |
88 | 0 | return result; |
89 | |
} |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
abstract public List<String> gatherManagedAttributes(); |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
public List<String> getManagedAttributes() |
100 | |
{ |
101 | 0 | if (managedAttributes == null) |
102 | 0 | managedAttributes = gatherManagedAttributes(); |
103 | |
|
104 | 0 | return managedAttributes; |
105 | |
} |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
public Map<String, IDIFGroup> getUserGroups(String userID, boolean parseGroupHierarchy) |
111 | |
throws IdentityManagerException |
112 | |
{ |
113 | 0 | Map<String, IDIFGroup> result = this.getUserGroups(userID); |
114 | 0 | if (parseGroupHierarchy) |
115 | |
{ |
116 | 0 | Map<String, IDIFGroup> newGroups = new HashMap<String, IDIFGroup>(); |
117 | 0 | for (IDIFGroup group: result.values()) |
118 | |
{ |
119 | 0 | IDIFGroup parentGroup = group.getParentGroup(); |
120 | 0 | while (parentGroup != null) |
121 | |
{ |
122 | 0 | newGroups.put(parentGroup.getID(), parentGroup); |
123 | 0 | parentGroup = parentGroup.getParentGroup(); |
124 | |
} |
125 | |
} |
126 | 0 | result.putAll(newGroups); |
127 | |
} |
128 | |
|
129 | 0 | return result; |
130 | |
} |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
public Set<String> getUserGroupsIDs(String userID, boolean parseGroupHierarchy) throws IdentityManagerException |
136 | |
{ |
137 | |
|
138 | 0 | Set<String> result = new HashSet<String>(); |
139 | |
|
140 | 0 | if (parseGroupHierarchy) |
141 | |
{ |
142 | 0 | result.addAll(getUserGroups(userID, parseGroupHierarchy).keySet()); |
143 | |
} |
144 | |
else |
145 | |
{ |
146 | 0 | result.addAll(this.getUserGroupsIDs(userID)); |
147 | |
} |
148 | |
|
149 | 0 | return result; |
150 | |
} |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
public Object getUserParameter(String userID, String parameterID) |
157 | |
{ |
158 | |
|
159 | 0 | if (this.parameters.containsKey(userID) && this.parameters.get(userID).containsKey(parameterID)) |
160 | |
{ |
161 | 0 | return this.parameters.get(userID).get(parameterID); |
162 | |
} |
163 | |
else |
164 | 0 | return null; |
165 | |
} |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
public Map<String, Object> getUserParameters(String userID) |
171 | |
{ |
172 | |
|
173 | 0 | if (this.parameters.containsKey(userID)) |
174 | |
{ |
175 | 0 | return this.parameters.get(userID); |
176 | |
} |
177 | |
else |
178 | 0 | return null; |
179 | |
} |
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
public boolean isUserInGroup(String userID, String groupID, boolean parseGroupHierarchy) |
186 | |
throws IdentityManagerException |
187 | |
{ |
188 | |
boolean result; |
189 | 0 | if (parseGroupHierarchy) |
190 | |
{ |
191 | 0 | result = getUserGroupsIDs(userID, true).contains(groupID); |
192 | |
} |
193 | |
else |
194 | |
{ |
195 | 0 | result = isUserInGroup(userID, groupID); |
196 | |
} |
197 | 0 | return result; |
198 | |
} |
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
public void removeUserParameter(String userID, String parameterID) |
205 | |
{ |
206 | 0 | this.parameters.get(userID).remove(parameterID); |
207 | 0 | } |
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
public void setUserParameter(String userID, String parameterID, Object parameterValue) |
214 | |
{ |
215 | |
|
216 | |
|
217 | 0 | if (this.parameters.get(userID) == null) |
218 | 0 | this.parameters.put(userID, new HashMap<String, Object>()); |
219 | |
|
220 | |
|
221 | 0 | this.parameters.get(userID).put(parameterID, parameterValue); |
222 | 0 | } |
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
public void setUserParameters(String userID, Map<String, Object> parametersMap) |
229 | |
{ |
230 | |
|
231 | 0 | this.parameters.put(userID, parametersMap); |
232 | 0 | } |
233 | |
|
234 | |
} |