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 org.gbif.api.model.registry.PostPersist;
019import org.gbif.api.model.registry.PrePersist;
020
021import java.util.Date;
022import java.util.HashMap;
023import java.util.HashSet;
024import java.util.Objects;
025import java.util.StringJoiner;
026
027import javax.annotation.Nullable;
028import javax.validation.constraints.NotNull;
029import javax.validation.constraints.Null;
030
031import com.fasterxml.jackson.annotation.JsonIgnore;
032
033/**
034 * A GBIF user account registered in the user Identity database (previously Drupal).
035 * This class is the replacement of {@link User}.
036 */
037@SuppressWarnings("unused")
038public class GbifUser extends AbstractGbifUser {
039
040  protected Integer key;
041
042  private String passwordHash;
043  private Date lastLogin;
044
045  public GbifUser() {
046  }
047
048  public GbifUser(GbifUser another) {
049    this.key = another.key;
050    this.passwordHash = another.passwordHash;
051    this.lastLogin = another.lastLogin;
052    this.userName = another.userName;
053    this.firstName = another.firstName;
054    this.lastName = another.lastName;
055    this.email = another.email;
056    this.roles = new HashSet<>(another.roles);
057    this.settings = new HashMap<>(another.settings);
058    this.systemSettings = new HashMap<>(another.systemSettings);
059    this.deleted = another.deleted;
060  }
061
062  @Null(groups = {PrePersist.class})
063  @NotNull(groups = {PostPersist.class})
064  public Integer getKey() {
065    return key;
066  }
067
068  public void setKey(Integer key) {
069    this.key = key;
070  }
071
072  @Nullable
073  public Date getLastLogin() {
074    return lastLogin;
075  }
076
077  public void setLastLogin(Date lastLogin) {
078    this.lastLogin = lastLogin;
079  }
080
081  /**
082   * @return the hashed version of the user password.
083   */
084  @NotNull
085  @JsonIgnore
086  public String getPasswordHash() {
087    return passwordHash;
088  }
089
090  public void setPasswordHash(String passwordHash) {
091    this.passwordHash = passwordHash;
092  }
093
094  @Override
095  public boolean equals(Object o) {
096    if (this == o) {
097      return true;
098    }
099    if (o == null || getClass() != o.getClass()) {
100      return false;
101    }
102    if (!super.equals(o)) {
103      return false;
104    }
105    GbifUser gbifUser = (GbifUser) o;
106    return Objects.equals(key, gbifUser.key) &&
107      Objects.equals(lastLogin, gbifUser.lastLogin);
108  }
109
110  @Override
111  public int hashCode() {
112    return Objects.hash(super.hashCode(), key, lastLogin);
113  }
114
115  @Override
116  public String toString() {
117    return new StringJoiner(", ", GbifUser.class.getSimpleName() + "[", "]")
118      .add("key=" + key)
119      .add("lastLogin=" + lastLogin)
120      .add("userName='" + userName + "'")
121      .add("firstName='" + firstName + "'")
122      .add("lastName='" + lastName + "'")
123      .add("email='" + email + "'")
124      .add("roles=" + roles)
125      .add("settings=" + settings)
126      .add("systemSettings=" + systemSettings)
127      .add("deleted=" + deleted)
128      .toString();
129  }
130}