Java tutorial
/** * This file is part of D.A.L.G.S. * * D.A.L.G.S is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * D.A.L.G.S is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with D.A.L.G.S. If not, see <http://www.gnu.org/licenses/>. */ package es.ucm.fdi.dalgs.domain; import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import javax.persistence.CollectionTable; import javax.persistence.Column; import javax.persistence.ElementCollection; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.JoinColumn; import javax.persistence.UniqueConstraint; import javax.validation.constraints.NotNull; import org.codehaus.jackson.annotate.JsonManagedReference; import org.hibernate.validator.constraints.NotBlank; import org.hibernate.validator.constraints.NotEmpty; import org.springframework.security.core.CredentialsContainer; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; import com.fasterxml.jackson.annotation.JsonIdentityInfo; import com.fasterxml.jackson.annotation.ObjectIdGenerators; @Entity @JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "@id") @Table(name = "user") public class User implements UserDetails, CredentialsContainer, Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id_user") private Long id; @NotEmpty @NotNull @NotBlank @Column(name = "firstname") private String firstName; @NotEmpty @NotNull @NotBlank @Column(name = "lastname") private String lastName; @NotEmpty @NotNull @NotBlank @Column(name = "username", unique = true) private String username; @NotEmpty @NotNull @NotBlank @Column(name = "password") private String password; @NotEmpty @NotNull @NotBlank @Column(name = "email", unique = true) private String email; @Column(name = "fullname") private String fullName; // User Credentials private String salt; @Column(name = "enabled") private boolean enabled; private boolean accountNonExpired; private boolean accountNonLocked; private boolean credentialsNonExpired; // User Roles @ElementCollection(fetch = FetchType.EAGER) @CollectionTable(name = "user_roles", joinColumns = @JoinColumn(name = "user"), uniqueConstraints = @UniqueConstraint(columnNames = { "user", "role" })) @JsonManagedReference private Collection<UserRole> roles; // Constructor public User() { this.accountNonExpired = true; this.credentialsNonExpired = true; this.enabled = true; this.accountNonLocked = true; this.roles = new ArrayList<UserRole>(); } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((email == null) ? 0 : email.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; User other = (User) obj; if (email == null) { if (other.email != null) return false; } else if (!email.equals(other.email)) return false; return true; } @Override public String toString() { return "User [id=" + id + ", email=" + email + "]"; } @Override public void eraseCredentials() { this.password = null; this.setSalt(null); } @Override public boolean isAccountNonExpired() { return this.accountNonExpired; } @Override public boolean isAccountNonLocked() { return this.accountNonLocked; } @Override public boolean isCredentialsNonExpired() { return this.credentialsNonExpired; } @Override public boolean isEnabled() { return this.enabled; } @Override public Collection<? extends GrantedAuthority> getAuthorities() { return Collections.unmodifiableCollection(this.roles); } public String getSalt() { return salt; } public String getFullName() { return fullName; } public void setFullName(String fullName) { this.fullName = fullName;// = this.lastName + ", " + this.firstName+ // " - " + this.username; } public void setSalt(String salt) { this.salt = salt; } public Collection<UserRole> getRoles() { return roles; } public void setRoles(Collection<UserRole> roles) { this.roles = roles; } public void setEnabled(boolean enabled) { this.enabled = enabled; } public void setAccountNonExpired(boolean accountNonExpired) { this.accountNonExpired = accountNonExpired; } public void setAccountNonLocked(boolean accountNonLocked) { this.accountNonLocked = accountNonLocked; } public void setCredentialsNonExpired(boolean credentialsNonExpired) { this.credentialsNonExpired = credentialsNonExpired; } public User(String username) { super(); this.username = username; } }