Browse Source

[Feature] Add CURD to the project/tenant/user section of the API and add its tests. (#10913)

3.1.0-release
Lyle Shaw 2 years ago committed by GitHub
parent
commit
181cd2ff79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 51
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/python/PythonGateway.java
  2. 18
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/UsersService.java
  3. 32
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java
  4. 80
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/UsersServiceTest.java

51
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/python/PythonGateway.java

@ -17,6 +17,7 @@
package org.apache.dolphinscheduler.api.python; package org.apache.dolphinscheduler.api.python;
import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.Date; import java.util.Date;
@ -391,23 +392,61 @@ public class PythonGateway {
} }
} }
public Project queryProjectByName(String userName, String projectName) {
User user = usersService.queryUser(userName);
return (Project) projectService.queryByName(user, projectName);
}
public void updateProject(String userName, Long projectCode, String projectName, String desc) {
User user = usersService.queryUser(userName);
projectService.update(user, projectCode, projectName, desc, userName);
}
public void deleteProject(String userName, Long projectCode) {
User user = usersService.queryUser(userName);
projectService.deleteProject(user, projectCode);
}
public Tenant createTenant(String tenantCode, String desc, String queueName) { public Tenant createTenant(String tenantCode, String desc, String queueName) {
return tenantService.createTenantIfNotExists(tenantCode, desc, queueName, queueName); return tenantService.createTenantIfNotExists(tenantCode, desc, queueName, queueName);
} }
public Result queryTenantList(String userName, String searchVal, Integer pageNo, Integer pageSize) {
User user = usersService.queryUser(userName);
return tenantService.queryTenantList(user, searchVal, pageNo, pageSize);
}
public void updateTenant(String userName, int id, String tenantCode, int queueId, String desc) throws Exception {
User user = usersService.queryUser(userName);
tenantService.updateTenant(user, id, tenantCode, queueId, desc);
}
public void deleteTenantById(String userName, Integer tenantId) throws Exception {
User user = usersService.queryUser(userName);
tenantService.deleteTenantById(user, tenantId);
}
public void createUser(String userName, public void createUser(String userName,
String userPassword, String userPassword,
String email, String email,
String phone, String phone,
String tenantCode, String tenantCode,
String queue, String queue,
int state) { int state) throws IOException {
User user = usersService.queryUser(userName); usersService.createUserIfNotExists(userName, userPassword, email, phone, tenantCode, queue, state);
if (Objects.isNull(user)) {
Map<String, Object> tenantResult = tenantService.queryByTenantCode(tenantCode);
Tenant tenant = (Tenant) tenantResult.get(Constants.DATA_LIST);
usersService.createUser(userName, userPassword, email, tenant.getId(), phone, queue, state);
} }
public User queryUser(int id) {
return usersService.queryUser(id);
}
public void updateUser(String userName, String userPassword, String email, String phone, String tenantCode, String queue, int state) throws Exception {
usersService.createUserIfNotExists(userName, userPassword, email, phone, tenantCode, queue, state);
}
public void deleteUser(String userName, int id) throws Exception {
User user = usersService.queryUser(userName);
usersService.deleteUserById(user, id);
} }
/** /**

18
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/UsersService.java

@ -297,4 +297,22 @@ public interface UsersService {
* @return create result code * @return create result code
*/ */
Map<String, Object> batchActivateUser(User loginUser, List<String> userNames); Map<String, Object> batchActivateUser(User loginUser, List<String> userNames);
/**
* Make sure user with given name exists, and create the user if not exists
* <p>
* ONLY for python gateway server, and should not use this in web ui function
*
* @param userName user name
* @param userPassword user password
* @param email user email
* @param phone user phone
* @param tenantCode tenant code
* @param queue queue
* @param state state
* @return create result code
*/
User createUserIfNotExists(String userName, String userPassword, String email, String phone, String tenantCode,
String queue,
int state) throws IOException;
} }

32
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java

