package com.worth.ifs.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.springframework.security.crypto.password.StandardPasswordEncoder;

import javax.persistence.*;
import java.util.*;

/**
 * User object for saving user details to the db. This is used so we can check authentication and authorization.
 */

@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
public class User {
    private static final CharSequence PASSWORD_SECRET = "a02214f47a45171c";

    public User(Long id, String name, String email, String password, String token, String imageUrl,
                List<UserApplicationRole> userApplicationRoles) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.password = password;
        this.token = token;
        this.imageUrl = imageUrl;
        this.userApplicationRoles = userApplicationRoles;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToMany(mappedBy="user")
    private List<UserApplicationRole> userApplicationRoles;

    @ManyToMany(mappedBy="user")
    private Set<Role> roles;

    private String name;
    private String imageUrl;

    @Column(unique=true)
    private String token;

    public String getEmail() {
        return email;
    }

    @Column(unique=true)
    private String email;

    private String password;

    public User(){
        userApplicationRoles = new ArrayList<>();
        roles = new HashSet<>();
    }

    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public String getToken() {
        return token;
    }

    @JsonIgnore
    public List<UserApplicationRole> getUserApplicationRoles() {
        return userApplicationRoles;
    }

    public void addUserApplicationRole(UserApplicationRole... r){
        if(this.userApplicationRoles == null){
            this.userApplicationRoles = new ArrayList<>();
        }
        this.userApplicationRoles.addAll(Arrays.asList(r));
    }

    public Boolean passwordEquals(String passwordInput){
        StandardPasswordEncoder encoder = new StandardPasswordEncoder(PASSWORD_SECRET);
        return encoder.matches(passwordInput, this.password);
    }

    public void setPassword(String setPassword) {
        StandardPasswordEncoder encoder = new StandardPasswordEncoder(PASSWORD_SECRET);
        setPassword = encoder.encode(setPassword);
        this.password = setPassword;
    }

    public boolean hasRole(Role role) {
        return roles.contains(role);
    }

    public Set<Role> getRoles() {
        return new HashSet<>(roles);
    }

}

@@ -1111,22 +1111,44 @@
 ionRoles
+ = new ArrayList%3C%3E()
 ;%0A%0A   
+//
  @ManyTo
@@ -1165,32 +1165,34 @@
 dBy=%22user%22)%0A    
+//
 private Set%3CRole
@@ -1198,16 +1198,34 @@
 e%3E roles
+ = new HashSet%3C%3E()
 ;%0A%0A    p
@@ -1491,90 +1491,8 @@
 ()%7B%0A
-        userApplicationRoles = new ArrayList%3C%3E();%0A        roles = new HashSet%3C%3E();
 %0A   
@@ -2544,32 +2544,34 @@
 assword;%0A    %7D%0A%0A
+//
     public boole
@@ -2590,24 +2590,26 @@
 ole role) %7B%0A
+//
         retu
@@ -2633,23 +2633,29 @@
 (role);%0A
+//
     %7D%0A
-%0A
+//%0A//
     publ
@@ -2672,32 +2672,34 @@
 e%3E getRoles() %7B%0A
+//
         return n
@@ -2719,16 +2719,18 @@
 roles);%0A
+//
     %7D%0A%0A%7D

Rollback on data > User : was breaking some tests. Sorry.