Skip to content

Commit

Permalink
[type:feat]Data-permission adapts to namespace transformation (#5650)
Browse files Browse the repository at this point in the history
* commit

* commit

* first draft,not verified

* simplify verified

* simplify verified

* commit

* commit

* fix

* fix e2e checkstyle

* fix e2e

* fix test

* fix

* fix

* fix test

* fix

* fix

* trigger ci

* trigger ci

* fix sql

* fix sql

* fix sql

* fix

* trigger ci

* trigger ci

* fix

* trigger ci

* fix

* fix

* fix

* fix

* trigger ci

* trigger ci

* fix

* trigger ci

* trigger ci

* trigger ci

* fix cr

* fix cr

* commit

* commit

* support e2e

* commit meta_data

* commit

* commit

* commit

* commit

* commit

* commit

* fix

* fix

* fix unit test

* fix unit test

* fix unit test

* fix unit test

* fix unit test

* fix unit test

* fix unit test

* trigger ci

* trigger ci

* merge fix

* data-permission

* fix tips

* add length valid

* fix e2e

* trigger ci

* trigger ci

* fix

* fix

---------

Co-authored-by: ‘xcsnx’ <‘1192709687@qq.com’>
Co-authored-by: aias00 <rokkki@163.com>
  • Loading branch information
3 people authored Sep 7, 2024
1 parent ade3ac3 commit d6d0223
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.shenyu.admin.controller;

import org.apache.shenyu.admin.aspect.annotation.RestApi;
import org.apache.shenyu.admin.mapper.NamespaceMapper;
import org.apache.shenyu.admin.model.dto.DataPermissionDTO;
import org.apache.shenyu.admin.model.page.CommonPager;
import org.apache.shenyu.admin.model.page.PageParameter;
Expand All @@ -27,6 +28,7 @@
import org.apache.shenyu.admin.model.vo.DataPermissionPageVO;
import org.apache.shenyu.admin.service.DataPermissionService;
import org.apache.shenyu.admin.utils.ShenyuResultMessage;
import org.apache.shenyu.admin.validation.annotation.Existed;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
Expand Down Expand Up @@ -56,16 +58,19 @@ public DataPermissionController(final DataPermissionService dataPermissionServic
* @param userId user id
* @param pluginId plugin id
* @param name selector name
* @param namespaceId namespaceId
* @return {@linkplain ShenyuAdminResult}
*/
@GetMapping("/selector")
public ShenyuAdminResult listPageSelectorDataPermissions(@RequestParam("currentPage") final Integer currentPage,
@RequestParam("pageSize") final Integer pageSize,
@RequestParam("userId") final String userId,
@RequestParam("pluginId") final String pluginId,
@RequestParam(value = "name", required = false) final String name) {
@RequestParam(value = "name", required = false) final String name,
@Valid @Existed(message = "namespaceId is not existed",
provider = NamespaceMapper.class) final String namespaceId) {
CommonPager<DataPermissionPageVO> selectorList = dataPermissionService.listSelectorsByPage(
new SelectorQuery(pluginId, name, new PageParameter(currentPage, pageSize)), userId);
new SelectorQuery(pluginId, name, new PageParameter(currentPage, pageSize), namespaceId), userId);
return ShenyuAdminResult.success(ShenyuResultMessage.QUERY_SUCCESS, selectorList);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shenyu.admin.model.dto;

import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.NotNull;
import java.io.Serializable;

Expand All @@ -35,12 +36,14 @@ public class NamespaceDTO implements Serializable {
/**
* namespace name.
*/
@Max(value = 255, message = "The maximum length is 255")
@NotNull(message = "namespace name not null")
private String name;

/**
* namespace description.
*/
@Max(value = 255, message = "The maximum length is 255")
@NotNull(message = "namespace description not null")
private String description;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,18 @@ public class SelectorQuery extends FilterQuery {
public SelectorQuery() {
}

public SelectorQuery(final String pluginId, final String name, final PageParameter pageParameter) {
public SelectorQuery(final String pluginId, final String name, final PageParameter pageParameter, final String namespaceId) {
this.pluginId = pluginId;
this.name = name;
this.pageParameter = pageParameter;
this.namespaceId = namespaceId;
}

public SelectorQuery(final List<String> pluginIds, final String name, final PageParameter pageParameter) {
public SelectorQuery(final List<String> pluginIds, final String name, final PageParameter pageParameter, final String namespaceId) {
this.pluginIds = pluginIds;
this.name = name;
this.pageParameter = pageParameter;
this.namespaceId = namespaceId;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,10 @@ default int createOrUpdate(SelectorDTO selectorDTO) {
*
* @param name name
* @param pluginNames pluginNames
* @param namespaceId namespaceId
* @return selectorDO list
*/
List<SelectorDO> findByNameAndPluginNames(String name, List<String> pluginNames);
List<SelectorDO> findByNameAndPluginNamesAndNamespaceId(String name, List<String> pluginNames, String namespaceId);

/**
* Build selector data by name and namespaceId.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private void removeRegister(final ApiDO apiDO) {
.collect(Collectors.toList()), SYS_DEFAULT_NAMESPACE_ID);
}
//clean selector
List<SelectorDO> selectorDOList = selectorService.findByNameAndPluginNames(apiDO.getContextPath(), PluginEnum.getUpstreamNames());
List<SelectorDO> selectorDOList = selectorService.findByNameAndPluginNamesAndNamespaceId(apiDO.getContextPath(), PluginEnum.getUpstreamNames(), SYS_DEFAULT_NAMESPACE_ID);
ArrayList<String> selectorIds = Lists.newArrayList();
Optional.ofNullable(selectorDOList).orElseGet(ArrayList::new).stream().forEach(selectorDO -> {
final String selectorId = selectorDO.getId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ public SelectorDO findByNameAndPluginNameAndNamespaceIdForUpdate(final String na
}

@Override
public List<SelectorDO> findByNameAndPluginNames(final String name, final List<String> pluginNames) {
public List<SelectorDO> findByNameAndPluginNamesAndNamespaceId(final String name, final List<String> pluginNames, final String namespaceID) {
final List<PluginDO> pluginDOList = pluginMapper.selectByNames(pluginNames);
if (CollectionUtils.isEmpty(pluginDOList)) {
return Lists.newArrayList();
Expand All @@ -377,6 +377,7 @@ public List<SelectorDO> findByNameAndPluginNames(final String name, final List<S
SelectorQuery selectorQuery = new SelectorQuery();
selectorQuery.setName(name);
selectorQuery.setPluginIds(pluginIds);
selectorQuery.setNamespaceId(namespaceID);
return selectorMapper.selectByQuery(selectorQuery);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import static org.apache.shenyu.common.constant.Constants.SYS_DEFAULT_NAMESPACE_ID;

/**
* Load Service Doc Entry.
*/
Expand Down Expand Up @@ -178,7 +180,7 @@ private List<UpstreamInstance> getAllClusterLastUpdateInstanceList() {
}
supportSwaggerPluginSet = new HashSet<>(pluginNames);
List<String> pluginIds = pluginDOList.stream().map(PluginDO::getId).collect(Collectors.toList());
CommonPager<SelectorVO> commonPager = selectorService.listByPage(new SelectorQuery(pluginIds, null, new PageParameter(1, Integer.MAX_VALUE)));
CommonPager<SelectorVO> commonPager = selectorService.listByPage(new SelectorQuery(pluginIds, null, new PageParameter(1, Integer.MAX_VALUE), SYS_DEFAULT_NAMESPACE_ID));
List<SelectorVO> clusterList = commonPager.getDataList();
if (CollectionUtils.isEmpty(clusterList)) {
LOG.info("getAllClusterLastUpdateInstanceList. Not loaded into available backend services.");
Expand Down
3 changes: 2 additions & 1 deletion shenyu-admin/src/main/resources/mappers/selector-sqlmap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@
<include refid="Base_Column_List"/>
FROM selector
<where>
namespace_id = #{namespaceId, jdbcType=VARCHAR}
<if test="pluginId != null">
plugin_id = #{pluginId, jdbcType=VARCHAR}
AND plugin_id = #{pluginId, jdbcType=VARCHAR}
</if>
<if test="pluginIds != null and pluginIds.size > 0">
AND plugin_id IN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import java.util.Collections;

import static org.apache.shenyu.common.constant.Constants.SYS_DEFAULT_NAMESPACE_ID;
import static org.hamcrest.core.Is.is;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
Expand Down Expand Up @@ -74,14 +76,15 @@ public void listPageSelectorDataPermissions() throws Exception {
final PageParameter pageParameter = new PageParameter(currentPage, pageSize);
final CommonPager<DataPermissionPageVO> commonPager = new CommonPager<>(pageParameter, Collections.singletonList(dataPermissionPageVO));
given(this.dataPermissionService.listSelectorsByPage(
new SelectorQuery(pluginId, name, pageParameter), userId)).willReturn(commonPager);
new SelectorQuery(pluginId, name, pageParameter, SYS_DEFAULT_NAMESPACE_ID), userId)).willReturn(commonPager);

this.mockMvc.perform(MockMvcRequestBuilders.get("/data-permission/selector")
.param("currentPage", String.valueOf(currentPage))
.param("pageSize", String.valueOf(pageSize))
.param("userId", userId)
.param("pluginId", pluginId)
.param("name", name))
.param("name", name)
.param("namespaceId", SYS_DEFAULT_NAMESPACE_ID))
.andExpect(status().isOk())
.andExpect(jsonPath("$.message", is(ShenyuResultMessage.QUERY_SUCCESS)))
.andExpect(jsonPath("$.data.dataList[0].dataId", is(dataPermissionPageVO.getDataId())))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void testSelectByQuery() {
int insert = selectorMapper.insert(selectorDO);
assertEquals(1, insert);

SelectorQuery query = new SelectorQuery(selectorDO.getPluginId(), selectorDO.getName(), new PageParameter());
SelectorQuery query = new SelectorQuery(selectorDO.getPluginId(), selectorDO.getName(), new PageParameter(), SYS_DEFAULT_NAMESPACE_ID);
List<SelectorDO> list = selectorMapper.selectByQuery(query);
assertNotNull(list);
assertEquals(list.size(), 1);
Expand Down Expand Up @@ -129,7 +129,7 @@ public void testCountByQuery() {
int insert = selectorMapper.insert(selectorDO);
assertEquals(1, insert);

SelectorQuery query = new SelectorQuery(selectorDO.getPluginId(), selectorDO.getName(), new PageParameter());
SelectorQuery query = new SelectorQuery(selectorDO.getPluginId(), selectorDO.getName(), new PageParameter(), SYS_DEFAULT_NAMESPACE_ID);
Integer count = selectorMapper.countByQuery(query);
assertNotNull(count);
assertEquals(Integer.valueOf(1), count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void assertDeleteSelector() {

@Test
public void assertListSelectorsByPage() {
SelectorQuery selectorQuery = new SelectorQuery("1", null, new PageParameter(1, 10));
SelectorQuery selectorQuery = new SelectorQuery("1", null, new PageParameter(1, 10), SYS_DEFAULT_NAMESPACE_ID);
given(selectorMapper.countByQuery(selectorQuery)).willReturn(100);
given(selectorMapper.selectByQuery(selectorQuery)).willReturn(Collections.singletonList(SelectorDO.buildSelectorDO(selectorDTO)));
given(dataPermissionMapper.selectDataIds(Collections.singletonList("1"), "1001", 0)).willReturn(Collections.singletonList("1"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public final class AdminConstants {
/**
* The constant SYS_DEFAULT_NAMESPACE_ID_DELETE.
*/
public static final String SYS_DEFAULT_NAMESPACE_ID_DELETE = "The default namespace can't deleted!";
public static final String SYS_DEFAULT_NAMESPACE_ID_DELETE = "The default namespace cannot delete!";

/**
* The constant SYS_API_ID_NOT_EXIST.
Expand Down

0 comments on commit d6d0223

Please sign in to comment.