1 package org.itracker.services.authentication;
2
3 import org.apache.commons.lang.StringUtils;
4 import org.itracker.model.Permission;
5 import org.itracker.model.PermissionType;
6 import org.itracker.model.User;
7 import org.itracker.model.util.UserUtilities;
8 import org.springframework.security.core.GrantedAuthority;
9 import org.springframework.security.core.authority.SimpleGrantedAuthority;
10 import org.springframework.security.core.userdetails.UserDetails;
11
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.HashSet;
15 import java.util.Set;
16
17
18 final public class ITrackerUserDetails implements UserDetails {
19 private final Set<GrantedAuthority> authorities = new HashSet<>();
20 private final boolean enabled;
21 private final String password;
22 private final String username;
23 private final boolean credentialsNonExpired;
24 private final boolean accountNonLocked;
25 private final boolean accountNonExpired;
26 private final String displayName;
27
28 public ITrackerUserDetails(User model, Collection<Permission> permissions) {
29 authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
30
31 for (Permission p : permissions) {
32 if (null == p.getProject()) {
33 authorities.add(new SimpleGrantedAuthority(p.getPermissionType().name()));
34 } else {
35 if (p.getPermissionType() == PermissionType.PRODUCT_ADMIN) {
36 authorities.add(new SimpleGrantedAuthority(p.getPermissionType().name()));
37 }
38 else if (p.getPermissionType() == PermissionType.ISSUE_VIEW_ALL) {
39 authorities.add(new SimpleGrantedAuthority(p.getPermissionType().name()));
40 }
41 authorities.add(new SimpleGrantedAuthority(p.getPermissionType().name(p.getProject())));
42 }
43 }
44
45 username = model.getLogin();
46 password = model.getPassword();
47 displayName = model.getFullName();
48 credentialsNonExpired = StringUtils.isNotEmpty(model.getPassword());
49 accountNonLocked = model.getStatus() != UserUtilities.STATUS_LOCKED;
50 accountNonExpired = model.getStatus() == UserUtilities.STATUS_ACTIVE;
51 enabled = isAccountNonExpired() && !model.isNew();
52
53 }
54
55 @Override
56 public Collection<? extends GrantedAuthority> getAuthorities() {
57 return Collections.unmodifiableCollection(authorities);
58 }
59
60 @Override
61 public String getPassword() {
62 return password;
63 }
64
65 @Override
66 public String getUsername() {
67 return username;
68 }
69
70 public String getDisplayName() {
71 return displayName;
72 }
73
74 @Override
75 public boolean isAccountNonExpired() {
76 return accountNonExpired;
77 }
78
79 @Override
80 public boolean isAccountNonLocked() {
81 return accountNonLocked;
82 }
83
84 @Override
85 public boolean isCredentialsNonExpired() {
86 return credentialsNonExpired;
87 }
88
89 @Override
90 public boolean isEnabled() {
91 return enabled;
92 }
93 }