package com.brframework.commonsecurity.core;

import com.google.common.collect.Lists;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;

/**
 * @Author xu
 * @Date 2018/1/31 0031 下午 7:09
 * 用户对象
 */
public class SecurityUserDetails implements UserDetails {

    /**
     *
     * @param subject   用户信息载体
     */
    public SecurityUserDetails(SecuritySubject subject) {
        this.subject = subject;

        authorities = Lists.newArrayList();
        subject.getAuthorities().forEach(s -> authorities.add(new SimpleGrantedAuthority(s)));

        this.accountNonExpired = true;
        this.accountNonLocked = true;
        this.credentialsNonExpired = true;
        this.enabled = true;
    }

    private SecuritySubject subject;
    private boolean accountNonExpired;
    private boolean accountNonLocked;
    private boolean credentialsNonExpired;
    private boolean enabled;
    private List<GrantedAuthority> authorities;

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return subject.getPassword();
    }

    @Override
    public String getUsername() {
        return subject.getUsername();
    }

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

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

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

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

    public String getId() {
        return subject.getId();
    }

    public SecuritySubject getSubject(){
        return this.subject;
    }
}
