diff --git a/backend/app/src/main/resources/migration/3.0.0/ddl/V11__system_ddl.sql b/backend/app/src/main/resources/migration/3.0.0/ddl/V11__system_ddl.sql index 7cca836c94..36880a76a0 100644 --- a/backend/app/src/main/resources/migration/3.0.0/ddl/V11__system_ddl.sql +++ b/backend/app/src/main/resources/migration/3.0.0/ddl/V11__system_ddl.sql @@ -30,10 +30,10 @@ CREATE TABLE IF NOT EXISTS `user` CREATE TABLE IF NOT EXISTS `user_extend` ( - `user_id` VARCHAR(50) NOT NULL COMMENT '用户ID', + `id` VARCHAR(50) NOT NULL COMMENT '用户ID', `platform_info` BLOB COMMENT '其他平台对接信息', `selenium_server` VARCHAR(255) DEFAULT NULL COMMENT 'UI本地调试地址', - PRIMARY KEY (`user_id`) + PRIMARY KEY (`id`) ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT ='用户扩展'; diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/UserDTO.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/UserDTO.java new file mode 100644 index 0000000000..0b1cd93663 --- /dev/null +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/UserDTO.java @@ -0,0 +1,17 @@ +package io.metersphere.sdk.dto; + +import io.metersphere.system.domain.User; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Data +@EqualsAndHashCode(callSuper = false) +public class UserDTO extends User { + + @ApiModelProperty(name = "其他平台对接信息", required = false, allowableValues = "range[1, 2000]") + private byte[] platformInfo; + + @ApiModelProperty(name = "UI本地调试地址", required = false, allowableValues = "range[1, 255]") + private String seleniumServer; +} diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/UserExtendMapper.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/UserExtendMapper.java new file mode 100644 index 0000000000..60380b474f --- /dev/null +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/UserExtendMapper.java @@ -0,0 +1,7 @@ +package io.metersphere.sdk.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import io.metersphere.system.domain.UserExtend; + +public interface UserExtendMapper extends BaseMapper { +} diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/UserMapper.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/UserMapper.java index 14db1b6b85..5f06319f6f 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/UserMapper.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/UserMapper.java @@ -1,7 +1,9 @@ package io.metersphere.sdk.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import io.metersphere.sdk.dto.UserDTO; import io.metersphere.system.domain.User; public interface UserMapper extends BaseMapper { + UserDTO selectById(String id); } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/UserMapper.xml b/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/UserMapper.xml new file mode 100644 index 0000000000..ae17b3c21f --- /dev/null +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/UserMapper.xml @@ -0,0 +1,11 @@ + + + + + + \ No newline at end of file diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/UserService.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/BaseUserService.java similarity index 78% rename from backend/framework/sdk/src/main/java/io/metersphere/sdk/service/UserService.java rename to backend/framework/sdk/src/main/java/io/metersphere/sdk/service/BaseUserService.java index 51edf5fed9..0932b5ab48 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/UserService.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/service/BaseUserService.java @@ -5,6 +5,7 @@ import io.metersphere.system.domain.User; import io.metersphere.sdk.mapper.UserMapper; import org.springframework.stereotype.Service; + @Service -public class UserService extends ServiceImpl { +public class BaseUserService extends ServiceImpl { } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/UserController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/UserController.java index f27b939bb6..a1be6303b7 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/UserController.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/UserController.java @@ -1,8 +1,9 @@ package io.metersphere.system.controller; -import io.metersphere.sdk.service.UserService; import io.metersphere.system.domain.User; +import io.metersphere.sdk.dto.UserDTO; +import io.metersphere.system.service.UserService; import io.metersphere.validation.groups.Created; import jakarta.annotation.Resource; import org.springframework.validation.annotation.Validated; @@ -27,7 +28,7 @@ public class UserController { } @PostMapping("/add") - public boolean addUser(@Validated({Created.class}) @RequestBody User user) { + public boolean addUser(@Validated({Created.class}) @RequestBody UserDTO user) { return userService.save(user); } } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserService.java new file mode 100644 index 0000000000..1e3b4681c1 --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/UserService.java @@ -0,0 +1,41 @@ +package io.metersphere.system.service; + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import io.metersphere.sdk.mapper.UserExtendMapper; +import io.metersphere.sdk.mapper.UserMapper; +import io.metersphere.sdk.util.BeanUtils; +import io.metersphere.system.domain.User; +import io.metersphere.system.domain.UserExtend; +import io.metersphere.sdk.dto.UserDTO; +import jakarta.annotation.Resource; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@Service +@Transactional(rollbackFor = Exception.class) +public class UserService { + @Resource + private UserMapper userMapper; + @Resource + private UserExtendMapper userExtendMapper; + + public boolean save(UserDTO entity) { + userMapper.insert(entity); + + UserExtend userExtend = new UserExtend(); + BeanUtils.copyBean(userExtend, entity); + userExtendMapper.insert(userExtend); + return true; + } + + public UserDTO getById(String id) { + return userMapper.selectById(id); + } + + public List list() { + QueryWrapper userQueryWrapper = new QueryWrapper<>(new User()); + return userMapper.selectList(userQueryWrapper); + } +} diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/UserControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/UserControllerTests.java index 29540fdf9f..7f19a016d4 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/UserControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/UserControllerTests.java @@ -1,7 +1,7 @@ package io.metersphere.system.controller; import io.metersphere.sdk.util.JSON; -import io.metersphere.system.domain.User; +import io.metersphere.sdk.dto.UserDTO; import jakarta.annotation.Resource; import org.junit.jupiter.api.Test; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; @@ -10,8 +10,6 @@ import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; -import java.util.regex.Matcher; - import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @@ -32,26 +30,47 @@ public class UserControllerTests { } @Test - public void testSelectAll2() throws Exception { + public void testGetUser() throws Exception { mockMvc.perform(MockMvcRequestBuilders.get("/user/get/admin")) .andExpect(status().isOk()) -// .andExpect(content().contentType(MediaType.APPLICATION_JSON)) -// .andExpect(jsonPath("$.person.name").value("Jason")) - .andDo(print()); + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.data.id").value("admin")); } @Test public void testAddUser() throws Exception { - User user = new User(); + UserDTO user = new UserDTO(); user.setId("admin"); user.setName("admin"); + user.setCreateUser("system"); + user.setSource("LOCAL"); + user.setEmail("bin@fit2cloud.com"); + user.setStatus("enabled"); + user.setCreateTime(System.currentTimeMillis()); + user.setUpdateTime(System.currentTimeMillis()); + + user.setSeleniumServer("http://localhost:4444"); + + mockMvc.perform(MockMvcRequestBuilders.post("/user/add") + .content(JSON.toJSONString(user)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)); + } + + @Test + public void testAddUserFailed() throws Exception { + UserDTO user = new UserDTO(); + user.setId("admin2"); + user.setCreateTime(System.currentTimeMillis()); + user.setUpdateTime(System.currentTimeMillis()); + + user.setSeleniumServer("http://localhost:4444"); + mockMvc.perform(MockMvcRequestBuilders.post("/user/add") .content(JSON.toJSONString(user)) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isBadRequest()) - .andExpect(content().contentType(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$.code").value("100400")) - .andDo(print()); + .andExpect(content().contentType(MediaType.APPLICATION_JSON)); } - }