package com.sock.mall.bo;

import com.sock.mall.model.UmsAdmin;
import com.sock.mall.model.UmsPermission;
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;
import java.util.stream.Collectors;

/**
 * SpringSecurity需要的用户详情
 */
public class AdminUserDetails implements UserDetails {
    private UmsAdmin umsAdmin;
    private List<UmsPermission> permissionList;
    public AdminUserDetails(UmsAdmin umsAdmin,List<UmsPermission> permissionList) {
        this.umsAdmin = umsAdmin;
        this.permissionList = permissionList;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        //返回当前用户的权限
        return permissionList.stream()
                .filter(permission -> permission.getValue()!=null)
                .map(permission ->new SimpleGrantedAuthority(permission.getValue()))
                .collect(Collectors.toList());
    }

    @Override
    /*登录密码*/
    public String getPassword() {
        return umsAdmin.getPassword();
    }

    @Override
    /*登录名称*/
    public String getUsername() {
        return umsAdmin.getUsername();
    }

    @Override
    /*有效性验证(是否过期)*/
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    /*有效性验证(是否被锁)*/
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    /*验证证书是否有效*/
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    /*状态判断890-*/
    public boolean isEnabled() {
        return umsAdmin.getStatus().equals(1);
    }
}
