1 package fr.ifremer.quadrige2.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 Quadrige2UserDetails 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<Quadrige2UserAuthority> authorities;
45
46 public Quadrige2UserDetails(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 authorities.add(Quadrige2UserAuthority.USER);
64
65 if (CollectionUtils.isNotEmpty(privilegeCodes)) {
66 for (String privilegeCd : privilegeCodes) {
67 try {
68 Quadrige2UserAuthority newAuthority = new Quadrige2UserAuthority(privilegeCd);
69 if (!authorities.contains(newAuthority)) {
70 authorities.add(newAuthority);
71 }
72 }
73 catch(IllegalArgumentException e) {
74
75 }
76 }
77 }
78
79 }
80 return authorities;
81 }
82
83 @Override
84 public String getPassword() {
85 return cryptPassword;
86 }
87
88 @Override
89 public String getUsername() {
90 return userName;
91 }
92
93 @Override
94 public String toString() {
95 return getUsername();
96 }
97
98 public boolean isLocal() {
99 return local;
100 }
101
102 @Override
103 public boolean isAccountNonExpired() {
104 return true;
105 }
106
107 @Override
108 public boolean isAccountNonLocked() {
109 return true;
110 }
111
112 @Override
113 public boolean isCredentialsNonExpired() {
114 return true;
115 }
116
117 @Override
118 public boolean isEnabled() {
119 return true;
120 }
121 }