wangwei 6 年 前
コミット
e4fc320e91

+ 49 - 0
examcloud-core-basic-api-provider/src/main/java/cn/com/qmth/examcloud/core/basic/api/provider/RolePrivilegeCloudServiceProvider.java

@@ -7,10 +7,18 @@ import org.springframework.web.bind.annotation.RestController;
 
 import cn.com.qmth.examcloud.commons.web.support.ControllerSupport;
 import cn.com.qmth.examcloud.core.basic.api.RolePrivilegeCloudService;
+import cn.com.qmth.examcloud.core.basic.api.bean.RoleBean;
+import cn.com.qmth.examcloud.core.basic.api.request.DeleteRoleReq;
+import cn.com.qmth.examcloud.core.basic.api.request.SaveRoleReq;
+import cn.com.qmth.examcloud.core.basic.api.response.DeleteRoleResp;
+import cn.com.qmth.examcloud.core.basic.api.response.SaveRoleResp;
 import cn.com.qmth.examcloud.core.basic.dao.PrivilegeGroupRepo;
 import cn.com.qmth.examcloud.core.basic.dao.PrivilegeRepo;
 import cn.com.qmth.examcloud.core.basic.dao.RolePrivilegeRelationRepo;
 import cn.com.qmth.examcloud.core.basic.dao.RoleRepo;
+import cn.com.qmth.examcloud.core.basic.dao.entity.RoleEntity;
+import cn.com.qmth.examcloud.core.basic.service.RolePrivilegeService;
+import cn.com.qmth.examcloud.core.basic.service.bean.RoleInfo;
 
 /**
  * 角色权限服务
@@ -40,4 +48,45 @@ public class RolePrivilegeCloudServiceProvider extends ControllerSupport
 	@Autowired
 	PrivilegeGroupRepo privilegeGroupRepo;
 
+	@Autowired
+	RolePrivilegeService rolePrivilegeService;
+
+	@Override
+	public SaveRoleResp saveRole(SaveRoleReq req) {
+		RoleInfo info = new RoleInfo();
+		info.setCode(req.getCode());
+		info.setName(req.getName());
+		info.setRootOrgId(req.getRootOrgId());
+
+		RoleEntity saved = rolePrivilegeService.saveRole(info);
+
+		RoleBean roleBean = new RoleBean();
+		roleBean.setRoleCode(saved.getCode());
+		roleBean.setRoleId(saved.getId());
+		roleBean.setRoleName(saved.getName());
+
+		SaveRoleResp resp = new SaveRoleResp();
+		resp.setRoleBean(roleBean);
+		return resp;
+	}
+
+	@Override
+	public DeleteRoleResp deleteRole(DeleteRoleReq req) {
+		RoleInfo info = new RoleInfo();
+		info.setCode(req.getCode());
+		info.setName(req.getName());
+		info.setRootOrgId(req.getRootOrgId());
+
+		RoleEntity saved = rolePrivilegeService.deleteRole(info, req.isCascade());
+
+		RoleBean roleBean = new RoleBean();
+		roleBean.setRoleCode(saved.getCode());
+		roleBean.setRoleId(saved.getId());
+		roleBean.setRoleName(saved.getName());
+
+		DeleteRoleResp resp = new DeleteRoleResp();
+		resp.setRoleBean(roleBean);
+		return resp;
+	}
+
 }

+ 4 - 0
examcloud-core-basic-dao/src/main/java/cn/com/qmth/examcloud/core/basic/dao/RolePrivilegeRelationRepo.java

@@ -25,4 +25,8 @@ public interface RolePrivilegeRelationRepo
 	List<RolePrivilegeRelationEntity> findAllByRoleIdInAndRootOrgIdAndPrivilegeId(
 			List<Long> roleIdList, Long rootOrgId, Long privilegeId);
 
+	int countByRoleIdAndRootOrgId(Long roleId, Long rootOrgId);
+
+	void deleteByRoleIdAndRootOrgId(Long roleId, Long rootOrgId);
+
 }

+ 4 - 0
examcloud-core-basic-dao/src/main/java/cn/com/qmth/examcloud/core/basic/dao/UserRoleRelationRepo.java

@@ -26,4 +26,8 @@ public interface UserRoleRelationRepo
 
 	void deleteByUserId(Long userId);
 
+	int countByRoleId(Long roleId);
+
+	void deleteByRoleId(Long roleId);
+
 }

+ 23 - 1
examcloud-core-basic-service/src/main/java/cn/com/qmth/examcloud/core/basic/service/RolePrivilegeService.java

@@ -2,6 +2,9 @@ package cn.com.qmth.examcloud.core.basic.service;
 
 import java.util.Set;
 
+import cn.com.qmth.examcloud.core.basic.dao.entity.RoleEntity;
+import cn.com.qmth.examcloud.core.basic.service.bean.RoleInfo;
+
 /**
  * 类注释
  *
@@ -19,6 +22,25 @@ public interface RolePrivilegeService {
 	 * @param roleId
 	 * @param privilegeIdSet
 	 */
