|
|
@@ -1,28 +1,37 @@
|
|
|
package com.zhongshu.card.server.core.service.org;
|
|
|
|
|
|
import com.github.microservice.auth.client.content.ResultState;
|
|
|
+import com.github.microservice.auth.client.model.AuthResourcesNameModel;
|
|
|
import com.github.microservice.auth.client.model.RoleGroupModel;
|
|
|
+import com.github.microservice.auth.client.service.AuthResourcesNameService;
|
|
|
+import com.github.microservice.auth.security.type.AuthType;
|
|
|
import com.github.microservice.components.data.base.util.PageEntityUtil;
|
|
|
import com.zhongshu.card.client.model.org.*;
|
|
|
import com.zhongshu.card.client.ret.ResultContent;
|
|
|
import com.zhongshu.card.client.ret.ResultMessage;
|
|
|
import com.zhongshu.card.client.utils.type.DataState;
|
|
|
+import com.zhongshu.card.client.utils.type.EditType;
|
|
|
import com.zhongshu.card.client.utils.type.RoleType;
|
|
|
+import com.zhongshu.card.server.core.dao.org.OrganizationDao;
|
|
|
import com.zhongshu.card.server.core.dao.org.RoleDao;
|
|
|
+import com.zhongshu.card.server.core.domain.org.Organization;
|
|
|
import com.zhongshu.card.server.core.domain.org.Role;
|
|
|
import com.zhongshu.card.server.core.service.base.SuperService;
|
|
|
import com.zhongshu.card.server.core.util.BeanUtils;
|
|
|
+import com.zhongshu.card.server.core.util.CommonUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.data.domain.PageRequest;
|
|
|
import org.springframework.data.domain.Pageable;
|
|
|
+import org.springframework.data.domain.Sort;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.Assert;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
@@ -39,6 +48,12 @@ public class RoleServiceImpl extends SuperService {
|
|
|
@Autowired
|
|
|
com.github.microservice.auth.client.service.RoleService roleService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ OrganizationDao organizationDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AuthResourcesNameService authResourcesNameService;
|
|
|
+
|
|
|
/**
|
|
|
* 添加/编辑角色
|
|
|
*
|
|
|
@@ -204,6 +219,142 @@ public class RoleServiceImpl extends SuperService {
|
|
|
return ResultContent.buildSuccess(models);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 得到角色的权限数据
|
|
|
+ *
|
|
|
+ * @param roleId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ResultContent getList(String roleId) {
|
|
|
+ Role roleObj = roleDao.findTopById(roleId);
|
|
|
+ if (roleObj == null) {
|
|
|
+ return ResultContent.buildFail("角色不存在");
|
|
|
+ }
|
|
|
+ Organization organization = organizationDao.findTopByOid(roleObj.getOid());
|
|
|
+ if (organization == null) {
|
|
|
+ return ResultContent.buildFail("企业不存在");
|
|
|
+ }
|
|
|
+ //获取该岗位的现在所有的权限
|
|
|
+ var roleGroup = roleService.getRoleGroup(roleObj.getRoleGroupId()).getContent();
|
|
|
+ Set<String> postAuth = new HashSet<>();
|
|
|
+ if (!CollectionUtils.isEmpty(roleGroup.getRoleId())) {
|
|
|
+ roleGroup.getRoleId().parallelStream().forEach(it -> {
|
|
|
+ var role = roleService.getRole(it).getContent();
|
|
|
+ if (role != null && !CollectionUtils.isEmpty(role.getAuth())) {
|
|
|
+ postAuth.addAll(role.getAuth());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ return ResultContent.buildContent(buildAuthList(organization.getAuthType(), postAuth));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建权限树
|
|
|
+ *
|
|
|
+ * @param organizationType
|
|
|
+ * @param postAuth
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ List<ModularModel> buildAuthList(AuthType organizationType, Set<String> postAuth) {
|
|
|
+ //获取系统所有权限
|
|
|
+ List<AuthResourcesNameModel> authResourcesNameModels = new ArrayList<>();
|
|
|
+ authResourcesNameModels = authResourcesNameService.list(
|
|
|
+ organizationType,
|
|
|
+ PageRequest.of(0, 9999,
|
|
|
+ Sort.by(Sort.Order.desc("createTime")))).getContent().getContent();
|
|
|
+ List<AuthModel> authModels = new ArrayList<>();
|
|
|
+ authResourcesNameModels.forEach(it -> {
|
|
|
+ String remark = it.getRemark();
|
|
|
+ if (StringUtils.isNotEmpty(remark)) {
|
|
|
+ String[] split = remark.split("_");
|
|
|
+ if (split.length >= 2) {
|
|
|
+ AuthModel authModel = new AuthModel();
|
|
|
+ authModel.setValue(it.getName());
|
|
|
+ authModel.setName(split[1]);
|
|
|
+ authModel.setModularName(split[0]);
|
|
|
+ int index = 1;
|
|
|
+ if (split.length >= 3) {
|
|
|
+ index = CommonUtil.turnStr2Inter(split[2]);
|
|
|
+ }
|
|
|
+ authModel.setIndex(index);
|
|
|
+ if (postAuth.contains(it.getName())) {
|
|
|
+ authModel.setSelect(true);
|
|
|
+ }
|
|
|
+ authModels.add(authModel);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ Comparator comparator = new Comparator<AuthModel>() {
|
|
|
+ @Override
|
|
|
+ public int compare(AuthModel o1, AuthModel o2) {
|
|
|
+ return Integer.compare(o1.getIndex(), o2.getIndex());
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ Map<String, List<AuthModel>> maps = authModels.stream().collect(Collectors.groupingBy(AuthModel::getModularName));
|
|
|
+ List<ModularModel> modularModels = new ArrayList<>();
|
|
|
+ maps.forEach((key, val) -> {
|
|
|
+ ModularModel modularModel = new ModularModel();
|
|
|
+ modularModel.setName(key);
|
|
|
+ // 排序
|
|
|
+ if (ObjectUtils.isNotEmpty(val)) {
|
|
|
+ val.sort(comparator);
|
|
|
+ }
|
|
|
+ modularModel.setAuthModels(val);
|
|
|
+ modularModels.add(modularModel);
|
|
|
+ });
|
|
|
+ return modularModels;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 改变角色权限
|
|
|
+ *
|
|
|
+ * @param param
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public ResultContent editRoleAuth(EditAuthParam param) {
|
|
|
+ Role role = roleDao.findTopById(param.getRoleId());
|
|
|
+ if (role == null) {
|
|
|
+ return ResultContent.buildFail("角色不存在");
|
|
|
+ }
|
|
|
+ var roleGroup = roleService.getRoleGroup(role.getRoleGroupId()).getContent();
|
|
|
+ com.github.microservice.auth.client.model.RoleModel roleModel;
|
|
|
+ if (roleGroup.getRoleId() == null || CollectionUtils.isEmpty(roleGroup.getRoleId())) {
|
|
|
+ // 如果角色不存在,则创建角色
|
|
|
+ var roleByName = roleService.getRoleByName(role.getName(), role.getOid());
|
|
|
+ if (roleByName.getState() != com.github.microservice.auth.client.content.ResultState.Success) {
|
|
|
+ roleModel = new com.github.microservice.auth.client.model.RoleModel();
|
|
|
+ roleModel.setOrganizationId(role.getOid());
|
|
|
+ roleModel.setName(role.getName());
|
|
|
+ roleModel.setRemark(role.getRemark());
|
|
|
+ com.github.microservice.auth.client.content.ResultContent<String> updateRole = roleService.updateRole(roleModel);
|
|
|
+ roleModel.setId(updateRole.getContent());
|
|
|
+ } else {
|
|
|
+ roleModel = roleByName.getContent();
|
|
|
+ }
|
|
|
+ // 权限中心角色绑定角色组
|
|
|
+ roleService.addRoleToRoleGroup(roleGroup.getId(), new String[]{roleModel.getId()});
|
|
|
+ } else {
|
|
|
+ // 角色存在,查询角色信息
|
|
|
+ List<String> roleIds = new ArrayList<>(roleGroup.getRoleId());
|
|
|
+ roleModel = roleService.getRole(roleIds.get(0)).getContent();
|
|
|
+ }
|
|
|
+ Set<String> auth = roleModel.getAuth();
|
|
|
+ if (CollectionUtils.isEmpty(auth)) {
|
|
|
+ auth = new HashSet<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (param.getType() == EditType.Add) {
|
|
|
+ auth.addAll(param.getAuths());
|
|
|
+ } else {
|
|
|
+ auth.removeAll(param.getAuths());
|
|
|
+ }
|
|
|
+ roleModel.setAuth(auth);
|
|
|
+ roleService.updateRole(roleModel);
|
|
|
+ return ResultContent.buildSuccess(roleModel);
|
|
|
+ }
|
|
|
+
|
|
|
public RoleModel toModel(Role role) {
|
|
|
RoleModel roleModel = new RoleModel();
|
|
|
if (ObjectUtils.isNotEmpty(role)) {
|