@ -77,10 +77,10 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TimeZone; import java.util.TimeZone;
import java.util.Arrays; import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.USER_MANAGER; import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.USER_MANAGER;
import static org.apache.dolphinscheduler.common.Constants.USER_PASSWORD_MAX_LENGTH;
/** /**
* users service impl * users service impl
@ -1344,4 +1344,34 @@ public class UsersServiceImpl extends BaseServiceImpl implements UsersService {
result.put(Constants.DATA_LIST, res); result.put(Constants.DATA_LIST, res);
return result; return result;
} }
/**
* Make sure user with given name exists, and create the user if not exists
* <p>
* ONLY for python gateway server, and should not use this in web ui function
*
* @param userName user name
* @param userPassword user password
* @param email user email
* @param phone user phone
* @param tenantCode tenant code
* @param queue queue
* @param state state
* @return create result code
*/
@Override
@Transactional
public User createUserIfNotExists(String userName, String userPassword, String email, String phone, String tenantCode,
String queue,
int state) throws IOException {
User user = userMapper.queryByUserNameAccurately(userName);
if (Objects.isNull(user)) {
Tenant tenant = tenantMapper.queryByTenantCode(tenantCode);
user = createUser(userName, userPassword, email, tenant.getId(), phone, queue, state);
return user;
}
updateUser(user, user.getId(), userName, userPassword, email, user.getTenantId(), phone, queue, state, null);
return user;
}
} }

80
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/UsersServiceTest.java