-	void updateRolePrivilegeRelations(Long rootOrgId, Long roleId, Long privilegeGroupId,Set<Long> privilegeIdSet);
+	void updateRolePrivilegeRelations(Long rootOrgId, Long roleId, Long privilegeGroupId,
+			Set<Long> privilegeIdSet);
+
+	/**
+	 * 保存角色
+	 *
+	 * @author WANGWEI
+	 * @param info
+	 * @return
+	 */
+	RoleEntity saveRole(RoleInfo info);
+
+	/**
+	 * 删除角色
+	 *
+	 * @author WANGWEI
+	 * @param info
+	 * @param cascade
+	 */
+	RoleEntity deleteRole(RoleInfo info, boolean cascade);
 
 }

+ 68 - 0
examcloud-core-basic-service/src/main/java/cn/com/qmth/examcloud/core/basic/service/bean/RoleInfo.java

@@ -0,0 +1,68 @@
+package cn.com.qmth.examcloud.core.basic.service.bean;
+
+import cn.com.qmth.examcloud.commons.web.cloud.api.JsonSerializable;
+
+/**
+ * 角色
+ *
+ * @author WANGWEI
+ * @date 2018年5月23日
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
+ */
+public class RoleInfo implements JsonSerializable {
+
+	private static final long serialVersionUID = -2167420238674588632L;
+
+	/**
+	 * 角色ID
+	 */
+	private Long id;
+
+	/**
+	 * 角色码
+	 */
+	private String code;
+
+	/**
+	 * 角色名称
+	 */
+	private String name;
+
+	/**
+	 * 顶级机构.为null时,为全局角色
+	 */
+	private Long rootOrgId;
+
+	public Long getId() {
+		return id;
+	}
+
+	public void setId(Long id) {
+		this.id = id;
+	}
+
+	public String getCode() {
+		return code;
+	}
+
+	public void setCode(String code) {
+		this.code = code;
+	}
+
+	public String getName() {
+		return name;
+	}
+
+	public void setName(String name) {
+		this.name = name;
+	}
+
+	public Long getRootOrgId() {
+		return rootOrgId;
+	}
+
+	public void setRootOrgId(Long rootOrgId) {
+		this.rootOrgId = rootOrgId;
+	}
+
+}

+ 84 - 4
examcloud-core-basic-service/src/main/java/cn/com/qmth/examcloud/core/basic/service/impl/RolePrivilegeServiceImpl.java

@@ -3,21 +3,25 @@ package cn.com.qmth.examcloud.core.basic.service.impl;
 import java.util.List;
 import java.util.Set;
 
-import javax.transaction.Transactional;
-
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import com.google.common.collect.Lists;
 
+import cn.com.qmth.examcloud.commons.base.exception.StatusException;
 import cn.com.qmth.examcloud.core.basic.dao.OrgRepo;
 import cn.com.qmth.examcloud.core.basic.dao.PrivilegeGroupRepo;
 import cn.com.qmth.examcloud.core.basic.dao.PrivilegeRepo;
 import cn.com.qmth.examcloud.core.basic.dao.RolePrivilegeRelationRepo;
+import cn.com.qmth.examcloud.core.basic.dao.RoleRepo;
+import cn.com.qmth.examcloud.core.basic.dao.UserRoleRelationRepo;
 import cn.com.qmth.examcloud.core.basic.dao.entity.PrivilegeEntity;
