UserRole.java

  1. /*
  2.  * Copyright 2020 Global Crop Diversity Trust
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *   http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package org.gringlobal.model.security;

  17. import org.springframework.security.core.GrantedAuthority;

  18. /**
  19.  * System-wide user roles.
  20.  */
  21. public enum UserRole implements GrantedAuthority {

  22.     /** The user. */
  23.     USER("User"),
  24.     /** The web user. */
  25.     WEBUSER("Web user"),
  26.     /** The administrator. */
  27.     ADMINISTRATOR("Administrator"),
  28.     /** Everyone role. */
  29.     EVERYONE("Everyone");

  30.     /** The label. */
  31.     String label;

  32.     /**
  33.      * Instantiates a new user role.
  34.      *
  35.      * @param label the label
  36.      */
  37.     UserRole(String label) {
  38.         this.label = label;
  39.     }

  40.     /**
  41.      * Gets the label.
  42.      *
  43.      * @return the label
  44.      */
  45.     public String getLabel() {
  46.         return label;
  47.     }

  48.     /**
  49.      * Gets the by label.
  50.      *
  51.      * @param value the value
  52.      * @return the by label
  53.      */
  54.     public static UserRole getByLabel(String value) {
  55.         for (final UserRole userRole : values()) {
  56.             if (userRole.label.equals(value)) {
  57.                 return userRole;
  58.             }
  59.         }
  60.         throw new IllegalArgumentException(value);
  61.     }

  62.     /**
  63.      * Gets the name.
  64.      *
  65.      * @return the name
  66.      */
  67.     public String getName() {
  68.         return name();
  69.     }

  70.     /**
  71.      * Gets the authority.
  72.      *
  73.      * @return the authority
  74.      */
  75.     @Override
  76.     public String getAuthority() {
  77.         return "ROLE_" + getName();
  78.     }
  79. }