package cn.reghao.devops.web.mgr.app.service;

import cn.reghao.devops.web.mgr.build.model.constant.EnvType;
import cn.reghao.devops.web.admin.account.model.constant.RoleType;
import cn.reghao.devops.web.admin.account.model.po.User;
import cn.reghao.devops.web.admin.account.service.UserContext;

import java.util.Set;

/**
 * @author reghao
 * @date 2024-02-04 15:15:38
 */
public class PermissionCheck {
    public static User check(String env) throws Exception {
        User user = UserContext.getUser();
        if (user == null) {
            throw new Exception("未登录");
        } else {
            Set<String> roles = user.getRole();
            boolean isAdmin = roles.contains(RoleType.ROLE_ADMIN.name());
            if (EnvType.prod.name().equals(env) && !isAdmin) {
                throw new Exception("没有权限");
            }
        }

        return user;
    }
}
