001/*
002 * Copyright 2020 Global Biodiversity Information Facility (GBIF)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.gbif.api.model.common;
017
018import java.util.Date;
019import java.util.HashMap;
020import java.util.HashSet;
021import java.util.Map;
022import java.util.Objects;
023import java.util.Set;
024import java.util.StringJoiner;
025
026import org.gbif.api.util.validators.email.ValidEmail;
027import org.gbif.api.vocabulary.UserRole;
028
029import com.fasterxml.jackson.annotation.JsonIgnore;
030
031import javax.annotation.Nullable;
032import javax.validation.constraints.NotNull;
033import javax.validation.constraints.Pattern;
034import javax.validation.constraints.Size;
035
036/**
037 * A GBIF user account registered in the Drupal user database.
038 *
039 * @deprecated replaced by {@link GbifUser}
040 */
041@Deprecated
042public class User {
043
044  private Integer key;
045  private String userName;
046  private String firstName;
047  private String lastName;
048  private String email;
049  private String passwordHash;
050  private Set<UserRole> roles = new HashSet<>();
051  private Date lastLogin;
052  // Note: Settings was introduced in the system developed to replace Drupal
053  private Map<String, String> settings = new HashMap<>();
054
055  @NotNull
056  public Integer getKey() {
057    return key;
058  }
059
060  public void setKey(Integer key) {
061    this.key = key;
062  }
063
064  @NotNull
065  @ValidEmail
066  public String getEmail() {
067    return email;
068  }
069
070  public void setEmail(String email) {
071    this.email = email;
072  }
073
074  @Nullable
075  public Date getLastLogin() {
076    return lastLogin;
077  }
078
079  public void setLastLogin(Date lastLogin) {
080    this.lastLogin = lastLogin;
081  }
082
083  /**
084   * The unique, immutable drupal user account name. This name should be used for referring to a
085   * user. The account name is made of ASCII lower case alphanumerics, underscore, dash or dots and
086   * is in particular void of whitespace.
087   */
088  @NotNull
089  @Pattern(regexp = "^[a-z0-9_.-]+$")
090  @Size(min = 3, max = 64)
091  public String getUserName() {
092    return userName;
093  }
094
095  public void setUserName(String userName) {
096    this.userName = userName;
097  }
098
099  /** @return the first name of a person */
100  @NotNull
101  public String getFirstName() {
102    return firstName;
103  }
104
105  public void setFirstName(String firstName) {
106    this.firstName = firstName;
107  }
108
109  /** @return the last name of the user */
110  @NotNull
111  public String getLastName() {
112    return lastName;
113  }
114
115  public void setLastName(String lastName) {
116    this.lastName = lastName;
117  }
118
119  /** @return the first and last name of the user concatenated with a space */
120  @NotNull
121  @JsonIgnore
122  public String getName() {
123    return firstName + " " + lastName;
124  }
125
126  /** @return the drupal hashed version of the user password. */
127  public String getPasswordHash() {
128    return passwordHash;
129  }
130
131  public void setPasswordHash(String passwordHash) {
132    this.passwordHash = passwordHash;
133  }
134
135  @NotNull
136  public Set<UserRole> getRoles() {
137    return roles;
138  }
139
140  public void setRoles(Set<UserRole> roles) {
141    this.roles = roles;
142  }
143
144  public void addRole(UserRole role) {
145    roles.add(role);
146  }
147
148  /**
149   * Checks if the user has the given user role.
150   *
151   * @param role
152   * @return true if the user has the requested role
153   */
154  public boolean hasRole(UserRole role) {
155    if (role != null && roles.contains(role)) {
156      return true;
157    }
158    return false;
159  }
160
161  public boolean isAdmin() {
162    return roles.contains(UserRole.ADMIN);
163  }
164
165  /**
166   * Gets the settings which may be empty but never null.
167   *
168   * @return
169   */
170  @NotNull
171  public Map<String, String> getSettings() {
172    return settings;
173  }
174
175  /** Sets the settings object, setting an empty map if null is provided. */
176  public void setSettings(Map<String, String> settings) {
177    // safeguard against misuse to avoid NPE
178    this.settings = settings == null ? new HashMap<>() : settings;
179  }
180
181  @Override
182  public boolean equals(Object o) {
183    if (this == o) {
184      return true;
185    }
186    if (o == null || getClass() != o.getClass()) {
187      return false;
188    }
189    User user = (User) o;
190    return Objects.equals(key, user.key)
191        && Objects.equals(userName, user.userName)
192        && Objects.equals(firstName, user.firstName)
193        && Objects.equals(lastName, user.lastName)
194        && Objects.equals(email, user.email)
195        && Objects.equals(passwordHash, user.passwordHash)
196        && Objects.equals(roles, user.roles)
197        && Objects.equals(lastLogin, user.lastLogin)
198        && Objects.equals(settings, user.settings);
199  }
200
201  @Override
202  public int hashCode() {
203    return Objects.hash(
204        key, userName, firstName, lastName, email, passwordHash, roles, lastLogin, settings);
205  }
206
207  @Override
208  public String toString() {
209    return new StringJoiner(", ", User.class.getSimpleName() + "[", "]")
210        .add("key=" + key)
211        .add("userName='" + userName + "'")
212        .add("firstName='" + firstName + "'")
213        .add("lastName='" + lastName + "'")
214        .add("email='" + email + "'")
215        .add("roles=" + roles)
216        .add("lastLogin=" + lastLogin)
217        .add("settings=" + settings)
218        .toString();
219  }
220}