content: package com.alkl1m.taskmanager.service.auth;
  
  import com.alkl1m.taskmanager.entity.User;
  import com.alkl1m.taskmanager.enums.Role;
  import com.fasterxml.jackson.annotation.JsonIgnore;
  import lombok.Data;
  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.List;
  import java.util.Objects;
  
  @Data
  public class UserDetailsImpl implements UserDetails {
      private static final long serialVersionUID = 1L;
      private Long id;
      private String name;
      private String email;
      private boolean enabled;
  
      @JsonIgnore
      private String password;
  
      private Collection<? extends GrantedAuthority> authorities;
  
      public UserDetailsImpl(Long id, String name, String email, String password, boolean enabled,
                             Collection<? extends GrantedAuthority> authorities) {
          this.id = id;
          this.name = name;
          this.email = email;
          this.password = password;
          this.enabled = enabled;
          this.authorities = authorities;
      }
  
      public static UserDetailsImpl build(User user) {
          List<GrantedAuthority> authorities = new ArrayList<>();
          if (user.getRole() == Role.USER) {
              authorities.add(new SimpleGrantedAuthority(Role.USER.name()));
          } else if (user.getRole() == Role.ADMIN) {
              authorities.add(new SimpleGrantedAuthority(Role.ADMIN.name()));
          }
          return new UserDetailsImpl(
                  user.getId(),
                  user.getName(),
                  user.getEmail(),
                  user.getPassword(),
                  user.getEnabled(),
                  authorities);
      }
  
      @Override
      public Collection<? extends GrantedAuthority> getAuthorities() {
          return authorities;
      }
  
      @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 enabled;
      }
  
      @Override
      public boolean equals(Object obj) {
          if (this == obj)
              return true;
          if (obj == null || getClass() != obj.getClass())
              return false;
          UserDetailsImpl user = (UserDetailsImpl) obj;
          return Objects.equals(id, user.id);
      }
  }
  