View Javadoc
1   package fr.ifremer.quadrige2.core.security;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 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 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              // By default add the ROLE_USER
63              authorities.add(Quadrige2UserAuthority.USER);
64              // Add other privileges
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                          // Ignore this privilege
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 }