Search in sources :

Example 36 with UserModel

use of io.jpom.model.data.UserModel 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 37 with UserModel

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

the class LoginControl method renewalToken.

/**
 * 刷新token
 *
 * @return json
 */
@RequestMapping(value = "renewal", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@NotLogin
public String renewalToken() {
    String token = getRequest().getHeader(ServerOpenApi.HTTP_HEAD_AUTHORIZATION);
    if (StrUtil.isEmpty(token)) {
        return JsonMessage.getString(ServerConfigBean.AUTHORIZE_TIME_OUT_CODE, "刷新token失败");
    }
    JWT jwt = JwtUtil.readBody(token);
    if (JwtUtil.expired(jwt, 0)) {
        int renewal = ServerExtConfigBean.getInstance().getAuthorizeRenewal();
        if (jwt == null || renewal <= 0 || JwtUtil.expired(jwt, TimeUnit.MINUTES.toSeconds(renewal))) {
            return JsonMessage.getString(ServerConfigBean.AUTHORIZE_TIME_OUT_CODE, "刷新token超时");
        }
    }
    UserModel userModel = userService.checkUser(JwtUtil.getId(jwt));
    if (userModel == null) {
        return JsonMessage.getString(ServerConfigBean.AUTHORIZE_TIME_OUT_CODE, "没有对应的用户");
    }
    UserLoginDto userLoginDto = userService.getUserJwtId(userModel);
    return JsonMessage.getString(200, "", userLoginDto);
}
Also used : UserModel(io.jpom.model.data.UserModel) JWT(cn.hutool.jwt.JWT) UserLoginDto(io.jpom.model.dto.UserLoginDto) NotLogin(io.jpom.common.interceptor.NotLogin)

Example 38 with UserModel

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

the class LoginControl method mfaVerify.

@GetMapping(value = "mfa_verify", produces = MediaType.APPLICATION_JSON_VALUE)
@NotLogin
public String mfaVerify(String token, String code) {
    String userId = MFA_TOKEN.get(token);
    if (StrUtil.isEmpty(userId)) {
        return JsonMessage.getString(201, "登录信息已经过期请重新登录");
    }
    boolean mfaCode = userService.verifyMfaCode(userId, code);
    Assert.state(mfaCode, "验证码不正确,请重新输入");
    UserModel userModel = userService.getByKey(userId);
    // 
    UserLoginDto userLoginDto = this.createToken(userModel);
    MFA_TOKEN.remove(token);
    return JsonMessage.getString(200, "登录成功", userLoginDto);
}
Also used : UserModel(io.jpom.model.data.UserModel) UserLoginDto(io.jpom.model.dto.UserLoginDto) NotLogin(io.jpom.common.interceptor.NotLogin)

Example 39 with UserModel

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

the class LoginInterceptor method checkHeaderUser.

/**
 * 尝试获取 header 中的信息
 *
 * @param session ses
 * @param request req
 * @return true 获取成功
 */
private int checkHeaderUser(HttpServletRequest request, HttpSession session) {
    String token = request.getHeader(ServerOpenApi.HTTP_HEAD_AUTHORIZATION);
    if (StrUtil.isEmpty(token)) {
        return ServerConfigBean.AUTHORIZE_TIME_OUT_CODE;
    }
    JWT jwt = JwtUtil.readBody(token);
    if (JwtUtil.expired(jwt, 0)) {
        int renewal = ServerExtConfigBean.getInstance().getAuthorizeRenewal();
        if (jwt == null || renewal <= 0 || JwtUtil.expired(jwt, TimeUnit.MINUTES.toSeconds(renewal))) {
            return ServerConfigBean.AUTHORIZE_TIME_OUT_CODE;
        }
        return ServerConfigBean.RENEWAL_AUTHORIZE_CODE;
    }
    UserModel user = (UserModel) session.getAttribute(SESSION_NAME);
    UserService userService = SpringUtil.getBean(UserService.class);
    String id = JwtUtil.getId(jwt);
    UserModel newUser = userService.checkUser(id);
    if (newUser == null) {
        return ServerConfigBean.AUTHORIZE_TIME_OUT_CODE;
    }
    if (null != user) {
        String tokenUserId = JwtUtil.readUserId(jwt);
        boolean b = user.getId().equals(tokenUserId);
        if (!b) {
            return ServerConfigBean.AUTHORIZE_TIME_OUT_CODE;
        }
    }
    session.setAttribute(LoginInterceptor.SESSION_NAME, newUser);
    return 0;
}
Also used : UserModel(io.jpom.model.data.UserModel) UserService(io.jpom.service.user.UserService) JWT(cn.hutool.jwt.JWT)

Example 40 with UserModel

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

the class SystemConfigController method saveNodeConfig.

@PostMapping(value = "save_node_config.json", produces = MediaType.APPLICATION_JSON_VALUE)
@Feature(method = MethodFeature.EDIT)
@SystemPermission(superUser = true)
public String saveNodeConfig(@ValidatorItem(msg = "请选择分发的节点") String nodeIds, String templateNodeId, String content, String restart) {
    Assert.hasText(content, "内容不能为空");
    HttpServletRequest httpServletRequest = getRequest();
    String workspaceId = nodeService.getCheckUserWorkspace(httpServletRequest);
    String id = StrUtil.format("node_config_{}", workspaceId);
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("templateNodeId", templateNodeId);
    jsonObject.put("nodeIds", nodeIds);
    systemParametersServer.upsert(id, jsonObject, id);
    // 
    List<String> nodeIdsStr = StrUtil.splitTrim(nodeIds, StrUtil.LF);
    UserModel user = getUser();
    for (String s : nodeIdsStr) {
        NodeModel byKey = nodeService.getByKey(s, httpServletRequest);
        JSONObject reqData = new JSONObject();
        reqData.put("content", content);
        reqData.put("restart", restart);
        JsonMessage<String> request = NodeForward.request(byKey, NodeUrl.SystemSaveConfig, user, reqData);
        Assert.state(request.getCode() == 200, "分发 " + byKey.getName() + " 节点配置失败" + request.getMsg());
    }
    return JsonMessage.getString(200, "修改成功");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) UserModel(io.jpom.model.data.UserModel) NodeModel(io.jpom.model.data.NodeModel) JSONObject(com.alibaba.fastjson.JSONObject) SystemPermission(io.jpom.permission.SystemPermission) PostMapping(org.springframework.web.bind.annotation.PostMapping) Feature(io.jpom.permission.Feature) MethodFeature(io.jpom.permission.MethodFeature) ClassFeature(io.jpom.permission.ClassFeature)

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