All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.iintelligas.persist.dto.User Maven / Gradle / Ivy

The newest version!
package com.github.iintelligas.persist.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.github.iintelligas.persist.entity.RoleEntity;
import com.github.iintelligas.persist.entity.UserEntity;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class User implements UserDetails {

    private static final long serialVersionUID = 1L;

    private Long id;

    private String username;

    private String password;

    private String tempPassword;

    private String confirm;

    private String firstName;

    private String lastName;

    private String email;

    private Date lastAccessDate;

    private Date createdOn;

    private String createdBy;

    private Date updatedOn;

    private String updatedBy;

    private boolean enabled;

    private boolean tokenExpired;

    private List roles;
    
    Collection grantedAuthorities = new ArrayList<>();

    public User() {

    }

    public User(String username, String password, String firstName, String lastName, String email) {
        this.username = username;
        this.password = password;
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.tokenExpired = false;
    }

    public User(Long id, String username, String password, String firstName, String lastName, String email, Date lastAccessDate, Date createdOn, String createdBy,
                Date updatedOn, String updatedBy, boolean enabled, boolean tokenExpired) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.lastAccessDate = lastAccessDate;
        this.createdOn = createdOn;
        this.createdBy = createdBy;
        this.updatedOn = updatedOn;
        this.updatedBy = updatedBy;
        this.enabled = enabled;
        this.tokenExpired = tokenExpired;
    }

    public User(UserEntity user) {
        if(user != null) {
            this.id = user.getId();
            this.username = user.getUsername();
            this.password = user.getPassword();
            this.firstName = user.getFirstName();
            this.lastName = user.getLastName();
            this.email = user.getEmail();
            this.lastAccessDate = user.getLastAccessDate();
            this.createdOn = user.getCreatedOn();
            this.createdBy = user.getCreatedBy();
            this.updatedOn = user.getUpdatedOn();
            this.updatedBy = user.getUpdatedBy();
            this.enabled = user.getEnabled();
            this.tokenExpired = user.isTokenExpired();
            if(user.getRoles() != null)
            {
                if(this.roles == null)
                {
                    this.roles = new ArrayList<>();
                }
                for(RoleEntity role : user.getRoles())
                {
                    this.roles.add(new Role(role));
                }
            }
            
            for (GrantedAuthority authority : user.getAuthorities())
            {
                GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(authority.getAuthority());
                grantedAuthorities.add(grantedAuthority);
            }
        }
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getConfirm() {
        return confirm;
    }

    public void setConfirm(String confirm) {
        this.confirm = confirm;
    }

    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 getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getLastAccessDate() {
        return lastAccessDate;
    }

    public void setLastAccessDate(Date lastAccessDate) {
        this.lastAccessDate = lastAccessDate;
    }

    public Date getCreatedOn() {
        return createdOn;
    }

    private void setCreatedOn(Date createdOn) {
        this.createdOn = createdOn;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public Date getUpdatedOn() {
        return updatedOn;
    }

    private void setUpdatedOn(Date updatedOn) {
        this.updatedOn = updatedOn;
    }

    public String getUpdatedBy() {
        return updatedBy;
    }

    public void setUpdatedBy(String updatedBy) {
        this.updatedBy = updatedBy;
    }

    public boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public boolean isTokenExpired() {
        return tokenExpired;
    }

    public void setTokenExpired(boolean tokenExpired) {
        this.tokenExpired = tokenExpired;
    }

    public List getRoles() {
        return roles;
    }

    public void addRole( Role role) {
        if(roles == null) {
            roles = new ArrayList<>();
        }
        roles.add(role);
    }
    public void setRoles(List roles) {
        this.roles = roles;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getTempPassword() {
        return tempPassword;
    }

    public void setTempPassword(String tempPassword) {
        this.tempPassword = tempPassword;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", email='" + email + '\'' +
                ", lastAccessDate=" + lastAccessDate +
                ", createdOn=" + createdOn +
                ", createdBy='" + createdBy + '\'' +
                ", updatedOn=" + updatedOn +
                ", updatedBy='" + updatedBy + '\'' +
                ", enabled=" + enabled +
                ", tokenExpired=" + tokenExpired +
                ", roles=" + roles +
                '}';
    }

    @Override
    public Collection getAuthorities()
    {
        return grantedAuthorities;
    }

    @Override
    public boolean isAccountNonExpired()
    {
        return true;
    }

    @Override
    public boolean isAccountNonLocked()
    {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired()
    {
        return true;
    }

    @Override
    public boolean isEnabled()
    {
        return true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy