1 package fr.ifremer.quadrige3.core.security;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import com.google.common.collect.Lists;
26 import org.apache.commons.collections4.CollectionUtils;
27 import org.apache.commons.lang3.StringUtils;
28 import org.springframework.security.core.GrantedAuthority;
29 import org.springframework.security.core.userdetails.UserDetails;
30
31 import java.util.Collection;
32 import java.util.List;
33
34
35
36
37 public class QuadrigeUserDetails implements UserDetails {
38
39 private final int userId;
40 private final String userName;
41 private final String cryptPassword;
42 private final List<String> privilegeCodes;
43 private final boolean local;
44 private List<QuadrigeUserAuthority> authorities;
45
46 public QuadrigeUserDetails(int userId, String userName, String cryptPassword, List<String> privilegeCodes, boolean local) {
47 this.userId = userId;
48 this.userName = userName;
49 this.cryptPassword = cryptPassword != null ? cryptPassword : StringUtils.EMPTY;
50 this.privilegeCodes = privilegeCodes;
51 this.local = local;
52 }
53
54 public int getUserId() {
55 return userId;
56 }
57
58 @Override
59 public Collection<? extends GrantedAuthority> getAuthorities() {
60 if (authorities == null) {
61 authorities = Lists.newArrayList();
62
63 if (CollectionUtils.isNotEmpty(privilegeCodes)) {
64 for (String privilegeCd : privilegeCodes) {
65 try {
66 QuadrigeUserAuthority newAuthority = new QuadrigeUserAuthority(privilegeCd);
67 if (!authorities.contains(newAuthority)) {
68 authorities.add(newAuthority);
69 }
70 }
71 catch(IllegalArgumentException e) {
72
73 }
74 }
75 }
76
77 }
78 return authorities;
79 }
80
81 @Override
82 public String getPassword() {
83 return cryptPassword;
84 }
85
86 @Override
87 public String getUsername() {
88 return userName;
89 }
90
91 @Override
92 public String toString() {
93 return getUsername();
94 }
95
96 public boolean isLocal() {
97 return local;
98 }
99
100 @Override
101 public boolean isAccountNonExpired() {
102 return true;
103 }
104
105 @Override
106 public boolean isAccountNonLocked() {
107 return true;
108 }
109
110 @Override
111 public boolean isCredentialsNonExpired() {
112 return true;
113 }
114
115 @Override
116 public boolean isEnabled() {
117 return true;
118 }
119 }