Search in sources :

Example 16 with UserModel

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

the class UserListController method unlock.

/**
 * 解锁用户锁定状态
 *
 * @param id id
 * @return json
 */
@GetMapping(value = "unlock", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.EDIT)
public String unlock(@ValidatorItem String id) {
    UserModel update = UserModel.unLock(id);
    userService.update(update);
    return JsonMessage.getString(200, "解锁成功");
}
Also used : UserModel(io.jpom.model.data.UserModel) ClassFeature(io.jpom.permission.ClassFeature) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature)

Example 17 with UserModel

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

the class UserListController method closeMfa.

/**
 * 关闭用户 mfa
 *
 * @param id id
 * @return json
 */
@GetMapping(value = "close_user_mfa", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.EDIT)
@SystemPermission(superUser = true)
public String closeMfa(@ValidatorItem String id) {
    UserModel update = new UserModel(id);
    update.setTwoFactorAuthKey(StrUtil.EMPTY);
    userService.update(update);
    return JsonMessage.getString(200, "关闭成功");
}
Also used : UserModel(io.jpom.model.data.UserModel) SystemPermission(io.jpom.permission.SystemPermission) ClassFeature(io.jpom.permission.ClassFeature) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature)

Example 18 with UserModel

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

the class ScriptController method syncNodeScript.

private void syncNodeScript(ScriptModel scriptModel, String oldNode) {
    List<String> oldNodeIds = StrUtil.splitTrim(oldNode, StrUtil.COMMA);
    List<String> newNodeIds = StrUtil.splitTrim(scriptModel.getNodeIds(), StrUtil.COMMA);
    Collection<String> delNode = CollUtil.subtract(oldNodeIds, newNodeIds);
    UserModel user = getUser();
    // 删除
    this.syncDelNodeScript(scriptModel, user, delNode);
    // 更新
    for (String newNodeId : newNodeIds) {
        NodeModel byKey = nodeService.getByKey(newNodeId, getRequest());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", scriptModel.getId());
        jsonObject.put("type", "sync");
        jsonObject.put("context", scriptModel.getContext());
        jsonObject.put("autoExecCron", scriptModel.getAutoExecCron());
        jsonObject.put("defArgs", scriptModel.getDefArgs());
        jsonObject.put("description", scriptModel.getDescription());
        jsonObject.put("name", scriptModel.getName());
        jsonObject.put("workspaceId", scriptModel.getWorkspaceId());
        JsonMessage<String> request = NodeForward.request(byKey, NodeUrl.Script_Save, user, jsonObject);
        Assert.state(request.getCode() == 200, "处理 " + byKey.getName() + " 节点同步脚本失败" + request.getMsg());
        nodeScriptServer.syncNode(byKey);
    }
}
Also used : UserModel(io.jpom.model.data.UserModel) NodeModel(io.jpom.model.data.NodeModel) JSONObject(com.alibaba.fastjson.JSONObject)

Example 19 with UserModel

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

the class SshController method save.

/**
 * 编辑
 *
 * @param name              名称
 * @param host              端口
 * @param user              用户名
 * @param password          密码
 * @param connectType       连接方式
 * @param privateKey        私钥
 * @param port              端口
 * @param charset           编码格式
 * @param fileDirs          文件夹
 * @param id                ID
 * @param notAllowedCommand 禁止输入的命令
 * @return json
 */
