Search in sources :

Example 6 with WorkspaceModel

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

the class WorkspaceController method create.

/**
 * 编辑工作空间
 *
 * @param name        工作空间名称
 * @param description 描述
 * @return json
 */
@PostMapping(value = "/edit", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.EDIT)
public String create(String id, @ValidatorItem String name, @ValidatorItem String description) {
    this.checkInfo(id, name);
    // 
    WorkspaceModel workspaceModel = new WorkspaceModel();
    workspaceModel.setName(name);
    workspaceModel.setDescription(description);
    if (StrUtil.isEmpty(id)) {
        // 创建
        workspaceService.insert(workspaceModel);
    } else {
        workspaceModel.setId(id);
        workspaceService.update(workspaceModel);
    }
    return JsonMessage.getString(200, "操作成功");
}
Also used : BaseWorkspaceModel(io.jpom.model.BaseWorkspaceModel) WorkspaceModel(io.jpom.model.data.WorkspaceModel) PostMapping(org.springframework.web.bind.annotation.PostMapping) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) ClassFeature(io.jpom.permission.ClassFeature)

Example 7 with WorkspaceModel

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

the class UserBasicInfoController method myWorkspace.

/**
 * 查询用户自己的工作空间
 *
 * @return msg
 */
@GetMapping(value = "my_workspace", produces = MediaType.APPLICATION_JSON_VALUE)
public String myWorkspace() {
    UserModel user = getUser();
    List<WorkspaceModel> models = userBindWorkspaceService.listUserWorkspaceInfo(user);
    Assert.notEmpty(models, "当前账号没有绑定任何工作空间,请联系管理员处理");
    return JsonMessage.getString(200, "", models);
}
Also used : UserModel(io.jpom.model.data.UserModel) WorkspaceModel(io.jpom.model.data.WorkspaceModel) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 8 with WorkspaceModel

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

the class NodeInfoController method receivePush.

/**
 * 接收节点推送的信息
 * <p>
 * yum install -y wget && wget -O install.sh https://dromara.gitee.io/jpom/docs/install.sh && bash install.sh Agent jdk
 * --auto-push-to-server http://127.0.0.1:3000/api/node/receive_push?token=462a47b8fba8da1f824370bb9fcdc01aa1a0fe20&workspaceId=DEFAULT
 *
 * @return json
 */