+import cn.com.qmth.examcloud.core.basic.dao.entity.RoleEntity;
 import cn.com.qmth.examcloud.core.basic.dao.entity.RolePrivilegeRelationEntity;
 import cn.com.qmth.examcloud.core.basic.dao.entity.RolePrivilegeRelationPK;
 import cn.com.qmth.examcloud.core.basic.service.RolePrivilegeService;
+import cn.com.qmth.examcloud.core.basic.service.bean.RoleInfo;
 
 /**
  * 类注释
@@ -27,7 +31,6 @@ import cn.com.qmth.examcloud.core.basic.service.RolePrivilegeService;
  * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
  */
 @Service
-@Transactional
 public class RolePrivilegeServiceImpl implements RolePrivilegeService {
 
 	@Autowired
@@ -36,6 +39,9 @@ public class RolePrivilegeServiceImpl implements RolePrivilegeService {
 	@Autowired
 	OrgRepo orgRepo;
 
+	@Autowired
+	RoleRepo roleRepo;
+
 	@Autowired
 	RolePrivilegeRelationRepo rolePrivilegeRelationRepo;
 
@@ -43,7 +49,7 @@ public class RolePrivilegeServiceImpl implements RolePrivilegeService {
 	PrivilegeGroupRepo privilegeGroupRepo;
 
 	@Autowired
-	RolePrivilegeService rolePrivilegeService;
+	UserRoleRelationRepo userRoleRelationRepo;
 
 	@Override
 	public void updateRolePrivilegeRelations(Long rootOrgId, Long roleId, Long privilegeGroupId,
@@ -74,4 +80,78 @@ public class RolePrivilegeServiceImpl implements RolePrivilegeService {
 		rolePrivilegeRelationRepo.save(list);
 	}
 
+	@Override
+	public RoleEntity saveRole(RoleInfo info) {
+		String code = info.getCode();
+		String name = info.getName();
+		Long rootOrgId = info.getRootOrgId();
+
+		if (StringUtils.isBlank(code)) {
+			throw new StatusException("B-620001", "code is blank");
+		}
+
+		if (StringUtils.isBlank(name)) {
+			throw new StatusException("B-620002", "name is blank");
+		}
+
+		if (null == rootOrgId) {
+			throw new StatusException("B-620003", "rootOrgId is null");
+		}
+
+		RoleEntity roleEntity = roleRepo.findByCodeAndRootOrgId(code, rootOrgId);
+
+		if (null == roleEntity) {
+			roleEntity = new RoleEntity();
+			roleEntity.setCode(code);
+			roleEntity.setName(name);
+			roleEntity.setRootOrgId(rootOrgId);
+		}
+
+		RoleEntity saved = roleRepo.save(roleEntity);
+
+		return saved;
+	}
+
+	@Override
+	public RoleEntity deleteRole(RoleInfo info, boolean cascade) {
+		Long id = info.getId();
+		String code = info.getCode();
+		Long rootOrgId = info.getRootOrgId();
+
+		if (null == rootOrgId) {
+			throw new StatusException("B-620001", "name is blank");
+		}
+
+		RoleEntity roleEntity = null;
+		if (null != id) {
+			roleEntity = roleRepo.findOne(id);
+		} else if (StringUtils.isNotBlank(code)) {
+			roleEntity = roleRepo.findByCodeAndRootOrgId(code, rootOrgId);
+		} else {
+			throw new StatusException("B-620002", "id,code can not be all null");
+		}
+
+		if (null == roleEntity) {
+			throw new StatusException("B-620003", "roleEntity is null");
+		}
+
+		if (!cascade) {
+			if (0 < rolePrivilegeRelationRepo.countByRoleIdAndRootOrgId(roleEntity.getId(),
+					rootOrgId)) {
+				throw new StatusException("B-620004", "角色已关联权限");
+			}
+
+			if (0 < userRoleRelationRepo.countByRoleId(roleEntity.getId())) {
+				throw new StatusException("B-620004", "角色已关联用户");
+			}
+		}
+
+		roleRepo.delete(roleEntity);
+
+		rolePrivilegeRelationRepo.deleteByRoleIdAndRootOrgId(roleEntity.getId(), rootOrgId);
+		userRoleRelationRepo.deleteByRoleId(roleEntity.getId());
+
+		return roleEntity;
+	}
+
 }