@PostMapping(value = "save.json", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.EDIT)
public String save(@ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "ssh名称不能为空") String name, @ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "host不能为空") String host, @ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "user不能为空") String user, String password, SshModel.ConnectType connectType, String privateKey, @ValidatorItem(value = ValidatorRule.POSITIVE_INTEGER, msg = "port错误") int port, String charset, String fileDirs, String id, String notAllowedCommand) {
    SshModel sshModel;
    boolean add = StrUtil.isEmpty(getParameter("id"));
    if (add) {
        // 优先判断参数 如果是 password 在修改时可以不填写
        if (connectType == SshModel.ConnectType.PASS) {
            Assert.hasText(password, "请填写登录密码");
        } else if (connectType == SshModel.ConnectType.PUBKEY) {
        // Assert.hasText(privateKey, "请填写证书内容");
        }
        sshModel = new SshModel();
    } else {
        sshModel = sshService.getByKey(id);
        Assert.notNull(sshModel, "不存在对应ssh");
    }
    // 目录
    if (StrUtil.isEmpty(fileDirs)) {
        sshModel.fileDirs(null);
    } else {
        List<String> list = StrSplitter.splitTrim(fileDirs, StrUtil.LF, true);
        for (String s : list) {
            String normalize = FileUtil.normalize(s + StrUtil.SLASH);
            int count = StrUtil.count(normalize, StrUtil.SLASH);
            Assert.state(count >= 2, "ssh 授权目录不能是根目录");
        }
        // 
        UserModel userModel = getUser();
        Assert.state(!userModel.isDemoUser(), PermissionInterceptor.DEMO_TIP);
        sshModel.fileDirs(list);
    }
    sshModel.setHost(host);
    // 如果密码传递不为空就设置值 因为上面已经判断了只有修改的情况下 password 才可能为空
    if (StrUtil.isNotEmpty(password)) {
        sshModel.setPassword(password);
    }
    if (StrUtil.startWith(privateKey, URLUtil.FILE_URL_PREFIX)) {
        String rsaPath = StrUtil.removePrefix(privateKey, URLUtil.FILE_URL_PREFIX);
        Assert.state(FileUtil.isFile(rsaPath), "配置的私钥文件不存在");
    }
    if (StrUtil.isNotEmpty(privateKey)) {
        sshModel.setPrivateKey(privateKey);
    }
    sshModel.setPort(port);
    sshModel.setUser(user);
    sshModel.setName(name);
    sshModel.setNotAllowedCommand(notAllowedCommand);
    sshModel.setConnectType(connectType.name());
    // 获取允许编辑的后缀
    String allowEditSuffix = getParameter("allowEditSuffix");
    List<String> allowEditSuffixList = AgentWhitelist.parseToList(allowEditSuffix, "允许编辑的文件后缀不能为空");
    sshModel.allowEditSuffix(allowEditSuffixList);
    try {
        Charset.forName(charset);
        sshModel.setCharset(charset);
    } catch (Exception e) {
        return JsonMessage.getString(405, "请填写正确的编码格式");
    }
    // 判断重复
    HttpServletRequest request = getRequest();
    String workspaceId = sshService.getCheckUserWorkspace(request);
    Entity entity = Entity.create();
    entity.set("host", sshModel.getHost());
    entity.set("port", sshModel.getPort());
    entity.set("workspaceId", workspaceId);
    if (StrUtil.isNotEmpty(id)) {
        entity.set("id", StrUtil.format(" <> {}", id));
    }
    boolean exists = sshService.exists(entity);
    Assert.state(!exists, "对应的SSH已经存在啦");
    try {
        SshModel model = sshService.getByKey(id, false);
        if (model != null) {
            sshModel.setPassword(StrUtil.emptyToDefault(sshModel.getPassword(), model.getPassword()));
            sshModel.setPrivateKey(StrUtil.emptyToDefault(sshModel.getPrivateKey(), model.getPrivateKey()));
        }
        Session session = SshService.getSessionByModel(sshModel);
        JschUtil.close(session);
    } catch (Exception e) {
        return JsonMessage.getString(505, "ssh连接失败:" + e.getMessage());
    }
    if (add) {
        sshService.insert(sshModel);
    } else {
        sshService.update(sshModel);
    }
    return JsonMessage.getString(200, "操作成功");
}
Also used : UserModel(io.jpom.model.data.UserModel) HttpServletRequest(javax.servlet.http.HttpServletRequest) Entity(cn.hutool.db.Entity) SshModel(io.jpom.model.data.SshModel) Session(com.jcraft.jsch.Session) PostMapping(org.springframework.web.bind.annotation.PostMapping) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) ClassFeature(io.jpom.permission.ClassFeature)

Example 20 with UserModel

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

the class MonitorListController method updateMonitor.

/**
 * 增加或修改监控
 *
 * @param id         id
 * @param name       name
 * @param notifyUser user
 * @return json
 */
