package com.yfd.platform.config;

import com.yfd.platform.utils.SecurityUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service(value = "el")
public class PermissionConfig {

    public Boolean check(String... permissions) throws RuntimeException {
        // 获取当前用户的所有权限
        List<String> elPermissions = SecurityUtils.getCurrentUser().getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
        String currentUsername = SecurityUtils.getCurrentUsername();
        // 判断当前用户的所有权限是否包含接口上定义的权限
        boolean flag = currentUsername.equals("admin") || Arrays.stream(permissions).anyMatch(elPermissions::contains);
        if (!flag) {
            throw new RuntimeException("没有该权限");
        }
        return flag;
    }
}