|
@@ -1,15 +1,36 @@
|
|
package cn.com.qmth.examcloud.core.basic.api.provider;
|
|
package cn.com.qmth.examcloud.core.basic.api.provider;
|
|
|
|
|
|
|
|
+import java.util.List;
|
|
|
|
+
|
|
|
|
+import org.apache.commons.collections.CollectionUtils;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
|
+import com.google.common.collect.Lists;
|
|
|
|
+
|
|
|
|
+import cn.com.qmth.examcloud.commons.base.exception.StatusException;
|
|
|
|
+import cn.com.qmth.examcloud.commons.web.security.enums.RoleMeta;
|
|
import cn.com.qmth.examcloud.commons.web.support.ControllerSupport;
|
|
import cn.com.qmth.examcloud.commons.web.support.ControllerSupport;
|
|
import cn.com.qmth.examcloud.core.basic.api.UserCloudService;
|
|
import cn.com.qmth.examcloud.core.basic.api.UserCloudService;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.api.bean.RoleBean;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.api.bean.UserBean;
|
|
import cn.com.qmth.examcloud.core.basic.api.request.AddUserReq;
|
|
import cn.com.qmth.examcloud.core.basic.api.request.AddUserReq;
|
|
import cn.com.qmth.examcloud.core.basic.api.request.GetUserReq;
|
|
import cn.com.qmth.examcloud.core.basic.api.request.GetUserReq;
|
|
import cn.com.qmth.examcloud.core.basic.api.response.AddUserResp;
|
|
import cn.com.qmth.examcloud.core.basic.api.response.AddUserResp;
|
|
import cn.com.qmth.examcloud.core.basic.api.response.GetUserResp;
|
|
import cn.com.qmth.examcloud.core.basic.api.response.GetUserResp;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.OrgRepo;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.RoleRepo;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.UserRepo;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.UserRoleRelationRepo;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.constants.Consts;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.entity.Org;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.entity.RoleEntity;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.entity.UserEntity;
|
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.entity.UserRoleRelationEntity;
|
|
import io.swagger.annotations.ApiOperation;
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -19,24 +40,136 @@ import io.swagger.annotations.ApiOperation;
|
|
* @date 2018年7月23日
|
|
* @date 2018年7月23日
|
|
* @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
|
|
* @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
|
|
*/
|
|
*/
|
|
|
|
+@Transactional
|
|
@RestController
|
|
@RestController
|
|
@RequestMapping("${$rmp.cloud.basic}" + "user")
|
|
@RequestMapping("${$rmp.cloud.basic}" + "user")
|
|
public class UserCloudServiceProvider extends ControllerSupport implements UserCloudService {
|
|
public class UserCloudServiceProvider extends ControllerSupport implements UserCloudService {
|
|
|
|
|
|
private static final long serialVersionUID = -7881522922704971610L;
|
|
private static final long serialVersionUID = -7881522922704971610L;
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ UserRepo userRepo;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ OrgRepo orgRepo;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ RoleRepo roleRepo;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ UserRoleRelationRepo userRoleRelationRepo;
|
|
|
|
+
|
|
@ApiOperation(value = "保存用户")
|
|
@ApiOperation(value = "保存用户")
|
|
@PostMapping("addUser")
|
|
@PostMapping("addUser")
|
|
@Override
|
|
@Override
|
|
public AddUserResp addUser(AddUserReq req) {
|
|
public AddUserResp addUser(AddUserReq req) {
|
|
- return null;
|
|
|
|
|
|
+ trim(req);
|
|
|
|
+ Long rootOrgId = req.getRootOrgId();
|
|
|
|
+ String loginName = req.getLoginName();
|
|
|
|
+ UserEntity userEntity = userRepo.findByRootOrgIdAndLoginName(rootOrgId, loginName);
|
|
|
|
+
|
|
|
|
+ if (null != userEntity) {
|
|
|
|
+ throw new StatusException("B-650001", "用户名已经存在");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ userEntity = new UserEntity();
|
|
|
|
+ userEntity.setLoginName(loginName);
|
|
|
|
+ userEntity.setRootOrgId(rootOrgId);
|
|
|
|
+ if (StringUtils.isBlank(req.getPassword())) {
|
|
|
|
+ userEntity.setPassword(Consts.DEFAULT_PASSWORD);
|
|
|
|
+ } else {
|
|
|
|
+ userEntity.setPassword(req.getPassword());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ UserEntity saved = userRepo.save(userEntity);
|
|
|
|
+
|
|
|
|
+ List<String> roleCodeList = req.getRoleCodeList();
|
|
|
|
+
|
|
|
|
+ if (CollectionUtils.isEmpty(roleCodeList)) {
|
|
|
|
+ throw new StatusException("B-650002", "角色不能为空");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ List<UserRoleRelationEntity> userRoles = Lists.newArrayList();
|
|
|
|
+ for (String roleCode : roleCodeList) {
|
|
|
|
+ RoleEntity roleEntity = roleRepo.findByCode(roleCode);
|
|
|
|
+ if (null == roleEntity) {
|
|
|
|
+ throw new StatusException("B-002002", "role code is wrong. roleCode=" + roleCode);
|
|
|
|
+ }
|
|
|
|
+ if (roleEntity.getCode().equals(RoleMeta.SUPER_ADMIN.name())) {
|
|
|
|
+ throw new StatusException("B-150007", "不允许新增或修改超级管理员");
|
|
|
|
+ }
|
|
|
|
+ UserRoleRelationEntity relation = new UserRoleRelationEntity(saved.getId(),
|
|
|
|
+ roleEntity.getId(), roleEntity.getCode());
|
|
|
|
+ userRoles.add(relation);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ userRoleRelationRepo.deleteByUserId(saved.getId());
|
|
|
|
+ userRoleRelationRepo.save(userRoles);
|
|
|
|
+ AddUserResp resp = new AddUserResp();
|
|
|
|
+ resp.setUserId(saved.getId());
|
|
|
|
+
|
|
|
|
+ return resp;
|
|
}
|
|
}
|
|
|
|
|
|
@ApiOperation(value = "获取用户")
|
|
@ApiOperation(value = "获取用户")
|
|
@PostMapping("getUser")
|
|
@PostMapping("getUser")
|
|
@Override
|
|
@Override
|
|
public GetUserResp getUser(GetUserReq req) {
|
|
public GetUserResp getUser(GetUserReq req) {
|
|
- return null;
|
|
|
|
|
|
+ Long rootOrgId = req.getRootOrgId();
|
|
|
|
+ String loginName = req.getLoginName();
|
|
|
|
+ UserEntity userEntity = userRepo.findByRootOrgIdAndLoginName(rootOrgId, loginName);
|
|
|
|
+
|
|
|
|
+ GetUserResp resp = new GetUserResp();
|
|
|
|
+ if (null == userEntity) {
|
|
|
|
+ resp.setExisting(false);
|
|
|
|
+ return resp;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ resp.setExisting(true);
|
|
|
|
+
|
|
|
|
+ UserBean userBean = new UserBean();
|
|
|
|
+ userBean.setDisplayName(userEntity.getLoginName() + " (" + userEntity.getName() + ")");
|
|
|
|
+ userBean.setOrgId(userEntity.getOrgId());
|
|
|
|
+ userBean.setRootOrgId(userEntity.getRootOrgId());
|
|
|
|
+ Org rootOrg = orgRepo.findOne(userBean.getRootOrgId());
|
|
|
|
+ userBean.setRootOrgName(rootOrg.getName());
|
|
|
|
+
|
|
|
|
+ if (null != userBean.getOrgId()) {
|
|
|
|
+ Org org = orgRepo.findOne(userBean.getOrgId());
|
|
|
|
+ userBean.setOrgName(org.getName());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ List<RoleBean> roleList = getUserRoles(userEntity.getId());
|
|
|
|
+ userBean.setRoleList(roleList);
|
|
|
|
+
|
|
|
|
+ resp.setUserBean(userBean);
|
|
|
|
+ return resp;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取角色集合
|
|
|
|
+ *
|
|
|
|
+ * @author WANGWEI
|
|
|
|
+ * @param userId
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ private List<RoleBean> getUserRoles(Long userId) {
|
|
|
|
+ List<UserRoleRelationEntity> relationList = userRoleRelationRepo.findAllByUserId(userId);
|
|
|
|
+ List<RoleBean> roleList = Lists.newArrayList();
|
|
|
|
+ if (CollectionUtils.isNotEmpty(relationList)) {
|
|
|
|
+ for (UserRoleRelationEntity cur : relationList) {
|
|
|
|
+ String roleCode = cur.getRoleCode();
|
|
|
|
+ RoleEntity roleEntity = roleRepo.findByCode(roleCode);
|
|
|
|
+ if (null == roleEntity) {
|
|
|
|
+ throw new StatusException("B-002002",
|
|
|
|
+ "role code is wrong. roleCode=" + roleCode);
|
|
|
|
+ }
|
|
|
|
+ RoleBean role = new RoleBean(roleEntity.getId(), roleEntity.getCode(),
|
|
|
|
+ roleEntity.getName());
|
|
|
|
+ roleList.add(role);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return roleList;
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|