001/*
002 * Copyright 2014 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 org.gbif.api.vocabulary.UserRole;
019
020import java.util.Date;
021import java.util.Map;
022import java.util.Set;
023import javax.annotation.Nullable;
024import javax.validation.constraints.NotNull;
025import javax.validation.constraints.Pattern;
026import javax.validation.constraints.Size;
027
028import com.google.common.base.Objects;
029import com.google.common.collect.Maps;
030import com.google.common.collect.Sets;
031import org.codehaus.jackson.annotate.JsonIgnore;
032
033/**
034 * A GBIF user account registered in the drupal user database.
035 * @Deprecated replaced by {@link GbifUser}
036 */
037@Deprecated
038public class User {
039
040  private static final String EMAIL_PATTERN =
041                "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
042                + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
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 = Sets.newHashSet();
051  private Date lastLogin;
052  // Note: Settings was introduced in the system developed to replace Drupal
053  private Map<String, String> settings = Maps.newHashMap();
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  @Pattern(regexp = EMAIL_PATTERN)
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  /**
085   * The unique, immutable drupal user account name.
086   * This name should be used for referring to a user.
087   * The account name is made of ASCII lower case alphanumerics, underscore, dash or dots and is in particular void
088   * of whitespace.
089   */
090  @NotNull
091  @Pattern(regexp = "^[a-z0-9_.-]+$")
092  @Size(min = 3, max = 64)
093  public String getUserName() {
094    return userName;
095  }
096
097  public void setUserName(String userName) {
098    this.userName = userName;
099  }
100
101  /**
102   * @return the first name of a person
103   */
104  @NotNull
105  public String getFirstName() {
106    return firstName;
107  }
108
109  public void setFirstName(String firstName) {
110    this.firstName = firstName;
111  }
112
113  /**
114   * @return the last name of the user
115   */
116  @NotNull
117  public String getLastName() {
118    return lastName;
119  }
120
121  public void setLastName(String lastName) {
122    this.lastName = lastName;
123  }
124
125  /**
126   * @return the first and last name of the user concatenated with a space
127   */
128  @NotNull
129  @JsonIgnore
130  public String getName() {
131    return firstName + " " + lastName;
132  }
133
134  /**
135   * @return the drupal hashed version of the user password.
136   */
137  public String getPasswordHash() {
138    return passwordHash;
139  }
140
141  public void setPasswordHash(String passwordHash) {
142    this.passwordHash = passwordHash;
143  }
144
145  @NotNull
146  public Set<UserRole> getRoles() {
147    return roles;
148  }
149
150  public void setRoles(Set<UserRole> roles) {
151    this.roles = roles;
152  }
153
154  public void addRole(UserRole role) {
155    roles.add(role);
156  }
157
158  /**
159   * Checks if the user has the given user role.
160   * @param role
161   *
162   * @return true if the user has the requested role
163   */
164  public boolean hasRole(UserRole role) {
165    if (role != null && roles.contains(role)) {
166      return true;
167    }
168    return false;
169  }
170
171  public boolean isAdmin() {
172    return roles.contains(UserRole.ADMIN);
173  }
174
175
176  /**
177   * Gets the settings which may be empty but never null.
178   * @return
179   */
180  @NotNull
181  public Map<String, String> getSettings() {
182    return settings;
183  }
184
185  /**
186   * Sets the settings object, setting an empty map if null is provided.
187   */
188  public void setSettings(Map<String, String> settings) {
189    // safeguard against misuse to avoid NPE
190    this.settings = settings == null ? Maps.<String,String>newHashMap() : settings;
191  }
192
193  @Override
194  public boolean equals(Object obj) {
195    if (this == obj) {
196      return true;
197    }
198    if (!(obj instanceof User)) {
199      return false;
200    }
201
202    User that = (User) obj;
203    return Objects.equal(this.key, that.key)
204           && Objects.equal(this.userName, that.userName)
205           && Objects.equal(this.firstName, that.firstName)
206           && Objects.equal(this.lastName, that.lastName)
207           && Objects.equal(this.email, that.email)
208           && Objects.equal(this.roles, that.roles)
209           && Objects.equal(this.lastLogin, that.lastLogin)
210           && Objects.equal(this.passwordHash, that.passwordHash)
211           && Objects.equal(this.settings, that.settings);
212  }
213
214  @Override
215  public int hashCode() {
216    return Objects.hashCode(key, userName, firstName, lastName, email, roles, lastLogin, passwordHash, settings);
217  }
218
219  @Override
220  public String toString() {
221    return Objects.toStringHelper(this)
222      .add("key", key)
223      .add("accountName", userName)
224      .add("firstName", firstName)
225      .add("lastName", lastName)
226      .add("email", email)
227      .add("roles", roles)
228      .add("lastLogin", lastLogin)
229      .add("passwordHash", passwordHash)
230      .add("settings", settings)
231      .toString();
232  }
233
234}