feat:优化权限和角色

This commit is contained in:
wangyu 2022-03-08 21:55:56 +08:00
parent 2b995f5d56
commit 8e058eb5c3
6 changed files with 100 additions and 4 deletions

View File

@ -1,7 +1,7 @@
package com.flyfish.framework.domain; package com.flyfish.framework.domain;
import com.flyfish.framework.domain.tree.TreeQo;
import com.flyfish.framework.domain.po.Department; import com.flyfish.framework.domain.po.Department;
import com.flyfish.framework.domain.tree.TreeQo;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@ -14,4 +14,8 @@ import lombok.Setter;
@Setter @Setter
public class DepartmentQo extends TreeQo<Department> { public class DepartmentQo extends TreeQo<Department> {
/**
* 额外的参数仅针对getList指定一般用于回显父级部门
*/
private String additional;
} }

View File

@ -6,6 +6,8 @@ import com.flyfish.framework.domain.po.Role;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.util.List;
/** /**
* 角色查询实体 * 角色查询实体
* *
@ -15,10 +17,21 @@ import lombok.Setter;
@Setter @Setter
public class RoleQo extends NameLikeQo<Role> { public class RoleQo extends NameLikeQo<Role> {
/**
* 是否仅管理员可选
*/
private Boolean admin; private Boolean admin;
/**
* 是否系统内置
*/
private Boolean system; private Boolean system;
/**
* 额外的查询拼接仅用于getList用于查询回显
*/
private List<String> additions;
@Override @Override
public CriteriaBuilder<Role> criteriaBuilder() { public CriteriaBuilder<Role> criteriaBuilder() {
return super.criteriaBuilder().with("admin", "system"); return super.criteriaBuilder().with("admin", "system");

View File

@ -10,6 +10,7 @@ import com.flyfish.framework.service.impl.BaseReactiveServiceImpl;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.mongodb.core.ReactiveMongoOperations; import org.springframework.data.mongodb.core.ReactiveMongoOperations;
import org.springframework.data.mongodb.core.aggregation.Aggregation; import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Criteria;
@ -19,10 +20,14 @@ import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
/** /**
* 部门服务 * 部门服务
* *
@ -65,7 +70,9 @@ public class DepartmentService extends BaseReactiveServiceImpl<Department> {
*/ */
private Mono<Integer> getMinDepthInIds(Collection<String> codes) { private Mono<Integer> getMinDepthInIds(Collection<String> codes) {
return reactiveMongoOperations.aggregate( return reactiveMongoOperations.aggregate(
Aggregation.newAggregation(Aggregation.group().min("depth").as("depth")), Aggregation.newAggregation(
Aggregation.match(Criteria.where("_id").in(codes)),
Aggregation.group().min("depth").as("depth")),
Department.class, Department.class) Department.class, Department.class)
.single() .single()
.map(TreeDomain::getDepth); .map(TreeDomain::getDepth);
@ -104,6 +111,12 @@ public class DepartmentService extends BaseReactiveServiceImpl<Department> {
if (null == qo.getParentId() && CollectionUtils.isEmpty(qo.getParentIds())) { if (null == qo.getParentId() && CollectionUtils.isEmpty(qo.getParentIds())) {
qo.setIds(userDetails.getVisibleDeparts()); qo.setIds(userDetails.getVisibleDeparts());
} }
// 如果存在额外的id追加记录
if (StringUtils.isNotBlank(qo.getAdditional())) {
Collection<String> ids = defaultIfNull(qo.getIds(), new HashSet<>());
ids.add(qo.getAdditional());
qo.setIds(ids);
}
return mono; return mono;
} }
} }

View File

@ -5,6 +5,10 @@ import com.flyfish.framework.domain.po.Permission;
import com.flyfish.framework.service.impl.BaseReactiveServiceImpl; import com.flyfish.framework.service.impl.BaseReactiveServiceImpl;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
/**
* 权限服务
* @author wangyu
*/
@Service @Service
public class PermissionService extends BaseReactiveServiceImpl<Permission> { public class PermissionService extends BaseReactiveServiceImpl<Permission> {

View File

@ -2,16 +2,29 @@ package com.flyfish.framework.service;
import com.flyfish.framework.domain.PermissionQo; import com.flyfish.framework.domain.PermissionQo;
import com.flyfish.framework.domain.RoleQo;
import com.flyfish.framework.domain.base.Domain;
import com.flyfish.framework.domain.base.IUser;
import com.flyfish.framework.domain.base.Qo;
import com.flyfish.framework.domain.po.Permission; import com.flyfish.framework.domain.po.Permission;
import com.flyfish.framework.domain.po.Role; import com.flyfish.framework.domain.po.Role;
import com.flyfish.framework.enums.RoleType; import com.flyfish.framework.enums.RoleType;
import com.flyfish.framework.enums.UserType;
import com.flyfish.framework.service.impl.BaseReactiveServiceImpl; import com.flyfish.framework.service.impl.BaseReactiveServiceImpl;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import java.util.Collections; import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;
@Service @Service
@RequiredArgsConstructor @RequiredArgsConstructor
@ -19,6 +32,8 @@ public class RoleService extends BaseReactiveServiceImpl<Role> {
private final PermissionService permissionService; private final PermissionService permissionService;
private final ReactiveMongoOperations reactiveMongoOperations;
/** /**
* 如果是管理员设置拥有所有权限 * 如果是管理员设置拥有所有权限
* *
@ -46,4 +61,42 @@ public class RoleService extends BaseReactiveServiceImpl<Role> {
return super.create(role); return super.create(role);
}); });
} }
/**
* 获取创建的主键集合
*
* @param user 用户
* @return 结果
*/
private Mono<Set<String>> getOwnedIds(IUser user) {
Query query = Query.query(Criteria.where("creatorId").is(user.getId()));
query.fields().include("_id");
return reactiveMongoOperations.find(query, Role.class).map(Domain::getId).collect(Collectors.toSet());
}
/**
* 查询时根据用户过滤可见项
*
* @param query 查询
* @return 结果
*/
@Override
public Flux<Role> getList(Qo<Role> query) {
// 基于创建者进行筛选
if (query instanceof RoleQo) {
IUser user = query.getUser();
if (user.getType() != UserType.SUPER_ADMIN) {
return getOwnedIds(user)
.flatMapMany(ids -> {
RoleQo qo = (RoleQo) query;
qo.setIds(ids);
if (CollectionUtils.isEmpty(qo.getAdditions())) {
qo.getIds().addAll(qo.getAdditions());
}
return super.getList(qo);
});
}
}
return super.getList(query);
}
} }

View File

@ -12,6 +12,7 @@ import org.springframework.web.bind.annotation.PathVariable;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -69,9 +70,17 @@ public abstract class ReactiveTreeController<T extends TreeDomain<T>, Q extends
List<T> roots = filtered.stream() List<T> roots = filtered.stream()
.filter(item -> null != item && TreeDomain.ROOT.equals(item.getParentId())) .filter(item -> null != item && TreeDomain.ROOT.equals(item.getParentId()))
.collect(Collectors.toList()); .collect(Collectors.toList());
// 不是从根开始直接查出 // 不是从根开始找到最小深度结果直接放在根下
if (CollectionUtils.isEmpty(roots)) { if (CollectionUtils.isEmpty(roots)) {
return filtered; int depth = filtered.stream()
.min(Comparator.comparing(TreeDomain::getDepth))
.map(TreeDomain::getDepth)
.orElse(0);
roots = filtered.stream().filter(d -> d.getDepth() == depth).collect(Collectors.toList());
// 如果仍然为空舍弃直接返回
if (CollectionUtils.isEmpty(roots)) {
return filtered;
}
} }
// 第四步根据父id的map填充根tree // 第四步根据父id的map填充根tree
return applyChildren(roots, group); return applyChildren(roots, group);