1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.user2.login;
20
21 import java.util.Collection;
22 import java.util.HashSet;
23 import java.util.Map;
24 import java.util.Set;
25
26 import org.mycore.common.MCRUserInformation;
27 import org.mycore.user2.MCRRealm;
28 import org.mycore.user2.MCRRealmFactory;
29 import org.mycore.user2.MCRUserAttributeMapper;
30 import org.mycore.user2.annotation.MCRUserAttribute;
31 import org.mycore.user2.annotation.MCRUserAttributeJavaConverter;
32 import org.mycore.user2.utils.MCRRolesConverter;
33
34
35
36
37
38 public class MCRShibbolethUserInformation implements MCRUserInformation {
39 private String userId;
40
41 private String realmId;
42
43 private Map<String, Object> attributes;
44
45 @MCRUserAttribute
46 private String realName;
47
48 private Set<String> roles = new HashSet<>();
49
50 public MCRShibbolethUserInformation(String userId, String realmId, Map<String, Object> attributes)
51 throws Exception {
52 this.userId = userId;
53 this.realmId = realmId;
54 this.attributes = attributes;
55
56 MCRUserAttributeMapper attributeMapper = MCRRealmFactory.getAttributeMapper(this.realmId);
57 if (attributeMapper != null) {
58 attributeMapper.mapAttributes(this, attributes);
59 }
60 }
61
62
63
64
65 @Override
66 public String getUserID() {
67 return userId + "@" + realmId;
68 }
69
70
71
72
73 @Override
74 public boolean isUserInRole(String role) {
75 return roles.contains(role);
76 }
77
78
79
80
81 @Override
82 public String getUserAttribute(String attribute) {
83 String key;
84 switch (attribute) {
85 case MCRUserInformation.ATT_REAL_NAME:
86 return this.realName;
87 case MCRRealm.USER_INFORMATION_ATTR:
88 return this.realmId;
89 default:
90 key = attribute;
91 break;
92 }
93
94 Object value = attributes.get(key);
95
96 return value != null ? value.toString() : null;
97 }
98
99
100
101 Collection<String> getRoles() {
102 return roles;
103 }
104
105 @MCRUserAttribute(name = "roles", separator = ";")
106 @MCRUserAttributeJavaConverter(MCRRolesConverter.class)
107 void setRoles(Collection<String> roles) {
108 this.roles.addAll(roles);
109 }
110 }