package net.devopssolutions.microservice.auth;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

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

@Entity
@Table(name = "users")
public class User implements UserDetails {

    @Id
    @GeneratedValue
    @Column(name = "id")
    private Long id;

    @Column(name = "name", unique = true)
    private String name;

    @Column(name = "password")
    private String password;

    @Column(name = "role")
    private String role;

    public User() {
    }

    public User(String name, String password, String role) {
        this.name = name;
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        this.password = passwordEncoder.encode(password);
        this.role = role;
    }

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        ArrayList<GrantedAuthority> collection = new ArrayList<>();
        if (getId() != null) {
            collection.add(new SimpleGrantedAuthority("ROLE_USER"));
        }
        if (getRole() != null) {
            collection.add(new SimpleGrantedAuthority("ROLE_" + getRole()));
        }
        return collection;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return name;
    }

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

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

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

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

    public void setPassword(CharSequence password) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        this.password = passwordEncoder.encode(password);
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

}

@@ -245,82 +245,37 @@
 ls;%0A
+%0A
 import 
-org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
+javax.persistence.*
 ;%0A
-%0A
 impo
@@ -277,39 +277,43 @@
 %0Aimport java
-x.persistence.*
+.security.Principal
 ;%0Aimport jav
@@ -431,16 +431,27 @@
 rDetails
+, Principal
  %7B%0A%0A    
@@ -829,133 +829,32 @@
     
-BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();%0A        this.password = passwordEncoder.encode(password)
+this.password = password
 ;%0A  
@@ -2064,20 +2064,14 @@
 ord(
-CharSequence
+String
  pas
@@ -2091,133 +2091,32 @@
     
-BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();%0A        this.password = passwordEncoder.encode(password)
+this.password = password
 ;%0A  

add auth user