@RequestMapping(value = ServerOpenApi.RECEIVE_PUSH, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@NotLogin
public String receivePush(@ValidatorItem(msg = "token empty") String token, @ValidatorItem(msg = "ips empty") String ips, @ValidatorItem(msg = "loginName empty") String loginName, @ValidatorItem(msg = "loginPwd empty") String loginPwd, @ValidatorItem(msg = "workspaceId empty") String workspaceId, @ValidatorItem(value = ValidatorRule.NUMBERS, msg = "port error") int port, String ping) {
    Assert.state(StrUtil.equals(token, JpomManifest.getInstance().randomIdSign()), "token error");
    boolean exists = workspaceService.exists(new WorkspaceModel(workspaceId));
    Assert.state(exists, "workspaceId error");
    String sha1Id = SecureUtil.sha1(ips);
    // 
    List<String> ipsList = StrUtil.split(ips, StrUtil.COMMA);
    String clientIp = getClientIP();
    if (!ipsList.contains(clientIp)) {
        ipsList.add(clientIp);
    }
    List<String> canUseIps = ipsList.stream().filter(s -> this.testIpProt(s, ping)).collect(Collectors.toList());
    List<NodeModel> canUseNode = canUseIps.stream().map(s -> {
        NodeModel model = NodeInfoController.this.createModel(s, loginName, loginPwd, port, workspaceId);
        try {
            nodeService.testNode(model);
        } catch (Exception e) {
            DefaultSystemLog.getLog().warn("测试结果:{} {}", model.getUrl(), e.getMessage());
            return null;
        }
        return model;
    }).filter(Objects::nonNull).collect(Collectors.toList());
    // 只返回能通的 IP
    canUseIps = canUseNode.stream().map(NodeModel::getName).collect(Collectors.toList());
    int size1 = CollUtil.size(canUseNode);
    // 
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("allIp", ipsList);
    jsonObject.put("canUseIp", canUseIps);
    jsonObject.put("port", port);
    jsonObject.put("id", sha1Id);
    jsonObject.put("canUseNode", canUseNode);
    // 
    exists = false;
    for (NodeModel nodeModel : canUseNode) {
        if (nodeService.existsByUrl(nodeModel.getUrl(), nodeModel.getWorkspaceId(), null)) {
            // 存在
            jsonObject.put("type", "exists");
            exists = true;
            break;
        }
    }
    if (!exists) {
        if (size1 == 1) {
            // 只有一个 ip 可以使用
            // 添加插件端
            NodeModel first = CollUtil.getFirst(canUseNode);
            nodeService.insertNotFill(first);
            jsonObject.put("type", "success");
        } else {
            jsonObject.put("type", size1 == 0 ? "canUseIpEmpty" : "multiIp");
        }
    }
    CACHE_RECEIVE_PUSH.put(sha1Id, jsonObject);
    return JsonMessage.getString(200, "done", jsonObject);
}
Also used : AbstractController(cn.jiangzeyin.controller.base.AbstractController) SecureUtil(cn.hutool.crypto.SecureUtil) java.util(java.util) ServerOpenApi(io.jpom.common.ServerOpenApi) DefaultSystemLog(cn.jiangzeyin.common.DefaultSystemLog) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) JsonMessage(cn.jiangzeyin.common.JsonMessage) NetUtil(cn.hutool.core.net.NetUtil) NodeService(io.jpom.service.node.NodeService) WorkspaceModel(io.jpom.model.data.WorkspaceModel) WorkspaceService(io.jpom.service.system.WorkspaceService) MediaType(org.springframework.http.MediaType) NodeModel(io.jpom.model.data.NodeModel) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) CollUtil(cn.hutool.core.collection.CollUtil) StrUtil(cn.hutool.core.util.StrUtil) ValidatorRule(cn.jiangzeyin.common.validator.ValidatorRule) JpomManifest(io.jpom.common.JpomManifest) ValidatorItem(cn.jiangzeyin.common.validator.ValidatorItem) Convert(cn.hutool.core.convert.Convert) JSONObject(com.alibaba.fastjson.JSONObject) NotLogin(io.jpom.common.interceptor.NotLogin) Assert(org.springframework.util.Assert) WorkspaceModel(io.jpom.model.data.WorkspaceModel) NodeModel(io.jpom.model.data.NodeModel) JSONObject(com.alibaba.fastjson.JSONObject) NotLogin(io.jpom.common.interceptor.NotLogin) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with WorkspaceModel

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

the class BaseNodeService method syncExecuteNode.

/**
 * 同步执行 同步节点信息
 *
 * @param nodeModel 节点信息
 * @return json
 */
