001package org.gbif.api.model.common;
002
003import org.gbif.api.vocabulary.UserRole;
004
005import java.security.Principal;
006
007import com.google.common.base.Preconditions;
008import com.google.common.base.Strings;
009
010/**
011 * A wrapper class for a GBIF User that exposes the unique account name as the principal name.
012 * @Deprecated replaced by {@link GbifUserPrincipal}
013 */
014@Deprecated
015public class UserPrincipal implements Principal {
016  private final User user;
017
018  public UserPrincipal(User user) {
019    Preconditions.checkNotNull(user);
020    this.user = user;
021  }
022
023  @Override
024  public String getName() {
025    return user.getUserName();
026  }
027
028  public User getUser() {
029    return user;
030  }
031
032  /**
033   * Checks if the user has the given string based role.
034   * We use strings here and not the enum to facilitate the use of the method with the standard SecurityContext
035   * which uses Strings for roles.
036   *
037   * @param role case insensitive role
038   *
039   * @return true if the user has the requested role
040   */
041  public boolean hasRole(String role) {
042    if (!Strings.isNullOrEmpty(role)) {
043      try {
044        UserRole r = UserRole.valueOf(role.toUpperCase());
045        return user.hasRole(r);
046      } catch (IllegalArgumentException e) {
047        // ignore
048      }
049    }
050    return false;
051  }
052}