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);
}
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);
}
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);
}
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;
}
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, "修改成功");
}
Aggregations