View Javadoc
1   package fr.ifremer.quadrige3.core.security;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 Client Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
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   * @author peck7 on 30/06/2017.
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              // Add other privileges
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                          // Ignore this privilege
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 }