com.formkiq.server.domain.User Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of formkiq-server Show documentation
Show all versions of formkiq-server Show documentation
Server-side integration for the FormKiQ ios application
/*
* Copyright (C) 2016 FormKiQ Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.formkiq.server.domain;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.UUID;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import org.hibernate.annotations.Type;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import com.formkiq.server.domain.type.UserRole;
import com.formkiq.server.domain.type.UserStatus;
/**
* User domain.
*
*/
@Entity
@Table(name = "users")
public class User implements UserDetails {
/** serialVersionUID. */
private static final long serialVersionUID = -7371006788796125701L;
/** password column length. */
private static final int FL_PASSWORD = 255;
/** email column length. */
private static final int FL_EMAIL = 255;
/** user status column length. */
private static final int FL_USER_STATUS = 10;
/** Security Token Column Length. */
private static final int FL_RESET_TOKEN = 255;
/** identifier column. */
@Id
@Column(name = "user_id", unique = true, columnDefinition = "uuid")
@Type(type = "pg-uuid")
private UUID userid;
/** email column. */
@NotNull
@Column(name = "email", length = FL_EMAIL, nullable = false)
private String email;
/** password column. */
@Column(name = "password", length = FL_PASSWORD, nullable = true)
private String password;
/** status column. */
@NotNull
@Enumerated(EnumType.STRING)
@Column(name = "status", length = FL_USER_STATUS, nullable = false)
private UserStatus status;
/** role column. */
@NotNull
@Enumerated(EnumType.STRING)
@Column(name = "role", length = FL_USER_STATUS, nullable = false)
private UserRole role;
/** reset token column. */
@Column(name = "reset_token", length = FL_RESET_TOKEN,
nullable = true)
private String resetToken;
/** Reset Token Inserted Date column. */
@Column(name = "reset_inserted_date", nullable = true)
private Date resetInsertedDate;
/** inserted column. */
@NotNull
@Column(name = "inserted_date", nullable = false)
private Date insertedDate;
/** updated column. */
@NotNull
@Column(name = "updated_date", nullable = false)
private Date updatedDate;
/** last login date column. */
@Column(name = "lastlogin_date", nullable = true)
private Date lastLogin;
/**
* default constructor.
*/
public User() {
}
/**
* constructor.
* @param emailStr {@link String}
* @param pass {@link String}
* @param userStatus {@link UserStatus}
* @param userRole {@link UserRole}
*/
public User(final String emailStr,
final String pass, final UserStatus userStatus,
final UserRole userRole) {
this();
this.email = emailStr;
this.password = pass;
this.status = userStatus;
this.role = userRole;
}
/**
* @return {@link String}
*/
public String getEmail() {
return this.email;
}
/**
* @return {@link UUID}
*/
public UUID getUserid() {
return this.userid;
}
/**
* @return {@link String}
*/
@Override
public String getPassword() {
return this.password;
}
/**
* @return {@link UserStatus}
*/
public UserStatus getStatus() {
return this.status;
}
/**
* @param emailStr {@link String}
*/
public void setEmail(final String emailStr) {
this.email = emailStr;
}
/**
* @param user {@link UUID}
*/
public void setUserid(final UUID user) {
this.userid = user;
}
/**
* @param pass {@link String}
*/
public void setPassword(final String pass) {
this.password = pass;
}
/**
* @param userStatus {@link UserStatus}
*/
public void setStatus(final UserStatus userStatus) {
this.status = userStatus;
}
@Override
public String toString() {
return String.format("User[user_id=%s, email='%s', password='%s', "
+ "status='%s', role='%s']",
this.userid, this.email, this.password,
this.status, this.role);
}
/**
* @return {@link UserRole}
*/
public UserRole getRole() {
return this.role;
}
/**
* @param userRole {@link UserRole}
*/
public void setRole(final UserRole userRole) {
this.role = userRole;
}
@Override
public Collection< ? extends GrantedAuthority> getAuthorities() {
GrantedAuthority ga = new SimpleGrantedAuthority(getRole().name());
return Arrays.asList(ga);
}
@Override
public String getUsername() {
return getEmail();
}
@Override
public boolean isAccountNonExpired() {
return isEnabled();
}
@Override
public boolean isAccountNonLocked() {
return isEnabled();
}
@Override
public boolean isCredentialsNonExpired() {
return isEnabled();
}
@Override
public boolean isEnabled() {
return UserStatus.ACTIVE.equals(this.status);
}
/**
* @return {@link String}
*/
public String getResetToken() {
return this.resetToken;
}
/**
* @param token {@link String}
*/
public void setResetToken(final String token) {
this.resetToken = token;
}
/**
* @return {@link Date}
*/
public Date getResetInsertedDate() {
return this.resetInsertedDate != null
? (Date) this.resetInsertedDate.clone() : null;
}
/**
* @param date {@link Date}
*/
public void setResetInsertedDate(final Date date) {
this.resetInsertedDate = date != null ? (Date) date.clone() : null;
}
/**
* @return {@link Date}
*/
public Date getInsertedDate() {
return this.insertedDate != null ? (Date) this.insertedDate.clone()
: null;
}
/**
* @param date {@link Date}
*/
public void setInsertedDate(final Date date) {
this.insertedDate = date != null ? (Date) date.clone() : null;
}
/**
* @return {@link Date}
*/
public Date getUpdatedDate() {
return this.updatedDate != null ? (Date) this.updatedDate.clone()
: null;
}
/**
* @param date {@link Date}
*/
public void setUpdatedDate(final Date date) {
this.updatedDate = date != null ? (Date) date.clone() : null;
}
/**
* @return {@link Date}
*/
public Date getLastLogin() {
return this.lastLogin != null ? (Date) this.lastLogin.clone()
: null;
}
/**
* @param date {@link Date}
*/
public void setLastLogin(final Date date) {
this.lastLogin = date != null ? (Date) date.clone() : null;
}
}