View Javadoc
1   package fr.ifremer.quadrige2.synchro.server.security;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 Synchro server
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  
26  
27  
28  import com.google.common.collect.Lists;
29  import java.util.Collection;
30  import java.util.List;
31  import java.util.Set;
32  
33  import com.google.common.collect.Sets;
34  import fr.ifremer.quadrige2.core.dao.administration.user.PrivilegeCode;
35  import fr.ifremer.quadrige2.core.vo.administration.user.PrivilegeVO;
36  import org.apache.commons.collections4.CollectionUtils;
37  import org.springframework.security.core.GrantedAuthority;
38  import org.springframework.security.core.authority.SimpleGrantedAuthority;
39  
40  /**
41   * <p>Quadrige2UserDetailsImpl class.</p>
42   *
43   * @author Ludovic Pecquot <ludovic.pecquot@e-is.pro>
44   * @since 1.0
45   */
46  public class Quadrige2UserDetailsImpl implements Quadrige2UserDetails {
47  
48      private static final long serialVersionUID = 1L;
49      
50      private int userId;
51      
52      private String password = "";
53  
54      private Collection<? extends GrantedAuthority> authorities;
55  
56      /**
57       * <p>Constructor for Quadrige2UserDetailsImpl.</p>
58       *
59       * @param userId a int.
60       */
61      public Quadrige2UserDetailsImpl(int userId) {
62          this.userId = userId;
63          this.password = "";
64          this.authorities = createAllAuthorities();
65      }
66  
67      /**
68       * <p>Constructor for Quadrige2UserDetailsImpl.</p>
69       *
70       * @param userId a int.
71       * @param privileges a {@link java.util.List} object.
72       */
73      public Quadrige2UserDetailsImpl(int userId, List<PrivilegeVO> privileges) {
74          this.userId = userId;
75          this.password = "";
76          this.authorities = createAuthoritiesFromPrivileges(privileges);
77      }
78  
79      /**
80       * <p>Constructor for Quadrige2UserDetailsImpl.</p>
81       *
82       * @param userId a int.
83       * @param password a {@link java.lang.String} object.
84       */
85      public Quadrige2UserDetailsImpl(int userId, String password) {
86          this.userId = userId;
87          this.password = password;
88          this.authorities = createAllAuthorities();
89      }
90  
91      /** {@inheritDoc} */
92      @Override
93      public int getUserId() {
94          return userId;
95      }
96  
97      /** {@inheritDoc} */
98      @Override
99      public Collection<? extends GrantedAuthority> getAuthorities() {
100         return authorities;
101     }
102 
103     /** {@inheritDoc} */
104     @Override
105     public String getPassword() {
106         return password;
107     }
108 
109     /** {@inheritDoc} */
110     @Override
111     public String getUsername() {
112         return "Ldap User";
113     }
114 
115     /** {@inheritDoc} */
116     @Override
117     public boolean isAccountNonExpired() {
118         return true;
119     }
120 
121     /** {@inheritDoc} */
122     @Override
123     public boolean isAccountNonLocked() {
124         return true;
125     }
126 
127     /** {@inheritDoc} */
128     @Override
129     public boolean isCredentialsNonExpired() {
130         return true;
131     }
132 
133     /** {@inheritDoc} */
134     @Override
135     public boolean isEnabled() {
136         return true;
137     }
138 
139     /* -- Internal methods -- */
140 
141     /**
142      * <p>createAllAuthorities.</p>
143      *
144      * @return a {@link java.util.Collection} object.
145      */
146     protected Collection<? extends GrantedAuthority> createAllAuthorities() {
147         List<SimpleGrantedAuthority> authorities = Lists.newArrayList(
148                 new SimpleGrantedAuthority(Quadrige2GrantedAuthority.ROLE_ADMIN.name()),
149                 new SimpleGrantedAuthority(Quadrige2GrantedAuthority.ROLE_USER.name())
150         );
151         return authorities;
152 
153     }
154 
155     /**
156      * <p>createAuthorities.</p>
157      *
158      * @param roles a {@link java.util.Set} object.
159      * @return a {@link java.util.Collection} object.
160      */
161     protected Collection<? extends GrantedAuthority> createAuthorities(Set<String> roles) {
162         List<SimpleGrantedAuthority> authorities = Lists.newArrayListWithExpectedSize(roles.size());
163         for (String role: roles) {
164             authorities.add(new SimpleGrantedAuthority(role));
165         }
166         return authorities;
167     }
168 
169     /**
170      * <p>createAuthoritiesFromPrivileges.</p>
171      *
172      * @param privileges a {@link java.util.List} object.
173      * @return a {@link java.util.Collection} object.
174      */
175     protected Collection<? extends GrantedAuthority> createAuthoritiesFromPrivileges(List<PrivilegeVO> privileges) {
176         Set<String> roles = Sets.newHashSet();
177 
178         if (CollectionUtils.isNotEmpty(privileges)) {
179             // If has privilege "referential administrator": add the ADMIN role
180             for (PrivilegeVO privilege : privileges) {
181                 if (PrivilegeCode.REFERENTIAL_ADMINISTRATOR.getValue().equalsIgnoreCase(privilege.getPrivilegeCd())) {
182                     roles.add(Quadrige2GrantedAuthority.ROLE_ADMIN.name());
183                     break;
184                 }
185             }
186         }
187 
188         // Always add the USER role
189         roles.add(Quadrige2GrantedAuthority.ROLE_USER.name());
190 
191         return createAuthorities(roles);
192     }
193 }