1
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 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 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 getManagedAttributes().add(attribute);
39 }
40
41
42
43
44 public void addManagedAttributes(String attributes)
45 {
46 String[] separatedAttributes = attributes.split(",\\s*");
47 for (String attribute: separatedAttributes)
48 {
49 this.addManagedAttribute(attribute);
50 }
51 }
52
53
54
55
56
57 public boolean containsUserParameter(String userID, String parameterID)
58 {
59 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 int result = 0;
69 if (parseGroupHierarchy)
70 {
71 Set<String> groups = new HashSet<String>();
72 for (IDIFGroup group: this.getUserGroups(userId).values())
73 {
74 groups.add(group.getID());
75 IDIFGroup parentGroup = group.getParentGroup();
76 while (parentGroup != null)
77 {
78 groups.add(parentGroup.getID());
79 parentGroup = parentGroup.getParentGroup();
80 }
81 }
82 result = groups.size();
83 }
84 else
85 {
86 result = countAllGroupsOfUser(userId);
87 }
88 return result;
89 }
90
91
92
93
94 abstract public List<String> gatherManagedAttributes();
95
96
97
98
99 public List<String> getManagedAttributes()
100 {
101 if (managedAttributes == null)
102 managedAttributes = gatherManagedAttributes();
103
104 return managedAttributes;
105 }
106
107
108
109
110 public Map<String, IDIFGroup> getUserGroups(String userID, boolean parseGroupHierarchy)
111 throws IdentityManagerException
112 {
113 Map<String, IDIFGroup> result = this.getUserGroups(userID);
114 if (parseGroupHierarchy)
115 {
116 Map<String, IDIFGroup> newGroups = new HashMap<String, IDIFGroup>();
117 for (IDIFGroup group: result.values())
118 {
119 IDIFGroup parentGroup = group.getParentGroup();
120 while (parentGroup != null)
121 {
122 newGroups.put(parentGroup.getID(), parentGroup);
123 parentGroup = parentGroup.getParentGroup();
124 }
125 }
126 result.putAll(newGroups);
127 }
128
129 return result;
130 }
131
132
133
134
135 public Set<String> getUserGroupsIDs(String userID, boolean parseGroupHierarchy) throws IdentityManagerException
136 {
137
138 Set<String> result = new HashSet<String>();
139
140 if (parseGroupHierarchy)
141 {
142 result.addAll(getUserGroups(userID, parseGroupHierarchy).keySet());
143 }
144 else
145 {
146 result.addAll(this.getUserGroupsIDs(userID));
147 }
148
149 return result;
150 }
151
152
153
154
155
156 public Object getUserParameter(String userID, String parameterID)
157 {
158
159 if (this.parameters.containsKey(userID) && this.parameters.get(userID).containsKey(parameterID))
160 {
161 return this.parameters.get(userID).get(parameterID);
162 }
163 else
164 return null;
165 }
166
167
168
169
170 public Map<String, Object> getUserParameters(String userID)
171 {
172
173 if (this.parameters.containsKey(userID))
174 {
175 return this.parameters.get(userID);
176 }
177 else
178 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 if (parseGroupHierarchy)
190 {
191 result = getUserGroupsIDs(userID, true).contains(groupID);
192 }
193 else
194 {
195 result = isUserInGroup(userID, groupID);
196 }
197 return result;
198 }
199
200
201
202
203
204 public void removeUserParameter(String userID, String parameterID)
205 {
206 this.parameters.get(userID).remove(parameterID);
207 }
208
209
210
211
212
213 public void setUserParameter(String userID, String parameterID, Object parameterValue)
214 {
215
216
217 if (this.parameters.get(userID) == null)
218 this.parameters.put(userID, new HashMap<String, Object>());
219
220
221 this.parameters.get(userID).put(parameterID, parameterValue);
222 }
223
224
225
226
227
228 public void setUserParameters(String userID, Map<String, Object> parametersMap)
229 {
230
231 this.parameters.put(userID, parametersMap);
232 }
233
234 }