@ -17,11 +17,14 @@
package org.apache.dolphinscheduler.api.service; package org.apache.dolphinscheduler.api.service;
import com.baomidou.mybatisplus.core.metadata.IPage; import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.USER_MANAGER;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists; import static org.mockito.ArgumentMatchers.any;
import org.apache.commons.collections.CollectionUtils; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.permission.ResourcePermissionCheckService;
import org.apache.dolphinscheduler.api.service.impl.BaseServiceImpl; import org.apache.dolphinscheduler.api.service.impl.BaseServiceImpl;
import org.apache.dolphinscheduler.api.service.impl.UsersServiceImpl; import org.apache.dolphinscheduler.api.service.impl.UsersServiceImpl;
import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.PageInfo;
@ -31,10 +34,31 @@ import org.apache.dolphinscheduler.common.enums.AuthorizationType;
import org.apache.dolphinscheduler.common.enums.UserType; import org.apache.dolphinscheduler.common.enums.UserType;
import org.apache.dolphinscheduler.common.storage.StorageOperate; import org.apache.dolphinscheduler.common.storage.StorageOperate;
import org.apache.dolphinscheduler.common.utils.EncryptionUtils; import org.apache.dolphinscheduler.common.utils.EncryptionUtils;
import org.apache.dolphinscheduler.dao.entity.*; import org.apache.dolphinscheduler.dao.entity.AlertGroup;
import org.apache.dolphinscheduler.dao.mapper.*; import org.apache.dolphinscheduler.dao.entity.Project;
import org.apache.dolphinscheduler.api.permission.ResourcePermissionCheckService; import org.apache.dolphinscheduler.dao.entity.Resource;
import org.apache.dolphinscheduler.dao.entity.Tenant;
import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.dao.mapper.AccessTokenMapper;
import org.apache.dolphinscheduler.dao.mapper.AlertGroupMapper;
import org.apache.dolphinscheduler.dao.mapper.DataSourceUserMapper;
import org.apache.dolphinscheduler.dao.mapper.K8sNamespaceUserMapper;
import org.apache.dolphinscheduler.dao.mapper.ProjectMapper;
import org.apache.dolphinscheduler.dao.mapper.ProjectUserMapper;
import org.apache.dolphinscheduler.dao.mapper.ResourceMapper;
import org.apache.dolphinscheduler.dao.mapper.ResourceUserMapper;
import org.apache.dolphinscheduler.dao.mapper.TenantMapper;
import org.apache.dolphinscheduler.dao.mapper.UDFUserMapper;
import org.apache.dolphinscheduler.dao.mapper.UserMapper;
import org.apache.dolphinscheduler.spi.enums.ResourceType; import org.apache.dolphinscheduler.spi.enums.ResourceType;
import org.apache.commons.collections.CollectionUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.junit.After; import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
@ -47,14 +71,9 @@ import org.mockito.junit.MockitoJUnitRunner;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.ArrayList; import com.baomidou.mybatisplus.core.metadata.IPage;
import java.util.List; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Map; import com.google.common.collect.Lists;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.USER_MANAGER;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
/** /**
* users service test * users service test
@ -234,14 +253,14 @@ public class UsersServiceTest {
user.setId(1); user.setId(1);
//no operate //no operate
Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ACCESS_TOKEN,1, USER_MANAGER, serviceLogger)).thenReturn(true); Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ACCESS_TOKEN, 1, USER_MANAGER, serviceLogger)).thenReturn(true);
Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ACCESS_TOKEN, null, 0, serviceLogger)).thenReturn(false); Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ACCESS_TOKEN, null, 0, serviceLogger)).thenReturn(false);
Map<String, Object> result = usersService.queryUserList(user); Map<String, Object> result = usersService.queryUserList(user);
logger.info(result.toString()); logger.info(result.toString());
Assert.assertEquals(Status.USER_NO_OPERATION_PERM, result.get(Constants.STATUS)); Assert.assertEquals(Status.USER_NO_OPERATION_PERM, result.get(Constants.STATUS));
//success //success
Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ACCESS_TOKEN,1, USER_MANAGER, serviceLogger)).thenReturn(true); Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.ACCESS_TOKEN, 1, USER_MANAGER, serviceLogger)).thenReturn(true);
Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ACCESS_TOKEN, null, 0, serviceLogger)).thenReturn(true); Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.ACCESS_TOKEN, null, 0, serviceLogger)).thenReturn(true);
user.setUserType(UserType.ADMIN_USER); user.setUserType(UserType.ADMIN_USER);
when(userMapper.selectList(null)).thenReturn(getUserList()); when(userMapper.selectList(null)).thenReturn(getUserList());
@ -465,7 +484,7 @@ public class UsersServiceTest {
logger.info(result.toString()); logger.info(result.toString());
Assert.assertEquals(Status.USER_NOT_EXIST, result.get(Constants.STATUS)); Assert.assertEquals(Status.USER_NOT_EXIST, result.get(Constants.STATUS));
//success //success
when(k8sNamespaceUserMapper.deleteNamespaceRelation(0,1)).thenReturn(1); when(k8sNamespaceUserMapper.deleteNamespaceRelation(0, 1)).thenReturn(1);
result = usersService.grantNamespaces(loginUser, 1, namespaceIds); result = usersService.grantNamespaces(loginUser, 1, namespaceIds);
logger.info(result.toString()); logger.info(result.toString());
Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));
@ -706,6 +725,30 @@ public class UsersServiceTest {
} }
} }
@Test
public void testCreateUserIfNotExists() throws IOException {
User user;
String userName = "userTest0001";
String userPassword = "userTest";
String email = "abc@x.com";
String phone = "123456789";
String tenantCode = "tenantCode";
int stat = 1;
// User exists
Mockito.when(userMapper.existUser(userName)).thenReturn(true);
Mockito.when(userMapper.queryByUserNameAccurately(userName)).thenReturn(getUser());
Mockito.when(tenantMapper.queryByTenantCode(tenantCode)).thenReturn(getTenant());
user = usersService.createUserIfNotExists(userName, userPassword, email, phone, tenantCode, queueName, stat);
Assert.assertEquals(getUser(), user);
// User not exists
Mockito.when(userMapper.existUser(userName)).thenReturn(false);
Mockito.when(tenantMapper.queryByTenantCode(tenantCode)).thenReturn(getTenant());
user = usersService.createUserIfNotExists(userName, userPassword, email, phone, tenantCode, queueName, stat);
Assert.assertNotNull(user);
}
/** /**
* get disabled user * get disabled user
*/ */
@ -720,6 +763,7 @@ public class UsersServiceTest {
/** /**
* Get project * Get project
*
* @return * @return
*/ */
private Project getProject() { private Project getProject() {

Loading…
Cancel
Save