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  
26  import fr.ifremer.quadrige3.core.dao.administration.user.PrivilegeCode;
27  import org.springframework.security.core.GrantedAuthority;
28  
29  import java.util.Objects;
30  
31  import static org.nuiton.i18n.I18n.n;
32  import static org.nuiton.i18n.I18n.t;
33  
34  /**
35   * <p>QuadrigeUserAuthority class.</p>
36   *
37   * @author Ludovic Pecquot <ludovic.pecquot@e-is.pro>
38   */
39  public class QuadrigeUserAuthority implements GrantedAuthority, Comparable<QuadrigeUserAuthority> {
40  
41      private static final long serialVersionUID = -5523288605119935110L;
42  
43      private static final String ROLE_ADMIN = "ROLE_ADMIN";
44      private static final int LEVEL_ADMIN = 4;
45  
46      private static final String ROLE_LOCAL_ADMIN = "ROLE_LOCAL_ADMIN";
47      private static final int LEVEL_LOCAL_ADMIN = 3;
48  
49      private static final String ROLE_QUALIFIER = "ROLE_QUALIFIER";
50      private static final int LEVEL_QUALIFIER = 2;
51  
52      private static final String ROLE_VALIDATOR = "ROLE_VALIDATOR";
53      private static final int LEVEL_VALIDATOR = 1;
54  
55      /** Constant <code>ADMIN</code> */
56      public static final QuadrigeUserAuthority ADMIN = new QuadrigeUserAuthority(PrivilegeCode.REFERENTIAL_ADMINISTRATOR.value());
57      /** Constant <code>LOCAL_ADMIN</code> */
58      public static final QuadrigeUserAuthority LOCAL_ADMIN = new QuadrigeUserAuthority(PrivilegeCode.LOCAL_ADMINISTRATOR.value());
59      /** Constant <code>VALIDATOR</code> */
60      public static final QuadrigeUserAuthority VALIDATOR = new QuadrigeUserAuthority(PrivilegeCode.VALIDATOR.value());
61      /** Constant <code>QUALIFIER</code> */
62      public static final QuadrigeUserAuthority QUALIFIER = new QuadrigeUserAuthority(PrivilegeCode.QUALIFIER.value());
63      /** Constant <code>USER</code> */
64  
65      static {
66          n("quadrige3.security.authority.ROLE_ADMIN");
67          n("quadrige3.security.authority.ROLE_LOCAL_ADMIN");
68          n("quadrige3.security.authority.ROLE_QUALIFIER");
69          n("quadrige3.security.authority.ROLE_VALIDATOR");
70          n("quadrige3.security.authority.ROLE_USER");
71      }
72  
73      private final String authority;
74  
75      private final int authorityLevel;
76  
77      /**
78       * <p>Constructor for QuadrigeUserAuthority.</p>
79       *
80       * @param privilegeCd a {@link String} object.
81       */
82      public QuadrigeUserAuthority(String privilegeCd) {
83          switch (PrivilegeCode.fromValue(privilegeCd)) {
84              case REFERENTIAL_ADMINISTRATOR:
85                  authority = ROLE_ADMIN;
86                  authorityLevel = LEVEL_ADMIN;
87                  break;
88              case LOCAL_ADMINISTRATOR:
89                  authority = ROLE_LOCAL_ADMIN;
90                  authorityLevel = LEVEL_LOCAL_ADMIN;
91                  break;
92              case QUALIFIER:
93                  authority = ROLE_QUALIFIER;
94                  authorityLevel = LEVEL_QUALIFIER;
95                  break;
96              case VALIDATOR:
97                  authority = ROLE_VALIDATOR;
98                  authorityLevel = LEVEL_VALIDATOR;
99                  break;
100             default:
101                 throw new IllegalArgumentException(String.format("Invalid privilege code : '%s'", privilegeCd));
102         }
103     }
104 
105     /** {@inheritDoc} */
106     @Override
107     public String getAuthority() {
108         return authority;
109     }
110 
111     /** {@inheritDoc} */
112     @Override
113     public int hashCode() {
114         int hash = 5;
115         hash = 13 * hash + Objects.hashCode(this.authority);
116         hash = 13 * hash + this.authorityLevel;
117         return hash;
118     }
119 
120     /** {@inheritDoc} */
121     @Override
122     public boolean equals(Object obj) {
123         if (obj == null) {
124             return false;
125         }
126         if (getClass() != obj.getClass()) {
127             return false;
128         }
129         final QuadrigeUserAuthority other = (QuadrigeUserAuthority) obj;
130         return Objects.equals(this.authority, other.authority) && this.authorityLevel == other.authorityLevel;
131     }
132 
133     /** {@inheritDoc} */
134     @Override
135     public int compareTo(QuadrigeUserAuthority o) {
136         return Integer.compare(this.authorityLevel, o.authorityLevel);
137     }
138 
139     /** {@inheritDoc} */
140     @Override
141     public String toString() {
142         return t("quadrige3.security.authority." + getAuthority());
143     }
144 }