@RequestMapping(value = "updateMonitor", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.EDIT)
public String updateMonitor(String id, @ValidatorItem(value = ValidatorRule.NOT_BLANK, msg = "监控名称不能为空") String name, @ValidatorItem(msg = "请配置监控周期") String execCron, String notifyUser, String webhook) {
    String status = getParameter("status");
    String autoRestart = getParameter("autoRestart");
    JSONArray jsonArray = JSONArray.parseArray(notifyUser);
    // List<String> notifyUsers = jsonArray.toJavaList(String.class);
    List<String> notifyUserList = jsonArray.toJavaList(String.class);
    if (CollUtil.isNotEmpty(notifyUserList)) {
        for (String userId : notifyUserList) {
            Assert.state(userService.exists(new UserModel(userId)), "没有对应的用户:" + userId);
        }
    }
    String projects = getParameter("projects");
    JSONArray projectsArray = JSONArray.parseArray(projects);
    List<MonitorModel.NodeProject> nodeProjects = projectsArray.toJavaList(MonitorModel.NodeProject.class);
    Assert.notEmpty(nodeProjects, "请至少选择一个节点");
    for (MonitorModel.NodeProject nodeProject : nodeProjects) {
        Assert.notEmpty(nodeProject.getProjects(), "请至少选择一个项目");
        for (String project : nodeProject.getProjects()) {
            boolean exists = projectInfoCacheService.exists(nodeProject.getNode(), project);
            Assert.state(exists, "没有对应的项目:" + project);
        }
    }
    // 设置参数
    if (StrUtil.isNotEmpty(webhook)) {
        Validator.validateMatchRegex(RegexPool.URL_HTTP, webhook, "WebHooks 地址不合法");
    }
    Assert.state(CollUtil.isNotEmpty(notifyUserList) || StrUtil.isNotEmpty(webhook), "请选择一位报警联系人或者填写webhook");
    boolean start = "on".equalsIgnoreCase(status);
    MonitorModel monitorModel = monitorService.getByKey(id);
    if (monitorModel == null) {
        monitorModel = new MonitorModel();
    }
    monitorModel.setAutoRestart("on".equalsIgnoreCase(autoRestart));
    monitorModel.setExecCron(this.checkCron(execCron));
    monitorModel.projects(nodeProjects);
    monitorModel.setStatus(start);
    monitorModel.setWebhook(webhook);
    monitorModel.notifyUser(notifyUserList);
    monitorModel.setName(name);
    if (StrUtil.isEmpty(id)) {
        // 添加监控
        monitorService.insert(monitorModel);
        return JsonMessage.getString(200, "添加成功");
    }
    HttpServletRequest request = getRequest();
    monitorService.updateById(monitorModel, request);
    return JsonMessage.getString(200, "修改成功");
}
Also used : UserModel(io.jpom.model.data.UserModel) HttpServletRequest(javax.servlet.http.HttpServletRequest) MonitorModel(io.jpom.model.data.MonitorModel) JSONArray(com.alibaba.fastjson.JSONArray) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) ClassFeature(io.jpom.permission.ClassFeature) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

UserModel (io.jpom.model.data.UserModel)66 JSONObject (com.alibaba.fastjson.JSONObject)17 MethodFeature (io.jpom.permission.MethodFeature)15 ClassFeature (io.jpom.permission.ClassFeature)14 Feature (io.jpom.permission.Feature)14 NodeModel (io.jpom.model.data.NodeModel)10 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)8 NotLogin (io.jpom.common.interceptor.NotLogin)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 UserService (io.jpom.service.user.UserService)5 GetMapping (org.springframework.web.bind.annotation.GetMapping)5 StrUtil (cn.hutool.core.util.StrUtil)4 Entity (cn.hutool.db.Entity)4 JSONArray (com.alibaba.fastjson.JSONArray)4 BuildInfoModel (io.jpom.model.data.BuildInfoModel)4 List (java.util.List)4 JsonMessage (cn.jiangzeyin.common.JsonMessage)3 BaseServerController (io.jpom.common.BaseServerController)3 WorkspaceModel (io.jpom.model.data.WorkspaceModel)3 UserLoginDto (io.jpom.model.dto.UserLoginDto)3