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

import cn.reghao.devops.mgr.admin.model.po.Role;
import cn.reghao.devops.mgr.builder.model.constant.EnvType;
import cn.reghao.devops.mgr.admin.model.constant.RoleType;
import cn.reghao.devops.mgr.admin.model.po.User;
import cn.reghao.devops.mgr.admin.service.UserContext;

/**
 * @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 {
            Role role = user.getRole();
            boolean isAdmin = role.getName().equals(RoleType.ROLE_ADMIN.name());
            if (EnvType.prod.name().equals(env) && !isAdmin) {
                throw new Exception("没有权限");
            }
        }

        return user;
    }
}
