com.evasion.entity.security.User Maven / Gradle / Ivy
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.evasion.entity.security;
import com.evasion.EntityJPA;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.PrePersist;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
/**
*create table users(
* username varchar_ignorecase(50) not null primary key,
* password varchar_ignorecase(50) not null,
* enabled boolean not null);
*
*create table authorities (
* username varchar_ignorecase(50) not null,
* authority varchar_ignorecase(50) not null,
* constraint fk_authorities_users foreign key(username) references users(username));
* create unique index ix_auth_username on authorities (username,authority);
* @author sebastien.glon
*/
@Entity
@Table(name = "users")
@NamedQuery(name = User.FIND_ALL, query = "SELECT b FROM User b")
public class User extends EntityJPA {
private static final long serialVersionUID = 1L;
public static final String FIND_ALL = "findAllUsers";
/**
* Taille max d'un username.
*/
public static final int USERNAME_MAX_LENGHT = 50;
/**
* username de la personne.
*/
@Id
@Column(nullable = false, length = USERNAME_MAX_LENGHT)
private String username;
public static final int PASSWORD_MAX_LENGHT = 50;
/**
* Mots de passe de l'utilisateur.
*/
@Column(nullable = false, length = PASSWORD_MAX_LENGHT)
private String password;
private Boolean enabled = false;
@JoinTable(name = "user_authority",
joinColumns =
@JoinColumn(name = "username"),
inverseJoinColumns =
@JoinColumn(name = "authority_id"))
@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
private Collection authoritiesInternal;
@JoinTable(name = "group_members",
joinColumns =
@JoinColumn(name = "username"),
inverseJoinColumns =
@JoinColumn(name = "group_id"))
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
private Collection groupsInternal;
/**
* Taille max d'une adresse mail.
*/
public static final int EMAIL_MAX_LENGTH = 320;
/**
* Email de contact de la personne.
*/
@Column(nullable = false, length = EMAIL_MAX_LENGTH)
private String email;
/**
* Date de dernière connection.
*/
@Temporal(javax.persistence.TemporalType.TIMESTAMP)
@Column(nullable = true)
private Calendar lastLoginInternal;
/**
* Date de creation du compte utilisateur.
*/
@Temporal(TemporalType.TIMESTAMP)
private Calendar creationDateInternal;
/**
* Constructeur par defaut.
*/
public User() {
super();
}
/**
* Methode d'inititialisation de la date de creation d'un utilisateur.
* Cette methode est utilise en pre-persistance.
*/
@PrePersist
protected void initCreationDate() {
this.setCreationDate(new Date());
}
protected Calendar getCreationDateInternal() {
return creationDateInternal;
}
protected void setCreationDateInternal(Calendar calendar) {
this.creationDateInternal = calendar;
}
/**
* Getter de email.
* @return email.
*/
public String getEmail() {
return email;
}
/**
* Setter de email.
* @param email email utilisateur.
*/
public void setEmail(String email) {
this.email = email;
}
/**
* Getter de la date de création.
* @return date de création.
*/
public Date getCreationDate() {
return this.getCreationDateInternal().getTime();
}
/**
* Setter interne pour la persistence de la date de création.
* @param date date de création.
*/
protected void setCreationDate(Date date) {
Calendar cal = new GregorianCalendar();
cal.setTime(date);
this.setCreationDateInternal(cal);
}
/**
* Getter interne de la date de dernière connection.
* @return date de dernière connection.
*/
protected Calendar getLastLoginInternal() {
if (lastLoginInternal == null) {
lastLoginInternal = new GregorianCalendar();
}
return lastLoginInternal;
}
/**
* Setter interne de la date de dernière connection.
* @param calendar date de dernière connection.
*/
protected void setLastLoginInternal(Calendar calendar) {
this.lastLoginInternal = calendar;
}
/**
* Getter de la date de dernière connection.
* @return date de dernière connection.
*/
public Date getLastLogin() {
return this.getLastLoginInternal().getTime();
}
/**
* Setter de la date de dernière connection.
* @param date date de dernière connection.
*/
public void setLastLogin(Date date) {
Calendar cal = new GregorianCalendar();
cal.setTime(date);
this.setLastLoginInternal(cal);
}
protected Collection getAuthoritiesInternal() {
if (authoritiesInternal == null) {
authoritiesInternal = new ArrayList();
}
return authoritiesInternal;
}
protected void setAuthoritiesInternal(Collection authorities) {
this.authoritiesInternal = authorities;
}
public Collection getAuthorities() {
return Collections.unmodifiableCollection(getAuthoritiesInternal());
}
public boolean addAuthority(Authority auth) {
return this.getAuthoritiesInternal().add(auth);
}
public boolean addAllAuthority(Collection auth) {
return this.getAuthoritiesInternal().addAll(auth);
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
protected Collection getGroupsInternal() {
if (groupsInternal == null) {
groupsInternal = new ArrayList();
}
return groupsInternal;
}
protected void setGroupsInternal(Collection groups) {
groupsInternal = groups;
}
public Collection getGroups() {
return Collections.unmodifiableCollection(this.getGroupsInternal());
}
public boolean addGroup(GroupSec group) {
return getGroupsInternal().add(group);
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String getId() {
return this.getUsername();
}
public void setId(String id) {
this.setUsername(id);
}
/**
* {@inheritDoc }.
*/
@Override
public boolean equals(final Object obj) {
if (obj == null) {
return false;
}
if (this == obj) {
return true;
}
if (!( obj instanceof User )) {
return false;
}
User rhs = (User) obj;
return new EqualsBuilder().append(this.username, rhs.username).isEquals();
}
/**
* {@inheritDoc }.
*/
@Override
public final int hashCode() {
return new HashCodeBuilder(17, 37).append(this.getUsername()).toHashCode();
}
/**
* {@inheritDoc }.
*/
@Override
public final String toString() {
return "com.evasion.plugin.security.User[username=" + username
+ " password=*********"
+ " lastLogin=" + lastLoginInternal
+ " enabled=" + enabled
+ " authorities=" + authoritiesInternal
+ " groups=" + groupsInternal
+ "]";
}
}