public String syncExecuteNode(NodeModel nodeModel) {
    String nodeModelName = nodeModel.getName();
    if (!nodeModel.isOpenStatus()) {
        DefaultSystemLog.getLog().debug("{} 节点未启用", nodeModelName);
        return "节点未启用";
    }
    try {
        JSONArray jsonArray = this.getLitDataArray(nodeModel);
        if (CollUtil.isEmpty(jsonArray)) {
            Entity entity = Entity.create();
            entity.set("nodeId", nodeModel.getId());
            int del = super.del(entity);
            // 
            DefaultSystemLog.getLog().debug("{} 节点没有拉取到任何{}", nodeModelName, dataName);
            return "节点没有拉取到任何" + dataName;
        }
        // 查询现在存在的项目
        T where = ReflectUtil.newInstance(this.tClass);
        where.setWorkspaceId(nodeModel.getWorkspaceId());
        where.setNodeId(nodeModel.getId());
        List<T> cacheAll = super.listByBean(where);
        cacheAll = ObjectUtil.defaultIfNull(cacheAll, Collections.EMPTY_LIST);
        Set<String> cacheIds = cacheAll.stream().map(BaseNodeModel::dataId).collect(Collectors.toSet());
        // 
        List<T> projectInfoModels = jsonArray.toJavaList(this.tClass);
        List<T> models = projectInfoModels.stream().peek(item -> this.fullData(item, nodeModel)).filter(item -> {
            // 检查对应的工作空间 是否存在
            return workspaceService.exists(new WorkspaceModel(item.getWorkspaceId()));
        }).filter(projectInfoModel -> {
            // 避免重复同步
            return StrUtil.equals(nodeModel.getWorkspaceId(), projectInfoModel.getWorkspaceId());
        }).peek(item -> cacheIds.remove(item.dataId())).collect(Collectors.toList());
        // 设置 临时缓存,便于放行检查
        BaseServerController.resetInfo(UserModel.EMPTY);
        // 
        models.forEach(BaseNodeService.super::upsert);
        // 删除项目
        Set<String> strings = cacheIds.stream().map(s -> BaseNodeModel.fullId(nodeModel.getWorkspaceId(), nodeModel.getId(), s)).collect(Collectors.toSet());
        if (CollUtil.isNotEmpty(strings)) {
            super.delByKey(strings, null);
        }
        String format = StrUtil.format("{} 节点拉取到 {} 个{},已经缓存 {} 个{},更新 {} 个{},删除 {} 个缓存", nodeModelName, CollUtil.size(jsonArray), dataName, CollUtil.size(cacheAll), dataName, CollUtil.size(models), dataName, CollUtil.size(strings));
        DefaultSystemLog.getLog().debug(format);
        return format;
    } catch (Exception e) {
        return this.checkException(e, nodeModelName);
    } finally {
        BaseServerController.removeEmpty();
    }
}
Also used : PageResultDto(io.jpom.model.PageResultDto) ObjectUtil(cn.hutool.core.util.ObjectUtil) DefaultSystemLog(cn.jiangzeyin.common.DefaultSystemLog) ServletUtil(cn.hutool.extra.servlet.ServletUtil) JSONArray(com.alibaba.fastjson.JSONArray) HttpServletRequest(javax.servlet.http.HttpServletRequest) NodeService(io.jpom.service.node.NodeService) Map(java.util.Map) BaseNodeModel(io.jpom.model.BaseNodeModel) WorkspaceModel(io.jpom.model.data.WorkspaceModel) WorkspaceService(io.jpom.service.system.WorkspaceService) NodeModel(io.jpom.model.data.NodeModel) Set(java.util.Set) SpringUtil(cn.jiangzeyin.common.spring.SpringUtil) Collectors(java.util.stream.Collectors) CollUtil(cn.hutool.core.collection.CollUtil) StrUtil(cn.hutool.core.util.StrUtil) List(java.util.List) Const(io.jpom.common.Const) ReflectUtil(cn.hutool.core.util.ReflectUtil) JSONObject(com.alibaba.fastjson.JSONObject) Entity(cn.hutool.db.Entity) ThreadUtil(cn.hutool.core.thread.ThreadUtil) UserModel(io.jpom.model.data.UserModel) AgentException(io.jpom.system.AgentException) Collections(java.util.Collections) BaseServerController(io.jpom.common.BaseServerController) Assert(org.springframework.util.Assert) Entity(cn.hutool.db.Entity) WorkspaceModel(io.jpom.model.data.WorkspaceModel) JSONArray(com.alibaba.fastjson.JSONArray) AgentException(io.jpom.system.AgentException)

Example 10 with WorkspaceModel

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

the class DbUserOperateLogService method checkMonitor.

/**
 * 判断当前操作是否需要报警
 *
 * @param userOperateLogV1 操作信息
 * @param cacheInfo        操作缓存相关
 */
