PermissionPersistService add PageHelper.
This commit is contained in:
parent
cb65d53c7e
commit
6067779974
@ -20,9 +20,9 @@ import com.alibaba.nacos.common.utils.StringUtils;
|
||||
import com.alibaba.nacos.persistence.configuration.condition.ConditionOnEmbeddedStorage;
|
||||
import com.alibaba.nacos.persistence.model.Page;
|
||||
import com.alibaba.nacos.persistence.repository.PaginationHelper;
|
||||
import com.alibaba.nacos.persistence.repository.embedded.operate.DatabaseOperate;
|
||||
import com.alibaba.nacos.config.server.service.repository.embedded.EmbeddedStoragePersistServiceImpl;
|
||||
import com.alibaba.nacos.persistence.repository.embedded.EmbeddedPaginationHelperImpl;
|
||||
import com.alibaba.nacos.persistence.repository.embedded.EmbeddedStorageContextHolder;
|
||||
import com.alibaba.nacos.persistence.repository.embedded.operate.DatabaseOperate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.stereotype.Component;
|
||||
@ -45,16 +45,13 @@ public class EmbeddedPermissionPersistServiceImpl implements PermissionPersistSe
|
||||
@Autowired
|
||||
private DatabaseOperate databaseOperate;
|
||||
|
||||
@Autowired
|
||||
private EmbeddedStoragePersistServiceImpl persistService;
|
||||
|
||||
private static final String PATTERN_STR = "*";
|
||||
|
||||
|
||||
private static final String SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE = " ESCAPE '\\' ";
|
||||
|
||||
@Override
|
||||
public Page<PermissionInfo> getPermissions(String role, int pageNo, int pageSize) {
|
||||
PaginationHelper<PermissionInfo> helper = persistService.createPaginationHelper();
|
||||
PaginationHelper<PermissionInfo> helper = createPaginationHelper();
|
||||
|
||||
String sqlCountRows = "SELECT count(*) FROM permissions WHERE ";
|
||||
|
||||
@ -107,15 +104,15 @@ public class EmbeddedPermissionPersistServiceImpl implements PermissionPersistSe
|
||||
EmbeddedStorageContextHolder.addSqlContext(sql, role, resource, action);
|
||||
databaseOperate.blockUpdate();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Page<PermissionInfo> findPermissionsLike4Page(String role, int pageNo, int pageSize) {
|
||||
PaginationHelper<PermissionInfo> helper = persistService.createPaginationHelper();
|
||||
|
||||
PaginationHelper<PermissionInfo> helper = createPaginationHelper();
|
||||
|
||||
String sqlCountRows = "SELECT count(*) FROM permissions ";
|
||||
|
||||
|
||||
String sqlFetchRows = "SELECT role,resource,action FROM permissions ";
|
||||
|
||||
|
||||
StringBuilder where = new StringBuilder(" WHERE 1=1");
|
||||
List<String> params = new ArrayList<>();
|
||||
if (StringUtils.isNotBlank(role)) {
|
||||
@ -123,11 +120,11 @@ public class EmbeddedPermissionPersistServiceImpl implements PermissionPersistSe
|
||||
where.append(SQL_DERBY_ESCAPE_BACK_SLASH_FOR_LIKE);
|
||||
params.add(generateLikeArgument(role));
|
||||
}
|
||||
|
||||
|
||||
Page<PermissionInfo> pageInfo = helper
|
||||
.fetchPage(sqlCountRows + where, sqlFetchRows + where, params.toArray(), pageNo, pageSize,
|
||||
PERMISSION_ROW_MAPPER);
|
||||
|
||||
|
||||
if (pageInfo == null) {
|
||||
pageInfo = new Page<>();
|
||||
pageInfo.setTotalCount(0);
|
||||
@ -135,7 +132,7 @@ public class EmbeddedPermissionPersistServiceImpl implements PermissionPersistSe
|
||||
}
|
||||
return pageInfo;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String generateLikeArgument(String s) {
|
||||
String underscore = "_";
|
||||
@ -150,5 +147,9 @@ public class EmbeddedPermissionPersistServiceImpl implements PermissionPersistSe
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public <E> PaginationHelper<E> createPaginationHelper() {
|
||||
return new EmbeddedPaginationHelperImpl<>(databaseOperate);
|
||||
}
|
||||
}
|
||||
|
@ -17,12 +17,12 @@
|
||||
package com.alibaba.nacos.plugin.auth.impl.persistence;
|
||||
|
||||
import com.alibaba.nacos.common.utils.StringUtils;
|
||||
import com.alibaba.nacos.config.server.utils.LogUtil;
|
||||
import com.alibaba.nacos.persistence.configuration.condition.ConditionOnExternalStorage;
|
||||
import com.alibaba.nacos.persistence.datasource.DynamicDataSource;
|
||||
import com.alibaba.nacos.persistence.model.Page;
|
||||
import com.alibaba.nacos.persistence.repository.PaginationHelper;
|
||||
import com.alibaba.nacos.config.server.service.repository.extrnal.ExternalStoragePersistServiceImpl;
|
||||
import com.alibaba.nacos.config.server.utils.LogUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import com.alibaba.nacos.persistence.repository.extrnal.ExternalStoragePaginationHelperImpl;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.jdbc.CannotGetJdbcConnectionException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
@ -44,21 +44,18 @@ import static com.alibaba.nacos.plugin.auth.impl.persistence.AuthRowMapperManage
|
||||
@Component
|
||||
public class ExternalPermissionPersistServiceImpl implements PermissionPersistService {
|
||||
|
||||
@Autowired
|
||||
private ExternalStoragePersistServiceImpl persistService;
|
||||
|
||||
private JdbcTemplate jt;
|
||||
|
||||
|
||||
private static final String PATTERN_STR = "*";
|
||||
|
||||
@PostConstruct
|
||||
protected void init() {
|
||||
jt = persistService.getJdbcTemplate();
|
||||
jt = DynamicDataSource.getInstance().getDataSource().getJdbcTemplate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<PermissionInfo> getPermissions(String role, int pageNo, int pageSize) {
|
||||
PaginationHelper<PermissionInfo> helper = persistService.createPaginationHelper();
|
||||
PaginationHelper<PermissionInfo> helper = createPaginationHelper();
|
||||
|
||||
String sqlCountRows = "SELECT count(*) FROM permissions WHERE ";
|
||||
String sqlFetchRows = "SELECT role,resource,action FROM permissions WHERE ";
|
||||
@ -128,40 +125,40 @@ public class ExternalPermissionPersistServiceImpl implements PermissionPersistSe
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Page<PermissionInfo> findPermissionsLike4Page(String role, int pageNo, int pageSize) {
|
||||
PaginationHelper<PermissionInfo> helper = persistService.createPaginationHelper();
|
||||
|
||||
PaginationHelper<PermissionInfo> helper = createPaginationHelper();
|
||||
|
||||
String sqlCountRows = "SELECT count(*) FROM permissions ";
|
||||
String sqlFetchRows = "SELECT role,resource,action FROM permissions ";
|
||||
|
||||
|
||||
StringBuilder where = new StringBuilder(" WHERE 1=1");
|
||||
List<String> params = new ArrayList<>();
|
||||
if (StringUtils.isNotBlank(role)) {
|
||||
where.append(" AND role LIKE ?");
|
||||
params.add(generateLikeArgument(role));
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
Page<PermissionInfo> pageInfo = helper
|
||||
.fetchPage(sqlCountRows + where, sqlFetchRows + where, params.toArray(), pageNo, pageSize,
|
||||
PERMISSION_ROW_MAPPER);
|
||||
|
||||
|
||||
if (pageInfo == null) {
|
||||
pageInfo = new Page<>();
|
||||
pageInfo.setTotalCount(0);
|
||||
pageInfo.setPageItems(new ArrayList<>());
|
||||
}
|
||||
|
||||
|
||||
return pageInfo;
|
||||
|
||||
|
||||
} catch (CannotGetJdbcConnectionException e) {
|
||||
LogUtil.FATAL_LOG.error("[db-error] " + e.toString(), e);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String generateLikeArgument(String s) {
|
||||
String underscore = "_";
|
||||
@ -176,5 +173,9 @@ public class ExternalPermissionPersistServiceImpl implements PermissionPersistSe
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public <E> PaginationHelper<E> createPaginationHelper() {
|
||||
return new ExternalStoragePaginationHelperImpl<>(jt);
|
||||
}
|
||||
}
|
||||
|
@ -17,6 +17,7 @@
|
||||
package com.alibaba.nacos.plugin.auth.impl.persistence;
|
||||
|
||||
import com.alibaba.nacos.persistence.model.Page;
|
||||
import com.alibaba.nacos.persistence.repository.PaginationHelper;
|
||||
|
||||
/**
|
||||
* Permission CRUD service.
|
||||
@ -58,4 +59,12 @@ public interface PermissionPersistService {
|
||||
Page<PermissionInfo> findPermissionsLike4Page(String role, int pageNo, int pageSize);
|
||||
|
||||
String generateLikeArgument(String s);
|
||||
|
||||
/**
|
||||
* create Pagination utils.
|
||||
*
|
||||
* @param <E> Generic object
|
||||
* @return {@link PaginationHelper}
|
||||
*/
|
||||
<E> PaginationHelper<E> createPaginationHelper();
|
||||
}
|
||||
|
@ -17,10 +17,8 @@
|
||||
package com.alibaba.nacos.plugin.auth.impl.persistence;
|
||||
|
||||
import com.alibaba.nacos.persistence.model.Page;
|
||||
import com.alibaba.nacos.persistence.repository.PaginationHelper;
|
||||
import com.alibaba.nacos.persistence.repository.embedded.operate.DatabaseOperate;
|
||||
import com.alibaba.nacos.config.server.service.repository.embedded.EmbeddedStoragePersistServiceImpl;
|
||||
import com.alibaba.nacos.persistence.repository.embedded.EmbeddedStorageContextHolder;
|
||||
import com.alibaba.nacos.persistence.repository.embedded.operate.DatabaseOperate;
|
||||
import com.alibaba.nacos.persistence.repository.embedded.sql.ModifyRequest;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
@ -33,32 +31,26 @@ import org.mockito.junit.MockitoJUnitRunner;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class EmbeddedPermissionPersistServiceImplTest {
|
||||
|
||||
@Mock
|
||||
private DatabaseOperate databaseOperate;
|
||||
|
||||
@Mock
|
||||
private PaginationHelper paginationHelper;
|
||||
|
||||
@Mock
|
||||
private EmbeddedStoragePersistServiceImpl embeddedStoragePersistService;
|
||||
|
||||
private EmbeddedPermissionPersistServiceImpl embeddedPermissionPersistService;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
when(databaseOperate.queryOne(any(String.class), any(Object[].class), eq(Integer.class))).thenReturn(0);
|
||||
embeddedPermissionPersistService = new EmbeddedPermissionPersistServiceImpl();
|
||||
Class<EmbeddedPermissionPersistServiceImpl> embeddedPermissionPersistServiceClass = EmbeddedPermissionPersistServiceImpl.class;
|
||||
Field databaseOperateF = embeddedPermissionPersistServiceClass.getDeclaredField("databaseOperate");
|
||||
databaseOperateF.setAccessible(true);
|
||||
databaseOperateF.set(embeddedPermissionPersistService, databaseOperate);
|
||||
|
||||
Field persistService = embeddedPermissionPersistServiceClass.getDeclaredField("persistService");
|
||||
persistService.setAccessible(true);
|
||||
persistService.set(embeddedPermissionPersistService, embeddedStoragePersistService);
|
||||
Mockito.when(embeddedStoragePersistService.createPaginationHelper()).thenReturn(paginationHelper);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -16,9 +16,11 @@
|
||||
|
||||
package com.alibaba.nacos.plugin.auth.impl.persistence;
|
||||
|
||||
import com.alibaba.nacos.persistence.configuration.DatasourceConfiguration;
|
||||
import com.alibaba.nacos.persistence.datasource.DataSourceService;
|
||||
import com.alibaba.nacos.persistence.datasource.DynamicDataSource;
|
||||
import com.alibaba.nacos.persistence.model.Page;
|
||||
import com.alibaba.nacos.persistence.repository.PaginationHelper;
|
||||
import com.alibaba.nacos.config.server.service.repository.extrnal.ExternalStoragePersistServiceImpl;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@ -30,40 +32,50 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ExternalPermissionPersistServiceImplTest {
|
||||
|
||||
@Mock
|
||||
private ExternalStoragePersistServiceImpl externalStoragePersistService;
|
||||
|
||||
@Mock
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
|
||||
@Mock
|
||||
private PaginationHelper paginationHelper;
|
||||
private DataSourceService dataSourceService;
|
||||
|
||||
private boolean embeddedStorageCache;
|
||||
|
||||
private DataSourceService dataSourceServiceCache;
|
||||
|
||||
private ExternalPermissionPersistServiceImpl externalPermissionPersistService;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
externalPermissionPersistService = new ExternalPermissionPersistServiceImpl();
|
||||
|
||||
Class<ExternalPermissionPersistServiceImpl> externalPermissionPersistServiceClass = ExternalPermissionPersistServiceImpl.class;
|
||||
Field persistServiceClassDeclaredField = externalPermissionPersistServiceClass
|
||||
.getDeclaredField("persistService");
|
||||
persistServiceClassDeclaredField.setAccessible(true);
|
||||
persistServiceClassDeclaredField.set(externalPermissionPersistService, externalStoragePersistService);
|
||||
|
||||
Mockito.when(externalStoragePersistService.getJdbcTemplate()).thenReturn(jdbcTemplate);
|
||||
Mockito.when(externalStoragePersistService.createPaginationHelper()).thenReturn(paginationHelper);
|
||||
|
||||
when(jdbcTemplate.queryForObject(any(), any(), eq(Integer.class))).thenReturn(0);
|
||||
when(dataSourceService.getJdbcTemplate()).thenReturn(jdbcTemplate);
|
||||
embeddedStorageCache = DatasourceConfiguration.isEmbeddedStorage();
|
||||
DatasourceConfiguration.setEmbeddedStorage(false);
|
||||
Field datasourceField = DynamicDataSource.class.getDeclaredField("basicDataSourceService");
|
||||
datasourceField.setAccessible(true);
|
||||
dataSourceServiceCache = (DataSourceService) datasourceField.get(DynamicDataSource.getInstance());
|
||||
datasourceField.set(DynamicDataSource.getInstance(), dataSourceService);
|
||||
externalPermissionPersistService.init();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws NoSuchFieldException, IllegalAccessException {
|
||||
DatasourceConfiguration.setEmbeddedStorage(embeddedStorageCache);
|
||||
Field datasourceField = DynamicDataSource.class.getDeclaredField("basicDataSourceService");
|
||||
datasourceField.setAccessible(true);
|
||||
datasourceField.set(DynamicDataSource.getInstance(), dataSourceServiceCache);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetPermissions() {
|
||||
Page<PermissionInfo> role = externalPermissionPersistService.getPermissions("role", 1, 10);
|
||||
|
||||
Assert.assertNotNull(role);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user