1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package fr.cnes.doi.db;
20
21 import java.util.ArrayList;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25 import java.util.concurrent.CopyOnWriteArrayList;
26
27 import org.restlet.Application;
28 import org.restlet.Request;
29 import org.restlet.Response;
30 import org.restlet.data.ClientInfo;
31 import org.restlet.engine.security.RoleMapping;
32 import org.restlet.security.Enroler;
33 import org.restlet.security.Group;
34 import org.restlet.security.Realm;
35 import org.restlet.security.Role;
36 import org.restlet.security.SecretVerifier;
37 import org.restlet.security.User;
38
39
40
41
42
43
44
45 public final class MyMemoryRealm extends Realm {
46
47
48
49
50 private final List<RoleMapping> roleMappings = new CopyOnWriteArrayList<>();
51
52
53
54
55 private final List<Group> rootGroups = new CopyOnWriteArrayList<>();
56
57
58
59
60 private final List<User> users = new CopyOnWriteArrayList<>();
61
62
63
64
65 public MyMemoryRealm() {
66 super();
67 setVerifier(new DefaultVerifier());
68 setEnroler(new DefaultEnroler());
69 }
70
71
72
73
74
75
76
77
78
79
80
81 private void addGroups(final User user, final Set<Group> userGroups,
82 final Group currentGroup, final List<Group> stack, final boolean inheritOnly) {
83 if ((currentGroup != null) && !stack.contains(currentGroup)) {
84 stack.add(currentGroup);
85
86 if (currentGroup.getMemberUsers().contains(user)) {
87 userGroups.add(currentGroup);
88
89
90 boolean inherit = !inheritOnly
91 || currentGroup.isInheritingRoles();
92 Group group;
93
94 for (int i = stack.size() - 2; inherit && (i >= 0); i--) {
95 group = stack.get(i);
96 userGroups.add(group);
97 inherit = !inheritOnly || group.isInheritingRoles();
98 }
99 }
100
101 for (final Group group : currentGroup.getMemberGroups()) {
102 addGroups(user, userGroups, group, stack, inheritOnly);
103 }
104 }
105 }
106
107
108
109
110
111
112
113
114 public Set<Group> findGroups(final User user) {
115 return findGroups(user, true);
116 }
117
118
119
120
121
122
123
124
125
126 public Set<Group> findGroups(final User user, final boolean inheritOnly) {
127 final Set<Group> result = new HashSet<>();
128 List<Group> stack;
129
130
131 for (final Group group : getRootGroups()) {
132 stack = new ArrayList<>();
133 addGroups(user, result, group, stack, inheritOnly);
134 }
135
136 return result;
137 }
138
139
140
141
142
143
144
145
146
147 public Set<Role> findRoles(final Application application, final Group userGroup) {
148 if (application == null) {
149 throw new IllegalArgumentException(
150 "The application argument can't be null");
151 }
152
153 final Set<Role> result = new HashSet<>();
154 Object source;
155
156 for (final RoleMapping mapping : getRoleMappings()) {
157 source = mapping.getSource();
158
159 if ((userGroup != null) && userGroup.equals(source)) {
160 if (mapping.getTarget().getApplication() == application) {
161 result.add(mapping.getTarget());
162 }
163 }
164 }
165
166 return result;
167 }
168
169
170
171
172
173
174
175
176
177 public Set<Role> findRoles(final Application application, final Set<Group> userGroups) {
178 if (application == null) {
179 throw new IllegalArgumentException(
180 "The application argument can't be null");
181 }
182
183 final Set<Role> result = new HashSet<>();
184 Object source;
185
186 for (final RoleMapping mapping : getRoleMappings()) {
187 source = mapping.getSource();
188
189 if (userGroups != null && userGroups.contains(source)) {
190 if (mapping.getTarget().getApplication() == application) {
191 result.add(mapping.getTarget());
192 }
193 }
194 }
195
196 return result;
197 }
198
199
200
201
202
203
204
205
206
207 public Set<Role> findRoles(final Application application, final User user) {
208 if (application == null) {
209 throw new IllegalArgumentException(
210 "The application argument can't be null");
211 }
212
213 final Set<Role> result = new HashSet<>();
214 Object source;
215
216 for (final RoleMapping mapping : getRoleMappings()) {
217 source = mapping.getSource();
218
219 if ((user != null) && user.equals(source)) {
220 if (mapping.getTarget().getApplication() == application) {
221 result.add(mapping.getTarget());
222 }
223 }
224 }
225
226 return result;
227 }
228
229
230
231
232
233
234
235 public Set<Role> findRoles(final Group userGroup) {
236 final Set<Role> result = new HashSet<>();
237 Object source;
238
239 for (final RoleMapping mapping : getRoleMappings()) {
240 source = mapping.getSource();
241
242 if ((userGroup != null) && userGroup.equals(source)) {
243 result.add(mapping.getTarget());
244 }
245 }
246
247 return result;
248 }
249
250
251
252
253
254
255
256 public Set<Role> findRoles(final Set<Group> userGroups) {
257 final Set<Role> result = new HashSet<>();
258 Object source;
259
260 for (final RoleMapping mapping : getRoleMappings()) {
261 source = mapping.getSource();
262
263 if (userGroups != null && userGroups.contains(source)) {
264 result.add(mapping.getTarget());
265 }
266 }
267
268 return result;
269 }
270
271
272
273
274
275
276
277 public Set<Role> findRoles(final User user) {
278 final Set<Role> result = new HashSet<>();
279 Object source;
280
281 for (final RoleMapping mapping : getRoleMappings()) {
282 source = mapping.getSource();
283
284 if (user != null && user.equals(source)) {
285 result.add(mapping.getTarget());
286 }
287 }
288
289 return result;
290 }
291
292
293
294
295
296
297
298 public User findUser(final String userIdentifier) {
299 User result = null;
300 User user;
301
302 for (int i = 0; (result == null) && (i < getUsers().size()); i++) {
303 user = getUsers().get(i);
304
305 if (user.getIdentifier().equals(userIdentifier)) {
306 result = user;
307 }
308 }
309
310 return result;
311 }
312
313
314
315
316
317
318 private List<RoleMapping> getRoleMappings() {
319 return roleMappings;
320 }
321
322
323
324
325
326
327 public List<Group> getRootGroups() {
328 return rootGroups;
329 }
330
331
332
333
334
335
336 public List<User> getUsers() {
337 return users;
338 }
339
340
341
342
343
344
345
346 public void map(final Group group, final Role role) {
347 getRoleMappings().add(new RoleMapping(group, role));
348 }
349
350
351
352
353
354
355
356
357
358 public void map(final User user, final Application application, final String roleName) {
359 map(user, Role.get(application, roleName, null));
360 }
361
362
363
364
365
366
367
368 public void map(final User user, final Role role) {
369 getRoleMappings().add(new RoleMapping(user, role));
370 }
371
372
373
374
375
376
377
378 public void setRootGroups(final List<Group> rootGroups) {
379 synchronized (getRootGroups()) {
380 if (rootGroups != getRootGroups()) {
381 getRootGroups().clear();
382
383 if (rootGroups != null) {
384 getRootGroups().addAll(rootGroups);
385 }
386 }
387 }
388 }
389
390
391
392
393
394
395
396 public void setUsers(final List<User> users) {
397 synchronized (getUsers()) {
398 if (users != getUsers()) {
399 getUsers().clear();
400
401 if (users != null) {
402 getUsers().addAll(users);
403 }
404 }
405 }
406 }
407
408
409
410
411
412
413
414
415
416
417 public void unmap(final Group group, final Application application, final String roleName) {
418 unmap(group, Role.get(application, roleName, null));
419 }
420
421
422
423
424
425
426
427
428 public void unmap(final Group group, final Role role) {
429 unmap((Object) group, role);
430 }
431
432
433
434
435
436
437
438
439 private void unmap(final Object source, final Role role) {
440 RoleMapping mapping;
441
442 for (int i = getRoleMappings().size() - 1; i >= 0; i--) {
443 mapping = getRoleMappings().get(i);
444
445 if (mapping.getSource().equals(source) && mapping.getTarget().equals(role)) {
446 getRoleMappings().remove(i);
447 }
448 }
449 }
450
451
452
453
454
455
456
457
458
459
460 public void unmap(final User user, final Application application, final String roleName) {
461 unmap(user, Role.get(application, roleName, null));
462 }
463
464
465
466
467
468
469
470
471 public void unmap(final User user, final Role role) {
472 unmap((Object) user, role);
473 }
474
475
476
477
478 private class DefaultEnroler implements Enroler {
479
480
481
482
483 @Override
484 public void enrole(final ClientInfo clientInfo) {
485 final User user = findUser(clientInfo.getUser().getIdentifier());
486
487 if (user != null) {
488
489 final Set<Group> userGroups = findGroups(user);
490
491
492 final Set<Role> userRoles = findRoles(user);
493
494 for (final Role role : userRoles) {
495 clientInfo.getRoles().add(role);
496 }
497
498
499 final Set<Role> groupRoles = findRoles(userGroups);
500
501 for (final Role role : groupRoles) {
502 clientInfo.getRoles().add(role);
503 }
504 }
505 }
506 }
507
508
509
510
511
512 private class DefaultVerifier extends SecretVerifier {
513
514
515
516
517 @Override
518 protected User createUser(final String identifier, final Request request,
519 final Response response) {
520 final User result = new User(identifier);
521
522
523 final User user = findUser(identifier);
524
525 if (user != null) {
526
527 result.setEmail(user.getEmail());
528 result.setFirstName(user.getFirstName());
529 result.setLastName(user.getLastName());
530 }
531
532 return result;
533 }
534
535
536
537
538 @Override
539 public int verify(final String identifier, final char[] secret) {
540 char[] actualSecret = null;
541 final User user = findUser(identifier);
542
543 if (user != null) {
544 actualSecret = user.getSecret();
545 }
546
547 return compare(secret, actualSecret) ? RESULT_VALID : RESULT_INVALID;
548 }
549 }
550 }