private void checkMonitor(UserOperateLogV1 userOperateLogV1, OperateLogController.CacheInfo cacheInfo) {
    ClassFeature classFeature = EnumUtil.fromString(ClassFeature.class, userOperateLogV1.getClassFeature(), null);
    MethodFeature methodFeature = EnumUtil.fromString(MethodFeature.class, userOperateLogV1.getMethodFeature(), null);
    UserModel optUserItem = userService.getByKey(userOperateLogV1.getUserId());
    if (classFeature == null || methodFeature == null || optUserItem == null) {
        return;
    }
    Map<String, Object> dataMap = this.buildDataMsg(classFeature, cacheInfo, userOperateLogV1);
    WorkspaceModel workspaceModel = workspaceService.getByKey(userOperateLogV1.getWorkspaceId());
    String optTypeMsg = StrUtil.format(" 【{}】->【{}】", classFeature.getName(), methodFeature.getName());
    List<MonitorUserOptModel> monitorUserOptModels = monitorUserOptService.listByType(userOperateLogV1.getWorkspaceId(), classFeature, methodFeature, userOperateLogV1.getUserId());
    if (CollUtil.isEmpty(monitorUserOptModels)) {
        return;
    }
    String context = this.buildContent(optUserItem, dataMap, workspaceModel, optTypeMsg, userOperateLogV1);
    for (MonitorUserOptModel monitorUserOptModel : monitorUserOptModels) {
        List<String> notifyUser = monitorUserOptModel.notifyUser();
        if (CollUtil.isEmpty(notifyUser)) {
            continue;
        }
        for (String userId : notifyUser) {
            UserModel item = userService.getByKey(userId);
            if (item == null) {
                continue;
            }
            // 邮箱
            String email = item.getEmail();
            if (StrUtil.isNotEmpty(email)) {
                MonitorModel.Notify notify1 = new MonitorModel.Notify(MonitorModel.NotifyType.mail, email);
                ThreadUtil.execute(() -> {
                    try {
                        NotifyUtil.send(notify1, "用户操作报警", context);
                    } catch (Exception e) {
                        DefaultSystemLog.getLog().error("发送报警信息错误", e);
                    }
                });
            }
            // dingding
            String dingDing = item.getDingDing();
            if (StrUtil.isNotEmpty(dingDing)) {
                MonitorModel.Notify notify1 = new MonitorModel.Notify(MonitorModel.NotifyType.dingding, dingDing);
                ThreadUtil.execute(() -> {
                    try {
                        NotifyUtil.send(notify1, "用户操作报警", context);
                    } catch (Exception e) {
                        DefaultSystemLog.getLog().error("发送报警信息错误", e);
                    }
                });
            }
            // 企业微信
            String workWx = item.getWorkWx();
            if (StrUtil.isNotEmpty(workWx)) {
                MonitorModel.Notify notify1 = new MonitorModel.Notify(MonitorModel.NotifyType.workWx, workWx);
                ThreadUtil.execute(() -> {
                    try {
                        NotifyUtil.send(notify1, "用户操作报警", context);
                    } catch (Exception e) {
                        DefaultSystemLog.getLog().error("发送报警信息错误", e);
                    }
                });
            }
        }
    }
}
Also used : WorkspaceModel(io.jpom.model.data.WorkspaceModel) MonitorModel(io.jpom.model.data.MonitorModel) MethodFeature(io.jpom.permission.MethodFeature) UserModel(io.jpom.model.data.UserModel) ClassFeature(io.jpom.permission.ClassFeature) MonitorUserOptModel(io.jpom.model.data.MonitorUserOptModel)

Aggregations

WorkspaceModel (io.jpom.model.data.WorkspaceModel)10 UserModel (io.jpom.model.data.UserModel)5 StrUtil (cn.hutool.core.util.StrUtil)3 JSONObject (com.alibaba.fastjson.JSONObject)3 NodeModel (io.jpom.model.data.NodeModel)3 MethodFeature (io.jpom.permission.MethodFeature)3 WorkspaceService (io.jpom.service.system.WorkspaceService)3 Collectors (java.util.stream.Collectors)3 Assert (org.springframework.util.Assert)3 CollUtil (cn.hutool.core.collection.CollUtil)2 Entity (cn.hutool.db.Entity)2 DefaultSystemLog (cn.jiangzeyin.common.DefaultSystemLog)2 NotLogin (io.jpom.common.interceptor.NotLogin)2 BaseWorkspaceModel (io.jpom.model.BaseWorkspaceModel)2 UserLoginDto (io.jpom.model.dto.UserLoginDto)2 ClassFeature (io.jpom.permission.ClassFeature)2 NodeService (io.jpom.service.node.NodeService)2 PostMapping (org.springframework.web.bind.annotation.PostMapping)2 Convert (cn.hutool.core.convert.Convert)1 NetUtil (cn.hutool.core.net.NetUtil)1