wangwei 7 yıl önce
ebeveyn
işleme
8e99540879

+ 62 - 17
examcloud-core-basic-api-provider/src/main/java/cn/com/qmth/examcloud/core/basic/api/controller/UserController.java

@@ -41,6 +41,7 @@ import cn.com.qmth.examcloud.commons.web.security.enums.RoleMeta;
 import cn.com.qmth.examcloud.commons.web.security.enums.UacPolicy;
 import cn.com.qmth.examcloud.commons.web.support.ControllerSupport;
 import cn.com.qmth.examcloud.core.basic.api.controller.bean.FullUserInfo;
+import cn.com.qmth.examcloud.core.basic.api.controller.bean.UserForm;
 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;
@@ -48,6 +49,7 @@ 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.UserRole;
+import cn.com.qmth.examcloud.core.basic.dao.enums.UserScope;
 import cn.com.qmth.examcloud.core.basic.dao.enums.UserType;
 import cn.com.qmth.examcloud.core.basic.service.bean.UserInfo;
 import cn.com.qmth.examcloud.core.basic.service.impl.UserServiceImpl;
@@ -75,12 +77,12 @@ public class UserController extends ControllerSupport {
 	@ApiOperation(value = "查询所有用户", notes = "")
 	@GetMapping("/all/{curPage}/{pageSize}")
 	public PageInfo<FullUserInfo> getAllUser(@PathVariable Integer curPage,
-			@PathVariable Integer pageSize, @RequestParam Long orgId,
+			@PathVariable Integer pageSize, @RequestParam Long rootOrgId,
 			@RequestParam String loginName, @RequestParam String name, @RequestParam String enable,
 			@RequestParam Long roleId, @RequestParam String type) {
 
 		cn.com.qmth.examcloud.commons.web.security.bean.User accessUser = getAccessUser();
-		if ((!isSuperAdmin()) && (!orgId.equals(accessUser.getRootOrgId()))) {
+		if ((!isSuperAdmin()) && (!rootOrgId.equals(accessUser.getRootOrgId()))) {
 			throw new StatusException("B-150001", "非法请求");
 		}
 
@@ -97,7 +99,7 @@ public class UserController extends ControllerSupport {
 		loginName = "%" + loginName.trim() + "%";
 		name = "%" + name.trim() + "%";
 
-		Page<UserEntity> userList = userRepo.findAll(orgId, name, loginName, type, roleCode,
+		Page<UserEntity> userList = userRepo.findAll(rootOrgId, name, loginName, type, roleCode,
 				pageable);
 		Iterator<UserEntity> iterator = userList.iterator();
 
@@ -150,27 +152,70 @@ public class UserController extends ControllerSupport {
 		return new ResponseEntity(userList, HttpStatus.OK);
 	}
 
+	/**
+	 * 重构 2018年6月26日
+	 *
+	 * @author WANGWEI
+	 * @param userForm
+	 * @return
+	 */
 	@ApiOperation(value = "新增用户", notes = "新增")
 	@PostMapping
-	public ResponseEntity addUser(@RequestBody UserEntity user, HttpServletRequest request) {
+	public Long addUser(@RequestBody UserForm userForm) {
 		cn.com.qmth.examcloud.commons.web.security.bean.User accessUser = getAccessUser();
-		if (accessUser != null) {
-			if (accessUser.getRootOrgId() != 0) {
-				user.setRootOrgId(accessUser.getRootOrgId());
-			} else {
-				user.setRootOrgId(user.getOrgId());
-			}
-		} else {
-			return new ResponseEntity(HttpStatus.NOT_FOUND);
+		Long rootOrgId = userForm.getRootOrgId();
+		Org org = orgRepo.findOne(rootOrgId);
+		if (null == org) {
+			throw new StatusException("B-150003", "机构不存在");
 		}
-		try {
-			return new ResponseEntity(userService.save(user), HttpStatus.CREATED);
-		} catch (Exception e) {
-			return new ResponseEntity(new ErrorMsg(e.getMessage()),
-					HttpStatus.INTERNAL_SERVER_ERROR);
+		if (0 != org.getParentId()) {
+			throw new StatusException("B-150004", "机构错误");
 		}
+
+		if ((!isSuperAdmin()) && (!rootOrgId.equals(accessUser.getRootOrgId()))) {
+			throw new StatusException("B-150005", "无权操作");
+		}
+
+		UserEntity userEntity = new UserEntity();
+		userEntity.setEnable(userForm.getEnable());
+		userEntity.setLoginName(userForm.getLoginName());
+		userEntity.setMobile(userForm.getMobile());
+		userEntity.setName(userForm.getName());
+		userEntity.setOrgId(rootOrgId);
+		userEntity.setRootOrgId(rootOrgId);
+		userEntity.setPassword(userForm.getPassword());
+		userEntity.setScope(UserScope.ORG);
+		userEntity.setType(UserType.NOT_STUDENT);
+
+		List<UserRole> userRoles = Lists.newArrayList();
+
+		List<Long> roleIds = userForm.getRoleIds();
+		for (Long cur : roleIds) {
+			RoleEntity curRoleEntity = roleRepo.findOne(cur);
+			if (null == curRoleEntity) {
+				throw new StatusException("B-150006", "角色错误");
+			}
+			if (curRoleEntity.getCode().equals(RoleMeta.SUPER_ADMIN.getCode())) {
+				throw new StatusException("B-150007", "不允许添加超级管理员");
+			}
+			UserRole userRole = new UserRole(curRoleEntity.getCode());
+			userRoles.add(userRole);
+		}
+		userEntity.setUserRoles(userRoles);
+
+		UserEntity saved = userService.save(userEntity);
+
+		return saved.getId();
 	}
 
+	/**
+	 * 重构 2018年6月26日
+	 *
+	 * @author WANGWEI
+	 * @param user
+	 * @param request
+	 * @return
+	 */
 	@ApiOperation(value = "更新用户", notes = "更新")
 	@PutMapping
 	public UserEntity updateUser(@RequestBody UserEntity user, HttpServletRequest request) {

+ 128 - 0
examcloud-core-basic-api-provider/src/main/java/cn/com/qmth/examcloud/core/basic/api/controller/bean/UserForm.java

@@ -0,0 +1,128 @@
+package cn.com.qmth.examcloud.core.basic.api.controller.bean;
+
+import java.util.List;
+
+import cn.com.qmth.examcloud.commons.web.cloud.api.JsonSerializable;
+
+/**
+ * 用户表单
+ *
+ * @author WANGWEI
+ * @date 2018年6月25日
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
+ */
+public class UserForm implements JsonSerializable {
+
+	private static final long serialVersionUID = -1326157272669439949L;
+
+	private Long id;
+
+	private Long rootOrgId;
+
+	private String rootOrgName;
+
+	private Long orgId;
+
+	private String orgName;
+
+	private String name;
+
+	private String loginName;
+
+	private String mobile;
+
+	private Boolean enable;
+
+	private String password;
+
+	private List<Long> roleIds;
+
+	public Long getId() {
+		return id;
+	}
+
+	public void setId(Long id) {
+		this.id = id;
+	}
+
+	public String getPassword() {
+		return password;
+	}
+
+	public void setPassword(String password) {
+		this.password = password;
+	}
+
+	public Long getRootOrgId() {
+		return rootOrgId;
+	}
+
+	public void setRootOrgId(Long rootOrgId) {
+		this.rootOrgId = rootOrgId;
+	}
+
+	public String getRootOrgName() {
+		return rootOrgName;
+	}
+
+	public void setRootOrgName(String rootOrgName) {
+		this.rootOrgName = rootOrgName;
+	}
+
+	public Long getOrgId() {
+		return orgId;
+	}
+
+	public void setOrgId(Long orgId) {
+		this.orgId = orgId;
+	}
+
+	public String getOrgName() {
+		return orgName;
+	}
+
+	public void setOrgName(String orgName) {
+		this.orgName = orgName;
+	}
+
+	public String getName() {
+		return name;
+	}
+
+	public void setName(String name) {
+		this.name = name;
+	}
+
+	public String getLoginName() {
+		return loginName;
+	}
+
+	public void setLoginName(String loginName) {
+		this.loginName = loginName;
+	}
+
+	public Boolean getEnable() {
+		return enable;
+	}
+
+	public void setEnable(Boolean enable) {
+		this.enable = enable;
+	}
+
+	public String getMobile() {
+		return mobile;
+	}
+
+	public void setMobile(String mobile) {
+		this.mobile = mobile;
+	}
+
+	public List<Long> getRoleIds() {
+		return roleIds;
+	}
+
+	public void setRoleIds(List<Long> roleIds) {
+		this.roleIds = roleIds;
+	}
+
+}

+ 1 - 2
examcloud-core-basic-service/src/main/java/cn/com/qmth/examcloud/core/basic/service/impl/UserServiceImpl.java

@@ -502,8 +502,7 @@ public class UserServiceImpl  implements UserService{
 		return specification;
 	}
 	
-	public UserEntity save(UserEntity user) throws Exception{
-        user.setScope(UserScope.ORG);
+	public UserEntity save(UserEntity user){
         user.setCreateTime(new Date());
 		checkLoginName(user.getRootOrgId(), user.getLoginName());
 		return userRepo.save(user);