diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/UiPluginController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/UiPluginController.java index 462db26253..6a678edd00 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/UiPluginController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/UiPluginController.java @@ -17,8 +17,6 @@ package org.apache.dolphinscheduler.api.controller; -import static org.apache.dolphinscheduler.api.enums.Status.QUERY_PLUGINS_ERROR; - import org.apache.dolphinscheduler.api.exceptions.ApiException; import org.apache.dolphinscheduler.api.service.UiPluginService; import org.apache.dolphinscheduler.api.utils.Result; @@ -30,13 +28,7 @@ import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestAttribute; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.ResponseStatus; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; @@ -44,6 +36,8 @@ import io.swagger.v3.oas.annotations.Parameters; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.tags.Tag; +import static org.apache.dolphinscheduler.api.enums.Status.*; + /** * ui plugin controller * Some plugins (such as alert plugin) need to provide UI interfaces to users. @@ -85,4 +79,23 @@ public class UiPluginController extends BaseController { Map result = uiPluginService.queryUiPluginDetailById(pluginId); return returnDataList(result); } + + /** + * obtain project version and address + * +// * @param loginUser login user +// * @param userId token for user + * @return product info + */ + @Operation(summary = "queryProductInfo", description = "QUERY_PRODUCT_INFO") + @PostMapping(value = "/queryProductInfo") + @ResponseStatus(HttpStatus.OK) + @ApiException(VERSION_INFO_STATE_ERROR) + public Result queryProductInfo( + @Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam(value = "userId") int userId) { + loginUser.setId(1); + Map result = uiPluginService.queryProductInfo(loginUser, userId); + return returnDataList(result); + } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/enums/Status.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/enums/Status.java index 9dc12d9ba0..8dd6d78938 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/enums/Status.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/enums/Status.java @@ -298,6 +298,7 @@ public enum Status { QUERY_PROJECT_PREFERENCE_ERROR(10302, "query project preference error", "查询项目偏好设置错误"), UPDATE_PROJECT_PREFERENCE_STATE_ERROR(10303, "Failed to update the state of the project preference", "更新项目偏好设置错误"), + VERSION_INFO_STATE_ERROR(10304, "Failed to obtain project version and address", "获取版本信息错误"), UDF_FUNCTION_NOT_EXIST(20001, "UDF function not found", "UDF函数不存在"), UDF_FUNCTION_EXISTS(20002, "UDF function already exists", "UDF函数已存在"), RESOURCE_NOT_EXIST(20004, "resource not exist", "资源不存在"), diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/UiPluginService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/UiPluginService.java index 325f1672c4..69b6bd0475 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/UiPluginService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/UiPluginService.java @@ -17,7 +17,9 @@ package org.apache.dolphinscheduler.api.service; +import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.enums.PluginType; +import org.apache.dolphinscheduler.dao.entity.User; import java.util.Map; @@ -30,4 +32,6 @@ public interface UiPluginService { Map queryUiPluginDetailById(int id); + Map queryProductInfo(User loginUser, int userId); + } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UiPluginServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UiPluginServiceImpl.java index a444d11c99..6387b9cbc7 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UiPluginServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UiPluginServiceImpl.java @@ -17,11 +17,17 @@ package org.apache.dolphinscheduler.api.service.impl; +import org.apache.commons.lang3.StringUtils; import org.apache.dolphinscheduler.api.enums.Status; +import org.apache.dolphinscheduler.api.exceptions.ServiceException; import org.apache.dolphinscheduler.api.service.UiPluginService; import org.apache.dolphinscheduler.common.constants.Constants; +import org.apache.dolphinscheduler.common.enums.AuthorizationType; import org.apache.dolphinscheduler.common.enums.PluginType; +import org.apache.dolphinscheduler.common.utils.EncryptionUtils; import org.apache.dolphinscheduler.dao.entity.PluginDefine; +import org.apache.dolphinscheduler.dao.entity.User; +import org.apache.dolphinscheduler.dao.entity.Version; import org.apache.dolphinscheduler.dao.mapper.PluginDefineMapper; import org.apache.commons.collections4.CollectionUtils; @@ -32,9 +38,13 @@ import java.util.Map; import lombok.extern.slf4j.Slf4j; + +import org.apache.dolphinscheduler.dao.mapper.VersionMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.ACCESS_TOKEN_CREATE; + /** * ui plugin service impl */ @@ -45,6 +55,9 @@ public class UiPluginServiceImpl extends BaseServiceImpl implements UiPluginServ @Autowired PluginDefineMapper pluginDefineMapper; + @Autowired + VersionMapper versionMapper; + @Override public Map queryUiPluginsByType(PluginType pluginType) { Map result = new HashMap<>(); @@ -82,4 +95,24 @@ public class UiPluginServiceImpl extends BaseServiceImpl implements UiPluginServ return result; } + @Override + public Map queryProductInfo(User loginUser, int userId) { + + Map result = new HashMap<>(); + // check if user is existed + if (userId <= 0 || !(loginUser.getId() == userId)) { + throw new ServiceException(Status.REQUEST_PARAMS_NOT_VALID_ERROR, + "User id: " + userId + " should not less than or equals to 0."); + } + // persist to the database + Version versionInfo = versionMapper.selectById(1); +// if(StringUtils.isBlank(versionInfo.getVersion())){ +// throw new ServiceException(Status.VERSION_INFO_STATE_ERROR); +// } + putMsg(result, Status.SUCCESS); + result.put(Constants.DATA_LIST, versionInfo.getVersion()); + + return result; + } + } diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/UiPluginControllerTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/UiPluginControllerTest.java index 2e3c36f5cd..9ba9136dc8 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/UiPluginControllerTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/UiPluginControllerTest.java @@ -22,6 +22,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -31,8 +32,13 @@ import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.constants.Constants; import org.apache.dolphinscheduler.common.enums.PluginType; import org.apache.dolphinscheduler.common.utils.JSONUtils; +import org.apache.dolphinscheduler.dao.entity.User; +import org.mockito.Mockito; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MvcResult; @@ -41,6 +47,9 @@ import org.springframework.util.MultiValueMap; import com.google.common.collect.ImmutableMap; +import java.util.HashMap; +import java.util.Map; + /** * ui plugin controller test */ @@ -53,6 +62,8 @@ public class UiPluginControllerTest extends AbstractControllerTest { private static final ImmutableMap uiPluginServiceResult = ImmutableMap.of(Constants.STATUS, Status.SUCCESS, Constants.DATA_LIST, "Test Data"); + private static final Logger logger = LoggerFactory.getLogger(TenantControllerTest.class); + @MockBean(name = "uiPluginService") private UiPluginService uiPluginService; @@ -91,4 +102,32 @@ public class UiPluginControllerTest extends AbstractControllerTest { JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class); assertThat(actualResponseContent.toString()).isEqualTo(expectResponseContent.toString()); } + + @Test + public void testQueryProductInfo() throws Exception { + Map mockResult = new HashMap<>(); + mockResult.put(Constants.STATUS, Status.SUCCESS); + Mockito.when(uiPluginService.queryProductInfo(Mockito.any(), Mockito.anyInt())).thenReturn(mockResult); + + MultiValueMap paramsMap = new LinkedMultiValueMap<>(); + paramsMap.add("userId", "1"); + + MvcResult mvcResult = mockMvc.perform(post("/ui-plugins/queryProductInfo") + .header(SESSION_ID, sessionId) + .params(paramsMap)) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andReturn(); + + Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class); + Assertions.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue()); + logger.info(mvcResult.getResponse().getContentAsString()); + } + + private User getLoginUser() { + User user = new User(); + user.setId(1); + user.setUserName("admin"); + return user; + } } diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/Version.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/Version.java new file mode 100644 index 0000000000..4b68158afe --- /dev/null +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/Version.java @@ -0,0 +1,38 @@ +/* + * 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.apache.dolphinscheduler.dao.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; +import org.apache.dolphinscheduler.common.enums.UserType; + +import java.util.Date; + +@Data +@TableName("t_ds_version") +public class Version { + + @TableId(value = "id", type = IdType.AUTO) + private Integer id; + + private String version; + +} diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/VersionMapper.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/VersionMapper.java new file mode 100644 index 0000000000..9c8a9ae7c2 --- /dev/null +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/VersionMapper.java @@ -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.apache.dolphinscheduler.dao.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import org.apache.dolphinscheduler.dao.entity.User; +import org.apache.dolphinscheduler.dao.entity.UserWithProcessDefinitionCode; +import org.apache.dolphinscheduler.dao.entity.Version; +import org.apache.ibatis.annotations.Param; + +import java.util.Date; +import java.util.List; + +/** + * user mapper interface + */ +public interface VersionMapper extends BaseMapper { + + /** + * select by user id + */ + Version selectById(int id); + +}