Skip to content

Commit

Permalink
[apache#1255] data permission complete (apache#1349)
Browse files Browse the repository at this point in the history
* add data_permission SQL and store userId and userName to token

* Use aop to implement data permission interception

* modify aop

* delete Slf4j

* install complete

* merged data permission mvc code

merged data permission mvc code

* modify data permission code

* modify data permission code

* refactor: delete unuse method and split method about data permission

* fix: fix unit test pass not pass

* modify admin constants

* reactor code

* merged plutokaito code

* feat: create api for query data permissions

* feat: refactor create and delete api that included selector/rule data permission

* fix bug for empty selector and rule

* add selector check data permission

* Update fontend resource deal with  data access for soul-admin apache#1255 (apache#18)

* data permission complete

* data permission complete

* add user info error code

* Update frontend resource  with  data access for soul-admin apache#1255 (apache#19)

* Update fontend resource deal with  data access for soul-admin apache#1255

* Update frontend resource  with  data access for soul-admin apache#1255

* delete common error code 401

* update soul-dashboard

* modify code

Co-authored-by: kaitoShy <[email protected]>
Co-authored-by: fengzhenbing <[email protected]>
  • Loading branch information
3 people authored Apr 28, 2021
1 parent 9dfdfe7 commit 9130d47
Show file tree
Hide file tree
Showing 41 changed files with 1,465 additions and 75 deletions.
5 changes: 5 additions & 0 deletions soul-admin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@
<version>${curator-test.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
import org.dromara.soul.admin.model.page.PageParameter;
import org.dromara.soul.admin.model.query.DashboardUserQuery;
import org.dromara.soul.admin.model.result.SoulAdminResult;
import org.dromara.soul.admin.model.vo.DashboardUserEditVO;
import org.dromara.soul.admin.model.vo.DashboardUserVO;
import org.dromara.soul.admin.service.DashboardUserService;
import org.dromara.soul.admin.utils.AesUtils;
import org.dromara.soul.admin.utils.SoulResultMessage;
import org.dromara.soul.admin.model.vo.DashboardUserEditVO;
import org.dromara.soul.admin.model.vo.DashboardUserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.dromara.soul.admin.controller;

import org.dromara.soul.admin.model.dto.DataPermissionDTO;
import org.dromara.soul.admin.model.page.CommonPager;
import org.dromara.soul.admin.model.page.PageParameter;
import org.dromara.soul.admin.model.query.RuleQuery;
import org.dromara.soul.admin.model.query.SelectorQuery;
import org.dromara.soul.admin.model.result.SoulAdminResult;
import org.dromara.soul.admin.model.vo.DataPermissionPageVO;
import org.dromara.soul.admin.service.DataPermissionService;
import org.dromara.soul.admin.utils.SoulResultMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;


/**
* this is dataPermission controller.
*
* @author kaitoShy
*/
@RestController
@RequestMapping("/data-permission")
public class DataPermissionController {

private final DataPermissionService dataPermissionService;

@Autowired(required = false)
public DataPermissionController(final DataPermissionService dataPermissionService) {
this.dataPermissionService = dataPermissionService;
}

/**
* Query paginated selectors with data permission.
* @param currentPage current page
* @param pageSize page size
* @param userId user id
* @param pluginId plugin id
* @return {@linkplain SoulAdminResult}
*/
@GetMapping("/selector")
public SoulAdminResult listPageSelectorDataPermissions(@RequestParam("currentPage") final Integer currentPage,
@RequestParam("pageSize") final Integer pageSize,
@RequestParam("userId") final String userId,
@RequestParam("pluginId") final String pluginId) {
CommonPager<DataPermissionPageVO> selectorList = dataPermissionService.listSelectorsByPage(
new SelectorQuery(pluginId, new PageParameter(currentPage, pageSize)), userId);
return SoulAdminResult.success(SoulResultMessage.QUERY_SUCCESS, selectorList);
}


/**
* Query paginated rules with data permission.
* @param currentPage current page
* @param pageSize page size
* @param userId user id
* @param selectorId selector id
* @return {@linkplain SoulAdminResult}
*/
@GetMapping("/rules")
public SoulAdminResult listPageRuleDataPermissions(@RequestParam("currentPage") final Integer currentPage,
@RequestParam("pageSize") final Integer pageSize,
@RequestParam("userId") final String userId,
@RequestParam("selectorId") final String selectorId) {
CommonPager<DataPermissionPageVO> selectorList = dataPermissionService.listRulesByPage(
new RuleQuery(selectorId, new PageParameter(currentPage, pageSize)), userId);
return SoulAdminResult.success(SoulResultMessage.QUERY_SUCCESS, selectorList);
}


/**
* create selector data permission.
* @param dataPermissionDTO {@linkplain DataPermissionDTO}
* @return effect rows count
*/
@PostMapping("/selector")
public SoulAdminResult saveSelector(@RequestBody final DataPermissionDTO dataPermissionDTO) {
return Optional.ofNullable(dataPermissionDTO)
.map(item -> SoulAdminResult.success(SoulResultMessage.SAVE_SUCCESS, dataPermissionService.createSelector(dataPermissionDTO)))
.orElse(SoulAdminResult.error(SoulResultMessage.SAVE_FAILED));

}

/**
* Delete selector data permission.
* @param dataPermissionDTO {@linkplain DataPermissionDTO}
* @return effect rows count
*/
@DeleteMapping("/selector")
public SoulAdminResult deleteSelector(@RequestBody final DataPermissionDTO dataPermissionDTO) {
return Optional.ofNullable(dataPermissionDTO)
.map(item -> SoulAdminResult.success(SoulResultMessage.DELETE_SUCCESS, dataPermissionService.deleteSelector(dataPermissionDTO)))
.orElse(SoulAdminResult.error(SoulResultMessage.DELETE_SUCCESS));

}

/**
* Delete rule data permission.
* @param dataPermissionDTO {@linkplain DataPermissionDTO}
* @return effect rows count
*/
@PostMapping("/rule")
public SoulAdminResult saveRule(@RequestBody final DataPermissionDTO dataPermissionDTO) {
return Optional.ofNullable(dataPermissionDTO)
.map(item -> SoulAdminResult.success(SoulResultMessage.SAVE_SUCCESS, dataPermissionService.createRule(dataPermissionDTO)))
.orElse(SoulAdminResult.error(SoulResultMessage.SAVE_FAILED));
}

/**
* Delete selector data permission.
* @param dataPermissionDTO {@linkplain DataPermissionDTO}
* @return effect rows count
*/
@DeleteMapping("/rule")
public SoulAdminResult deleteRule(@RequestBody final DataPermissionDTO dataPermissionDTO) {
return Optional.ofNullable(dataPermissionDTO)
.map(item -> SoulAdminResult.success(SoulResultMessage.DELETE_SUCCESS, dataPermissionService.deleteRule(dataPermissionDTO)))
.orElse(SoulAdminResult.error(SoulResultMessage.DELETE_SUCCESS));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import org.dromara.soul.admin.model.page.PageParameter;
import org.dromara.soul.admin.model.query.RuleQuery;
import org.dromara.soul.admin.model.result.SoulAdminResult;
import org.dromara.soul.admin.model.vo.RuleVO;
import org.dromara.soul.admin.service.RuleService;
import org.dromara.soul.admin.utils.SoulResultMessage;
import org.dromara.soul.admin.model.vo.RuleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -66,6 +66,7 @@ public RuleController(final RuleService ruleService) {
public SoulAdminResult queryRules(final String selectorId, final Integer currentPage, final Integer pageSize) {
CommonPager<RuleVO> commonPager = ruleService.listByPage(new RuleQuery(selectorId, new PageParameter(currentPage, pageSize)));
return SoulAdminResult.success(SoulResultMessage.QUERY_SUCCESS, commonPager);

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import org.dromara.soul.admin.model.page.PageParameter;
import org.dromara.soul.admin.model.query.SelectorQuery;
import org.dromara.soul.admin.model.result.SoulAdminResult;
import org.dromara.soul.admin.model.vo.SelectorVO;
import org.dromara.soul.admin.service.SelectorService;
import org.dromara.soul.admin.utils.SoulResultMessage;
import org.dromara.soul.admin.model.vo.SelectorVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -66,6 +66,7 @@ public SelectorController(final SelectorService selectorService) {
public SoulAdminResult querySelectors(final String pluginId, final Integer currentPage, final Integer pageSize) {
CommonPager<SelectorVO> commonPager = selectorService.listByPage(new SelectorQuery(pluginId, new PageParameter(currentPage, pageSize)));
return SoulAdminResult.success(SoulResultMessage.QUERY_SUCCESS, commonPager);

}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.dromara.soul.admin.interceptor;

import lombok.SneakyThrows;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.dromara.soul.admin.interceptor.annotation.DataPermission;
import org.dromara.soul.admin.model.query.RuleQuery;
import org.dromara.soul.admin.model.query.SelectorQuery;
import org.dromara.soul.admin.service.DataPermissionService;
import org.dromara.soul.admin.utils.JwtUtils;
import org.dromara.soul.common.constant.AdminConstants;
import org.springframework.stereotype.Component;

import java.util.List;

/**
* data permission aop interceptor.
*
* @author nuo-promise
*/
@Aspect
@Component
public class DataPermissionInterceptor {

private final DataPermissionService dataPermissionService;

public DataPermissionInterceptor(final DataPermissionService dataPermissionService) {
this.dataPermissionService = dataPermissionService;
}

/**
* define data permission aop point cut.
*/
@Pointcut("@annotation(org.dromara.soul.admin.interceptor.annotation.DataPermission)")
public void dataPermissionCut() { }


/**
* Real method processing around.
*
* @param point point {@link ProceedingJoinPoint}
* @return result {@link Object}
*/
@SneakyThrows
@Around("dataPermissionCut()")
public Object around(final ProceedingJoinPoint point) {
return point.proceed(getFilterSQLData(point));
}

/**
* Organize SQL parameters with data permissions.
*
* @param point {@link ProceedingJoinPoint}
* @return args {@link List}
*/
private Object[] getFilterSQLData(final ProceedingJoinPoint point) {
Object[] args = point.getArgs();
List<String> dataPermissionList = dataPermissionService.getDataPermission(JwtUtils.getUserId());
if (dataPermissionList.size() > 0) {
DataPermission dataPermission = ((MethodSignature) point.getSignature()).getMethod().getAnnotation(DataPermission.class);
if (dataPermission != null && args != null) {
if (dataPermission.dataType().equals(AdminConstants.DATA_PERMISSION_SELECTOR)) {
SelectorQuery selectorQuery = (SelectorQuery) args[0];
selectorQuery.setFilterIds(dataPermissionList);
args[0] = selectorQuery;
} else if (dataPermission.dataType().equals(AdminConstants.DATA_PERMISSION_RULE)) {
RuleQuery ruleQuery = (RuleQuery) args[0];
ruleQuery.setFilterIds(dataPermissionList);
args[0] = ruleQuery;
}
}
}
return args;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.dromara.soul.admin.interceptor.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* data permission annotation type.
*
* @author nuo-promise
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DataPermission {

/**
* record data type.
*
* @return dataType {@link String}
*/
String dataType() default "";

}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public interface AppAuthMapper {
int updateSelective(AppAuthDO appAuthDO);

/**
* delete application authority.
* deleteSelector application authority.
*
* @param id primary key.
* @return rows int
Expand Down
Loading

0 comments on commit 9130d47

Please sign in to comment.