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
29
30
31
32
33
34 package info.magnolia.cms.security;
35
36 import info.magnolia.cms.security.auth.Entity;
37 import info.magnolia.cms.security.auth.GroupList;
38 import info.magnolia.cms.security.auth.RoleList;
39 import info.magnolia.context.MgnlContext;
40
41 import java.io.Serializable;
42 import java.util.Collection;
43 import java.util.Iterator;
44 import java.util.Set;
45
46 import javax.security.auth.Subject;
47
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51
52
53
54
55
56 public class ExternalUser extends AbstractUser implements Serializable {
57 private static final Logger log = LoggerFactory.getLogger(ExternalUser.class);
58
59
60
61
62 private Entity userDetails;
63
64
65
66
67 private RoleList roleList;
68
69
70
71
72 private GroupList groupList;
73
74
75
76
77 protected ExternalUser(Subject subject) {
78 final Set<Entity> principalDetails = subject.getPrincipals(Entity.class);
79 final Iterator<Entity> entityIterator = principalDetails.iterator();
80 this.userDetails = entityIterator.next();
81
82 final Set<RoleList> principalRoles = subject.getPrincipals(RoleList.class);
83 final Iterator<RoleList> roleListIterator = principalRoles.iterator();
84 this.roleList = roleListIterator.next();
85
86 final Set<GroupList> principalGroups = subject.getPrincipals(GroupList.class);
87 final Iterator<GroupList> groupListIterator = principalGroups.iterator();
88 this.groupList = groupListIterator.next();
89 }
90
91 @Override
92 public boolean hasRole(String roleName) {
93 return this.roleList.has(roleName);
94 }
95
96 @Override
97 public void removeRole(String roleName) {
98 throw new UnsupportedOperationException("not implemented for this ExternalUser");
99 }
100
101 @Override
102 public void addRole(String roleName) {
103 throw new UnsupportedOperationException("not implemented for this ExternalUser");
104 }
105
106
107
108
109
110 @Override
111 public boolean inGroup(String groupName) {
112 return this.groupList.has(groupName);
113 }
114
115
116
117
118 @Override
119 public void removeGroup(String groupName) throws UnsupportedOperationException {
120 throw new UnsupportedOperationException("not implemented for this ExternalUser");
121 }
122
123
124
125
126 @Override
127 public void addGroup(String groupName) throws UnsupportedOperationException {
128 throw new UnsupportedOperationException("not implemented for this ExternalUser");
129 }
130
131 @Override
132 public boolean isEnabled() {
133 return true;
134 }
135
136 @Override
137 public void setEnabled(boolean enabled) {
138 }
139
140 @Override
141 public String getLanguage() {
142 String language = (String) this.userDetails.getProperty(Entity.LANGUAGE);
143 if (null == language) {
144 language = MgnlContext.getSystemContext().getLocale().getLanguage();
145 }
146 return language;
147 }
148
149 @Override
150 public String getName() {
151 return (String) this.userDetails.getProperty(Entity.NAME);
152 }
153
154 @Override
155 public String getPassword() {
156 return (String) this.userDetails.getProperty(Entity.PASSWORD);
157 }
158
159 public String getEmail() {
160 return (String) this.userDetails.getProperty(Entity.EMAIL);
161 }
162
163 @Override
164 public String getProperty(String propertyName) {
165 String property = (String) this.userDetails.getProperty(propertyName);
166 if(null == property){
167 log.error("Unable to retrieve property " + propertyName + " for user " + getName());
168 }
169 return property;
170 }
171
172 @Override
173 public void setProperty(String propertyName, String value) {
174
175 throw new UnsupportedOperationException("not implemented for this ExternalUser");
176 }
177
178 @Override
179 public Collection<String> getGroups() {
180 return this.groupList.getList();
181 }
182
183 @Override
184 public Collection<String> getAllGroups() {
185 return this.getGroups();
186 }
187
188 @Override
189 public Collection<String> getRoles() {
190 return this.roleList.getList();
191 }
192
193 @Override
194 public Collection<String> getAllRoles() {
195 return this.getRoles();
196 }
197 }