167 lines
3.4 KiB
Java
Executable File
167 lines
3.4 KiB
Java
Executable File
package com.plannaplan.entities;
|
|
|
|
import java.sql.Timestamp;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import javax.persistence.Entity;
|
|
import javax.persistence.GeneratedValue;
|
|
import javax.persistence.GenerationType;
|
|
import javax.persistence.Id;
|
|
|
|
import com.plannaplan.types.UserRoles;
|
|
|
|
/**
|
|
* Entity of User grouping of state ssociated about id,name,surname,email,role,token,tokenCreatedDate
|
|
*/
|
|
|
|
@Entity
|
|
public class User {
|
|
private static final float TOKEN_EXPIRE_MINUTES = 15;
|
|
|
|
@Id
|
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
|
private Long id;
|
|
private String name;
|
|
private String surname;
|
|
private String email;
|
|
private UserRoles role;
|
|
private String token;
|
|
private Timestamp tokenUsageDate;
|
|
|
|
public User() {
|
|
}
|
|
|
|
/*
|
|
* User
|
|
*
|
|
* @param name name given to the user
|
|
* @param surname surname given to the user
|
|
* @param email mail given to the user
|
|
* @param role role given to the user
|
|
*/
|
|
public User(String name, String surname, String mail, UserRoles role) {
|
|
this.name = name;
|
|
this.surname = surname;
|
|
this.email = mail;
|
|
this.role = role;
|
|
}
|
|
|
|
/*
|
|
* getEmail
|
|
*
|
|
* @return email
|
|
*/
|
|
public String getEmail() {
|
|
return email;
|
|
}
|
|
|
|
/*
|
|
* setEmail
|
|
*
|
|
* @param email set email to the user
|
|
*/
|
|
public void setEmail(String email) {
|
|
this.email = email;
|
|
}
|
|
|
|
/*
|
|
* getTokenUsageDate
|
|
*
|
|
* @return tokenUsageDate
|
|
*/
|
|
public Timestamp getTokenUsageDate() {
|
|
return tokenUsageDate;
|
|
}
|
|
|
|
/*
|
|
* getToken
|
|
*
|
|
* @return token
|
|
*/
|
|
public String getToken() {
|
|
return token;
|
|
}
|
|
|
|
/*
|
|
* setToken
|
|
*
|
|
* @param token set token to the entity
|
|
*/
|
|
public void setToken(String token) {
|
|
this.tokenUsageDate = new Timestamp(System.currentTimeMillis());
|
|
this.token = token;
|
|
}
|
|
|
|
/* getName
|
|
*
|
|
* @return name
|
|
*/
|
|
public String getName() {
|
|
return name;
|
|
}
|
|
|
|
/* getRole
|
|
*
|
|
* @return role
|
|
*/
|
|
public UserRoles getRole() {
|
|
return role;
|
|
}
|
|
|
|
/* setRole
|
|
*
|
|
* @param role set role to the entity
|
|
*/
|
|
public void setRole(UserRoles role) {
|
|
this.role = role;
|
|
}
|
|
|
|
/*
|
|
* getSurname
|
|
* @return surname
|
|
*/
|
|
public String getSurname() {
|
|
return surname;
|
|
}
|
|
|
|
/*
|
|
* setSurname
|
|
* @param surname set surname into entity user
|
|
*/
|
|
public void setSurname(String surname) {
|
|
this.surname = surname;
|
|
}
|
|
|
|
/*
|
|
* setName
|
|
* @param name set name into entity user
|
|
*/
|
|
public void setName(String name) {
|
|
this.name = name;
|
|
}
|
|
|
|
/*
|
|
* getId
|
|
* @return id
|
|
*/
|
|
public Long getId() {
|
|
return this.id;
|
|
}
|
|
|
|
/*
|
|
* isCredentialsNonExpired
|
|
* Returns TRUE if is Credentials Non Expired in the otherwise it returns false
|
|
*/
|
|
public boolean isCredentialsNonExpired() {
|
|
final long diffInMilliseconds = Math
|
|
.abs(this.tokenUsageDate.getTime() - new Timestamp(System.currentTimeMillis()).getTime());
|
|
final long minutes = TimeUnit.MILLISECONDS.toMinutes(diffInMilliseconds);
|
|
|
|
if (minutes > TOKEN_EXPIRE_MINUTES) {
|
|
return false;
|
|
}
|
|
this.tokenUsageDate = new Timestamp(System.currentTimeMillis());
|
|
return true;
|
|
}
|
|
}
|