Search in sources :

Example 31 with NodeModel

use of io.jpom.model.data.NodeModel in project Jpom by dromara.

the class PermissionInterceptor method checkNodeDataPermission.

/**
 * 检查管理员权限
 *
 * @param userModel     用户
 * @param response      响应
 * @param handlerMethod 拦截到到方法
 * @return true 有权限
 */
private boolean checkNodeDataPermission(UserModel userModel, HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod) {
    NodeDataPermission nodeDataPermission = this.getNodeDataPermission(handlerMethod);
    if (nodeDataPermission == null || userModel.isSuperSystemUser()) {
        return true;
    }
    NodeModel node = (NodeModel) request.getAttribute("node");
    if (node != null) {
        String parameterName = nodeDataPermission.parameterName();
        BaseNodeService<?> baseNodeService = SpringUtil.getBean(nodeDataPermission.cls());
        String dataId = request.getParameter(parameterName);
        if (StrUtil.isNotEmpty(dataId)) {
            BaseNodeModel data = baseNodeService.getData(node.getId(), dataId);
            if (data != null) {
                boolean exists = userBindWorkspaceService.exists(userModel.getId(), data.getWorkspaceId());
                if (!exists) {
                    this.errorMsg(response, "您没有对应到数据权限:-3");
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : NodeModel(io.jpom.model.data.NodeModel) BaseNodeModel(io.jpom.model.BaseNodeModel) BaseNodeModel(io.jpom.model.BaseNodeModel)

Example 32 with NodeModel

use of io.jpom.model.data.NodeModel in project Jpom by dromara.

the class PermissionInterceptor method addNode.

private void addNode(HttpServletRequest request) {
    String nodeId = request.getParameter("nodeId");
    if (!StrUtil.isBlankOrUndefined(nodeId)) {
        // 节点信息
        NodeModel nodeModel = nodeService.getByKey(nodeId);
        if (nodeModel != null && !nodeModel.isOpenStatus()) {
            throw new AgentException(nodeModel.getName() + "节点未启用");
        }
        request.setAttribute("node", nodeModel);
    }
}
Also used : NodeModel(io.jpom.model.data.NodeModel) BaseNodeModel(io.jpom.model.BaseNodeModel) AgentException(io.jpom.system.AgentException)

Example 33 with NodeModel

use of io.jpom.model.data.NodeModel in project Jpom by dromara.

the class IndexControl method menusData.

/**
 * @return json
 * @api {post} menus_data.json 获取系统菜单相关数据
 * @apiGroup index
 * @apiUse loginUser
 * @apiParam {String} nodeId 节点ID
 * @apiSuccess {JSON}  data 菜单相关字段
 */
@RequestMapping(value = "menus_data.json", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public String menusData() {
    NodeModel nodeModel = tryGetNode();
    UserModel userModel = getUserModel();
    String workspaceId = nodeService.getCheckUserWorkspace(getRequest());
    JSONObject config = systemParametersServer.getConfigDefNewInstance(StrUtil.format("menus_config_{}", workspaceId), JSONObject.class);
    // 菜单
    InputStream inputStream;
    JSONArray showArray;
    if (nodeModel == null) {
        inputStream = ResourceUtil.getStream("classpath:/menus/index.json");
        showArray = config.getJSONArray("serverMenuKeys");
    } else {
        inputStream = ResourceUtil.getStream("classpath:/menus/node-index.json");
        showArray = config.getJSONArray("nodeMenuKeys");
    }
    String json = IoUtil.read(inputStream, CharsetUtil.CHARSET_UTF_8);
    JSONArray jsonArray = JSONArray.parseArray(json);
    List<Object> collect1 = jsonArray.stream().filter(o -> {
        JSONObject jsonObject = (JSONObject) o;
        if (!testMenus(jsonObject, userModel, nodeModel, showArray)) {
            return false;
        }
        JSONArray childs = jsonObject.getJSONArray("childs");
        if (childs != null) {
            List<Object> collect = childs.stream().filter(o1 -> {
                JSONObject jsonObject1 = (JSONObject) o1;
                return testMenus(jsonObject1, userModel, nodeModel, showArray);
            }).collect(Collectors.toList());
            if (collect.isEmpty()) {
                return false;
            }
            jsonObject.put("childs", collect);
        }
        return true;
    }).collect(Collectors.toList());
    Assert.notEmpty(jsonArray, "没有任何菜单,请联系管理员");
    return JsonMessage.getString(200, "", collect1);
}
Also used : UserModel(io.jpom.model.data.UserModel) ResourceUtil(cn.hutool.core.io.resource.ResourceUtil) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ServletUtil(cn.hutool.extra.servlet.ServletUtil) BaseJpomInterceptor(io.jpom.common.interceptor.BaseJpomInterceptor) FileTypeUtil(cn.hutool.core.io.FileTypeUtil) JsonMessage(cn.jiangzeyin.common.JsonMessage) JSONArray(com.alibaba.fastjson.JSONArray) UrlRedirectUtil(io.jpom.common.UrlRedirectUtil) SystemParametersServer(io.jpom.service.system.SystemParametersServer) GetMapping(org.springframework.web.bind.annotation.GetMapping) RegexPool(cn.hutool.core.lang.RegexPool) ContentType(cn.hutool.http.ContentType) ExtConfigBean(io.jpom.system.ExtConfigBean) UserBindWorkspaceService(io.jpom.service.user.UserBindWorkspaceService) IoUtil(cn.hutool.core.io.IoUtil) MediaType(org.springframework.http.MediaType) HttpServletResponse(javax.servlet.http.HttpServletResponse) NodeModel(io.jpom.model.data.NodeModel) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) IOException(java.io.IOException) ReUtil(cn.hutool.core.util.ReUtil) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) CollUtil(cn.hutool.core.collection.CollUtil) StrUtil(cn.hutool.core.util.StrUtil) List(java.util.List) Const(io.jpom.common.Const) CharsetUtil(cn.hutool.core.util.CharsetUtil) ConfigBean(io.jpom.system.ConfigBean) FileUtil(cn.hutool.core.io.FileUtil) JSONObject(com.alibaba.fastjson.JSONObject) NotLogin(io.jpom.common.interceptor.NotLogin) UserModel(io.jpom.model.data.UserModel) UserService(io.jpom.service.user.UserService) BaseServerController(io.jpom.common.BaseServerController) Validator(cn.hutool.core.lang.Validator) ServerExtConfigBean(io.jpom.system.ServerExtConfigBean) Assert(org.springframework.util.Assert) InputStream(java.io.InputStream) NodeModel(io.jpom.model.data.NodeModel) JSONObject(com.alibaba.fastjson.JSONObject) InputStream(java.io.InputStream) JSONArray(com.alibaba.fastjson.JSONArray) JSONObject(com.alibaba.fastjson.JSONObject) List(java.util.List) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 34 with NodeModel

use of io.jpom.model.data.NodeModel in project Jpom by dromara.

the class ReleaseManage method diffSyncProject.

/**
 * 差异上传发布
 *
 * @param nodeModel 节点
 * @param projectId 项目ID
 * @param afterOpt  发布后的操作
 */
private void diffSyncProject(NodeModel nodeModel, String projectId, AfterOpt afterOpt, boolean clearOld) {
    File resultFile = this.resultFile;
    String resultFileParent = resultFile.isFile() ? FileUtil.getAbsolutePath(resultFile.getParent()) : FileUtil.getAbsolutePath(this.resultFile);
    // 
    List<File> files = FileUtil.loopFiles(resultFile);
    List<JSONObject> collect = files.stream().map(file -> {
        // 
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", StringUtil.delStartPath(file, resultFileParent, true));
        jsonObject.put("sha1", SecureUtil.sha1(file));
        return jsonObject;
    }).collect(Collectors.toList());
    // 
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("id", projectId);
    jsonObject.put("data", collect);
    JsonMessage<JSONObject> requestBody = NodeForward.requestBody(nodeModel, NodeUrl.MANAGE_FILE_DIFF_FILE, this.userModel, jsonObject);
    if (requestBody.getCode() != HttpStatus.HTTP_OK) {
        throw new JpomRuntimeException("对比项目文件失败:" + requestBody);
    }
    JSONObject data = requestBody.getData();
    JSONArray diff = data.getJSONArray("diff");
    JSONArray del = data.getJSONArray("del");
    int delSize = CollUtil.size(del);
    int diffSize = CollUtil.size(diff);
    if (clearOld) {
        logRecorder.info(StrUtil.format("对比文件结果,产物文件 {} 个、需要上传 {} 个、需要删除 {} 个", CollUtil.size(collect), CollUtil.size(diff), delSize));
    } else {
        logRecorder.info(StrUtil.format("对比文件结果,产物文件 {} 个、需要上传 {} 个", CollUtil.size(collect), CollUtil.size(diff)));
    }
    // 清空发布才先执行删除
    if (delSize > 0 && clearOld) {
        jsonObject.put("data", del);
        requestBody = NodeForward.requestBody(nodeModel, NodeUrl.MANAGE_FILE_BATCH_DELETE, this.userModel, jsonObject);
        if (requestBody.getCode() != HttpStatus.HTTP_OK) {
            throw new JpomRuntimeException("删除项目文件失败:" + requestBody);
        }
    }
    for (int i = 0; i < diffSize; i++) {
        boolean last = (i == diffSize - 1);
        JSONObject diffData = (JSONObject) diff.get(i);
        String name = diffData.getString("name");
        File file = FileUtil.file(resultFileParent, name);
        // 
        String startPath = StringUtil.delStartPath(file, resultFileParent, false);
        // 
        JsonMessage<String> jsonMessage = OutGivingRun.fileUpload(file, startPath, projectId, false, last ? afterOpt : AfterOpt.No, nodeModel, this.userModel, false);
        if (jsonMessage.getCode() != HttpStatus.HTTP_OK) {
            throw new JpomRuntimeException("同步项目文件失败:" + jsonMessage);
        }
        if (last) {
            // 最后一个
            logRecorder.info("发布项目包成功:" + jsonMessage);
        }
    }
}
Also used : DateUtil(cn.hutool.core.date.DateUtil) SecureUtil(cn.hutool.crypto.SecureUtil) DockerInfoService(io.jpom.service.docker.DockerInfoService) Map(java.util.Map) WorkspaceEnvVarService(io.jpom.service.system.WorkspaceEnvVarService) BetweenFormatter(cn.hutool.core.date.BetweenFormatter) Sftp(cn.hutool.extra.ssh.Sftp) NodeForward(io.jpom.common.forward.NodeForward) LineHandler(cn.hutool.core.io.LineHandler) SshService(io.jpom.service.node.ssh.SshService) NodeModel(io.jpom.model.data.NodeModel) AfterOpt(io.jpom.model.AfterOpt) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) StrUtil(cn.hutool.core.util.StrUtil) List(java.util.List) Builder(lombok.Builder) Session(com.jcraft.jsch.Session) ArrayUtil(cn.hutool.core.util.ArrayUtil) SshModel(io.jpom.model.data.SshModel) SystemClock(cn.hutool.core.date.SystemClock) CharPool(cn.hutool.core.text.CharPool) JSONObject(com.alibaba.fastjson.JSONObject) HttpStatus(cn.hutool.http.HttpStatus) IPlugin(io.jpom.plugin.IPlugin) CommandUtil(io.jpom.util.CommandUtil) ResourceUtil(cn.hutool.core.io.resource.ResourceUtil) LogRecorder(io.jpom.util.LogRecorder) BuildReleaseMethod(io.jpom.model.enums.BuildReleaseMethod) FileUtils(io.jpom.util.FileUtils) JschUtil(cn.hutool.extra.ssh.JschUtil) JpomRuntimeException(io.jpom.system.JpomRuntimeException) JsonMessage(cn.jiangzeyin.common.JsonMessage) JSONArray(com.alibaba.fastjson.JSONArray) BaseEnum(io.jpom.model.BaseEnum) NodeService(io.jpom.service.node.NodeService) OutGivingRun(io.jpom.outgiving.OutGivingRun) PluginFactory(io.jpom.plugin.PluginFactory) IoUtil(cn.hutool.core.io.IoUtil) DockerInfoModel(io.jpom.model.docker.DockerInfoModel) SpringUtil(cn.jiangzeyin.common.spring.SpringUtil) DockerSwarmInfoService(io.jpom.service.docker.DockerSwarmInfoService) File(java.io.File) NodeUrl(io.jpom.common.forward.NodeUrl) Consumer(java.util.function.Consumer) CollUtil(cn.hutool.core.collection.CollUtil) StringUtil(io.jpom.util.StringUtil) ConfigBean(io.jpom.system.ConfigBean) FileUtil(cn.hutool.core.io.FileUtil) UserModel(io.jpom.model.data.UserModel) BuildStatus(io.jpom.model.enums.BuildStatus) InputStream(java.io.InputStream) JSONObject(com.alibaba.fastjson.JSONObject) JpomRuntimeException(io.jpom.system.JpomRuntimeException) JSONArray(com.alibaba.fastjson.JSONArray) File(java.io.File)

Example 35 with NodeModel

use of io.jpom.model.data.NodeModel in project Jpom by dromara.

the class NodeForward method addUser.

/**
 * 添加agent 授权信息header
 *
 * @param httpRequest request
 * @param nodeModel   节点
 * @param userModel   用户
 */
private static void addUser(HttpRequest httpRequest, NodeModel nodeModel, NodeUrl nodeUrl, UserModel userModel) {
    // 判断开启状态
    if (!nodeModel.isOpenStatus()) {
        throw new AgentException(nodeModel.getName() + "节点未启用");
    }
    if (userModel != null) {
        httpRequest.header(ConfigBean.JPOM_SERVER_USER_NAME, URLUtil.encode(userModel.getId()));
    // httpRequest.header(ConfigBean.JPOM_SERVER_SYSTEM_USER_ROLE, userModel.getUserRole(nodeModel).name());
    }
    if (StrUtil.isEmpty(nodeModel.getLoginPwd())) {
        NodeService nodeService = SpringUtil.getBean(NodeService.class);
        NodeModel model = nodeService.getByKey(nodeModel.getId(), false);
        nodeModel.setLoginPwd(model.getLoginPwd());
        nodeModel.setLoginName(model.getLoginName());
    }
    httpRequest.header(ConfigBean.JPOM_AGENT_AUTHORIZE, nodeModel.toAuthorize());
    httpRequest.header(Const.WORKSPACEID_REQ_HEADER, nodeModel.getWorkspaceId());
    // 
    int timeOut = nodeModel.getTimeOut();
    if (nodeUrl.getTimeOut() != -1 && timeOut > 0) {
        // 
        timeOut = Math.max(timeOut, 2);
        httpRequest.timeout(timeOut * 1000);
    }
    // 添加 http proxy
    Proxy proxy = nodeModel.proxy();
    httpRequest.setProxy(proxy);
}
Also used : NodeModel(io.jpom.model.data.NodeModel) Proxy(java.net.Proxy) AgentException(io.jpom.system.AgentException) NodeService(io.jpom.service.node.NodeService)

Aggregations

NodeModel (io.jpom.model.data.NodeModel)67 JSONObject (com.alibaba.fastjson.JSONObject)29 Feature (io.jpom.permission.Feature)23 MethodFeature (io.jpom.permission.MethodFeature)23 ClassFeature (io.jpom.permission.ClassFeature)22 UserModel (io.jpom.model.data.UserModel)18 StrUtil (cn.hutool.core.util.StrUtil)13 NodeService (io.jpom.service.node.NodeService)13 Collectors (java.util.stream.Collectors)13 CollUtil (cn.hutool.core.collection.CollUtil)12 JsonMessage (cn.jiangzeyin.common.JsonMessage)12 List (java.util.List)12 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 DefaultSystemLog (cn.jiangzeyin.common.DefaultSystemLog)10 NodeForward (io.jpom.common.forward.NodeForward)9 NodeUrl (io.jpom.common.forward.NodeUrl)9 JSONArray (com.alibaba.fastjson.JSONArray)8 BaseServerController (io.jpom.common.BaseServerController)8 Assert (org.springframework.util.Assert)8 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)8