1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.kuali.mobility.security.user.entity;
16
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import javax.persistence.CascadeType;
21 import javax.persistence.Column;
22 import javax.persistence.Entity;
23 import javax.persistence.FetchType;
24 import javax.persistence.GeneratedValue;
25 import javax.persistence.GenerationType;
26 import javax.persistence.Id;
27 import javax.persistence.NamedQueries;
28 import javax.persistence.NamedQuery;
29 import javax.persistence.OneToMany;
30 import javax.persistence.Table;
31 import javax.persistence.Transient;
32
33 import org.kuali.mobility.security.authn.util.AuthenticationConstants;
34 import org.kuali.mobility.security.group.api.Group;
35 import org.kuali.mobility.security.user.api.User;
36 import org.kuali.mobility.security.user.api.UserAttribute;
37
38 @NamedQueries({
39 @NamedQuery(
40 name = "User.lookupById",
41 query = "select u from User u where u.id = :id"
42 ),
43 @NamedQuery(
44 name = "User.lookupByLoginName",
45 query = "select u from User u where u.loginName = :loginName"
46 ),
47 @NamedQuery(
48 name = "User.lookupAllUsers",
49 query = "select u from User u"
50 )
51 })
52
53
54
55 @Entity(name="User")
56 @Table(name="KME_USER_T")
57 public class UserImpl implements User {
58
59 public static final String EMPTY = "";
60
61 @Id
62 @GeneratedValue(strategy = GenerationType.TABLE)
63 @Column(name="ID")
64 private Long id;
65
66 @Column(name="LOGIN_NM",unique = true)
67 private String loginName;
68
69 @Column(name="PASSWORD_X")
70 private String password;
71
72 @Column(name="DISPLAY_NM")
73 private String displayName;
74
75 @Column(name="FIRST_NM")
76 private String firstName;
77
78 @Column(name="LAST_NM")
79 private String lastName;
80
81 @Column(name="URL")
82 private String requestURL;
83
84 @Column(name="CAMPUS_ID")
85 private String viewCampus;
86
87 @Column(name="EMAIL_ADDR_X")
88 private String email;
89
90
91
92
93
94
95 @Column(name="PREF_LANG", length = 10)
96 private String preferredLanguage;
97
98
99 @Transient
100 private List<Group> groups;
101
102 @OneToMany(mappedBy="user", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
103 private List<UserAttribute> attributes;
104
105 public UserImpl() {
106 this.setAttributes(new ArrayList<UserAttribute>());
107 this.setGroups(new ArrayList<Group>());
108 }
109
110 public String getLoginName() {
111 return loginName;
112 }
113
114 public void setLoginName(String principalName) {
115 this.loginName = principalName;
116 }
117
118 public String getDisplayName() {
119 return displayName;
120 }
121
122 public void setDisplayName(String displayName) {
123 this.displayName = displayName;
124 }
125
126 public String getFirstName() {
127 return firstName;
128 }
129
130 public void setFirstName(String firstName) {
131 this.firstName = firstName;
132 }
133
134 public String getLastName() {
135 return lastName;
136 }
137
138 public void setLastName(String lastName) {
139 this.lastName = lastName;
140 }
141
142 public String getRequestURL() {
143 return requestURL;
144 }
145
146 public void setRequestURL(String requestURL) {
147 this.requestURL = requestURL;
148 }
149
150 public String getViewCampus() {
151 return viewCampus;
152 }
153
154 public void setViewCampus(String viewCampus) {
155 this.viewCampus = viewCampus;
156 }
157
158 public String getEmail() {
159 return email;
160 }
161
162 public void setEmail(String email) {
163 this.email = email;
164 }
165
166 @Override
167 public void invalidateUser() {
168 this.setLoginName(null);
169 this.setDisplayName(null);
170 this.setId(null);
171 this.setEmail(null);
172 this.setFirstName(null);
173 this.setLoginName(null);
174 this.setGroups(new ArrayList<Group>());
175 this.setAttributes(new ArrayList<UserAttribute>());
176 this.setPassword(null);
177 }
178
179 @Override
180 public boolean isPublicUser() {
181 if( this.getLoginName() == null
182 || EMPTY.equalsIgnoreCase(this.getLoginName())
183 || this.getLoginName().startsWith(AuthenticationConstants.PUBLIC_USER) )
184 {
185 return true;
186 }
187 else
188 {
189 return false;
190 }
191 }
192
193 @Override
194 public boolean isMember(String groupName) {
195 boolean isMember = false;
196 if( getGroups() != null ) {
197 for( Group g : getGroups() ) {
198 if( groupName.equalsIgnoreCase(g.getName()) ) {
199 isMember = true;
200 break;
201 }
202 }
203 }
204 return isMember;
205 }
206
207 @Override
208 public void addGroup(Group group) {
209 getGroups().add(group);
210 }
211
212 @Override
213 public boolean removeGroup(Group group) {
214 boolean didRemove = false;
215 if(getGroups().contains(group)) {
216 getGroups().remove(group);
217 }
218 return didRemove;
219 }
220
221
222
223
224 @Override
225 public boolean attributeExists(String key, String value) {
226 boolean attributeExists = false;
227 if( !getAttributes().isEmpty() ) {
228 for( UserAttribute ua : getAttributes() ) {
229 if( key.equals(ua.getAttributeName())
230 && value.equals(ua.getAttributeValue())) {
231 attributeExists = true;
232 }
233 }
234 }
235 return attributeExists;
236 }
237
238 @Override
239 public void addAttribute(String key, String value) {
240 key = key.toUpperCase();
241
242 if( !attributeExists(key,value) ) {
243 UserAttribute attribute = new UserAttribute();
244 attribute.setAttributeName(key);
245 attribute.setAttributeValue(value);
246 attribute.setUser(this);
247 this.attributes.add(attribute);
248 }
249 }
250
251 @Override
252 public void clearAttribute(String key) {
253 key = key.toUpperCase();
254 for( UserAttribute ua : getAttributes() ) {
255 if( key.equals(ua.getAttributeName()) ) {
256 ua.setAttributeValue(null);
257 }
258 }
259 }
260
261 @Override
262 public List<UserAttribute> getAttribute(String name) {
263 name = name.toUpperCase();
264 List<UserAttribute> attributesForName = new ArrayList<UserAttribute>();
265 for( UserAttribute ua : getAttributes() ) {
266 if( name.equals(ua.getAttributeName()) ) {
267 attributesForName.add(ua);
268 }
269 }
270 return attributesForName;
271 }
272
273 @Override
274 public List<String> getAttributeNames() {
275 List<String> attributeNames = new ArrayList<String>();
276 for( UserAttribute ua : getAttributes() ) {
277 if( attributeNames.contains(ua.getAttributeName()) ) {
278 continue;
279 } else {
280 attributeNames.add(ua.getAttributeName());
281 }
282 }
283 return attributeNames;
284 }
285
286 @Override
287 public List<UserAttribute> getAttributes() {
288 return attributes;
289 }
290
291 @Override
292 public void setAttributes(List<UserAttribute> attributes) {
293 this.attributes = attributes;
294 }
295
296 @Override
297 public void clearAttributes() {
298 attributes.clear();
299 }
300
301 @Override
302 public String getPreferredLanguage() {
303 return preferredLanguage;
304 }
305
306 @Override
307 public void setPreferredLanguage(String language) {
308 this.preferredLanguage = language;
309 }
310
311 @Override
312 public boolean removeAttribute(String name, String value) {
313 boolean success = false;
314 if( name != null ) {
315 name = name.toUpperCase();
316 List<UserAttribute> attributesToRemove = new ArrayList<UserAttribute>();
317 for(UserAttribute ua : getAttributes()) {
318 if(name.equalsIgnoreCase(ua.getAttributeName())) {
319 if(null==value) {
320 attributesToRemove.add(ua);
321 } else if (value.equalsIgnoreCase(ua.getAttributeValue())) {
322 attributesToRemove.add(ua);
323 }
324 }
325 }
326 if(!attributesToRemove.isEmpty()) {
327 success = attributes.removeAll(attributesToRemove);
328 }
329 }
330 return success;
331 }
332
333 @Override
334 public boolean removeAttribute(String name) {
335 return removeAttribute(name,null);
336 }
337
338
339
340
341
342
343
344 public List<Group> getGroups() {
345 return groups;
346 }
347
348 public void setGroups(List<Group> groups) {
349 this.groups = groups;
350 }
351
352 public Long getId() {
353 return id;
354 }
355
356 public void setId(Long id) {
357 this.id = id;
358 }
359
360 public String getPassword() {
361 return password;
362 }
363
364 public void setPassword(String password) {
365 this.password = password;
366 }
367 }