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