Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package model;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.HashSet;
- import java.util.Set;
- import javax.persistence.*;
- import javax.validation.constraints.NotNull;
- import org.springframework.security.core.GrantedAuthority;
- import org.springframework.security.core.authority.SimpleGrantedAuthority;
- import org.springframework.security.core.userdetails.UserDetails;
- /**
- * Created by Karol on 24.07.2016.
- */
- @Entity(name = "users")
- @Table(name = "users")
- public class UserEntity extends AbstractEntity implements UserDetails {
- @NotNull
- private String username;
- @NotNull
- private String password;
- @ManyToMany(mappedBy = "users", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
- private Set<RoleEntity> userRoleEntity = new HashSet<>();
- public UserEntity(String username, String password, Set<RoleEntity> userRoleEntity) {
- super();
- this.username = username;
- this.password = password;
- this.userRoleEntity = userRoleEntity;
- }
- public Set<RoleEntity> getUserRoleEntity() {
- return userRoleEntity;
- }
- public void setUserRoleEntity(Set<RoleEntity> userRoleEntity) {
- this.userRoleEntity = userRoleEntity;
- }
- public UserEntity() {
- super();
- // TODO Auto-generated constructor stub
- }
- 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;
- }
- @Transient
- @Override
- public Collection<? extends GrantedAuthority> getAuthorities() {
- Collection<GrantedAuthority> authorities = new ArrayList<>();
- Set<RoleEntity> userRoleEntities = this.getUserRoleEntity();
- if (userRoleEntities != null) {
- for (RoleEntity roleEntity : userRoleEntities) {
- SimpleGrantedAuthority authority = new SimpleGrantedAuthority(roleEntity.getRoleName());
- authorities.add(authority);
- }
- }
- return authorities;
- }
- @Transient
- @Override
- public boolean isAccountNonExpired() {
- // TODO Auto-generated method stub
- return true;
- }
- @Transient
- @Override
- public boolean isAccountNonLocked() {
- // TODO Auto-generated method stub
- return true;
- }
- @Transient
- @Override
- public boolean isCredentialsNonExpired() {
- // TODO Auto-generated method stub
- return true;
- }
- @Transient
- @Override
- public boolean isEnabled() {
- // TODO Auto-generated method stub
- return true;
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- if (!super.equals(o)) return false;
- UserEntity that = (UserEntity) o;
- if (username != null ? !username.equals(that.username) : that.username != null) return false;
- return password != null ? password.equals(that.password) : that.password == null;
- }
- @Override
- public int hashCode() {
- int result = super.hashCode();
- result = 31 * result + (username != null ? username.hashCode() : 0);
- result = 31 * result + (password != null ? password.hashCode() : 0);
- return result;
- }
- }
Add Comment
Please, Sign In to add comment