This is an automated email from the ASF dual-hosted git repository.

nicholasjiang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/paimon-webui.git


The following commit(s) were added to refs/heads/main by this push:
     new 2dc7a3e  [Improvement] Support changing status and assigning role of 
user (#224)
2dc7a3e is described below

commit 2dc7a3ecfa45653b288facd9a9ea4644be2181e1
Author: s7monk <[email protected]>
AuthorDate: Mon May 20 14:16:25 2024 +0800

    [Improvement] Support changing status and assigning role of user (#224)
---
 .../web/server/controller/UserController.java      | 24 ++++++
 .../paimon/web/server/service/UserService.java     | 16 ++++
 .../web/server/service/impl/UserServiceImpl.java   | 15 ++++
 .../web/server/controller/UserControllerTest.java  | 92 +++++++++++++++++++---
 4 files changed, 137 insertions(+), 10 deletions(-)

diff --git 
a/paimon-web-server/src/main/java/org/apache/paimon/web/server/controller/UserController.java
 
b/paimon-web-server/src/main/java/org/apache/paimon/web/server/controller/UserController.java
index 25de1f5..c1c9c07 100644
--- 
a/paimon-web-server/src/main/java/org/apache/paimon/web/server/controller/UserController.java
+++ 
b/paimon-web-server/src/main/java/org/apache/paimon/web/server/controller/UserController.java
@@ -140,4 +140,28 @@ public class UserController {
         }
         return userService.changePassword(user) ? R.succeed() : R.failed();
     }
+
+    /**
+     * Changes the status of a user via a PUT request.
+     *
+     * @param user the user object containing the new status information
+     * @return a response object indicating success or failure
+     */
+    @SaCheckPermission("system:user:update")
+    @PutMapping("/changeStatus")
+    public R<Void> changeStatus(@RequestBody User user) {
+        return userService.updateUserStatus(user) ? R.succeed() : R.failed();
+    }
+
+    /**
+     * Allocates a role to a user.
+     *
+     * @param user the user to whom the role is to be allocated
+     * @return a response object indicating success or failure
+     */
+    @SaCheckPermission("system:user:update")
+    @PostMapping("/allocate")
+    public R<Void> allocateRole(@RequestBody User user) {
+        return userService.allocateRole(user) > 0 ? R.succeed() : R.failed();
+    }
 }
diff --git 
a/paimon-web-server/src/main/java/org/apache/paimon/web/server/service/UserService.java
 
b/paimon-web-server/src/main/java/org/apache/paimon/web/server/service/UserService.java
index 3bcc4b8..2c972d5 100644
--- 
a/paimon-web-server/src/main/java/org/apache/paimon/web/server/service/UserService.java
+++ 
b/paimon-web-server/src/main/java/org/apache/paimon/web/server/service/UserService.java
@@ -115,4 +115,20 @@ public interface UserService extends IService<User> {
      * @return true if the password was successfully changed, false otherwise.
      */
     boolean changePassword(User user);
+
+    /**
+     * Updates the status of the specified user.
+     *
+     * @param user the user whose status needs to be updated
+     * @return true if the update is successful, false otherwise
+     */
+    boolean updateUserStatus(User user);
+
+    /**
+     * Allocates roles to a user.
+     *
+     * @param user the user to whom the role should be allocated
+     * @return the number of rows affected
+     */
+    int allocateRole(User user);
 }
diff --git 
a/paimon-web-server/src/main/java/org/apache/paimon/web/server/service/impl/UserServiceImpl.java
 
b/paimon-web-server/src/main/java/org/apache/paimon/web/server/service/impl/UserServiceImpl.java
index 6a1e711..e505f7f 100644
--- 
a/paimon-web-server/src/main/java/org/apache/paimon/web/server/service/impl/UserServiceImpl.java
+++ 
b/paimon-web-server/src/main/java/org/apache/paimon/web/server/service/impl/UserServiceImpl.java
@@ -49,6 +49,7 @@ import cn.dev33.satoken.stp.StpUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.google.common.base.Preconditions;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -259,6 +260,20 @@ public class UserServiceImpl extends 
ServiceImpl<UserMapper, User> implements Us
         return this.updateById(user);
     }
 
+    @Override
+    public boolean updateUserStatus(User user) {
+        Preconditions.checkArgument(user != null && user.getId() != null);
+        return this.lambdaUpdate()
+                .set(User::getEnabled, user.getEnabled())
+                .eq(User::getId, user.getId())
+                .update();
+    }
+
+    @Override
+    public int allocateRole(User user) {
+        return this.insertUserRole(user);
+    }
+
     private int insertUserRole(User user) {
         int rows = 1;
         if (user.getRoleIds() != null && user.getRoleIds().length > 0) {
diff --git 
a/paimon-web-server/src/test/java/org/apache/paimon/web/server/controller/UserControllerTest.java
 
b/paimon-web-server/src/test/java/org/apache/paimon/web/server/controller/UserControllerTest.java
index 37ca6aa..4c448b4 100644
--- 
a/paimon-web-server/src/test/java/org/apache/paimon/web/server/controller/UserControllerTest.java
+++ 
b/paimon-web-server/src/test/java/org/apache/paimon/web/server/controller/UserControllerTest.java
@@ -62,6 +62,7 @@ public class UserControllerTest extends ControllerTestBase {
         user.setId(userId);
         user.setUsername(username);
         user.setNickname(username);
+        user.setPassword("test");
         user.setUserType(0);
         user.setEnabled(true);
         user.setIsDelete(false);
@@ -79,6 +80,31 @@ public class UserControllerTest extends ControllerTestBase {
     @Test
     @Order(2)
     public void testGetUser() throws Exception {
+        UserVO user = getUser(userId);
+        assertNotNull(user);
+        assertEquals(user.getUsername(), username);
+    }
+
+    @Test
+    @Order(3)
+    public void testUpdateUser() throws Exception {
+        String newUserName = username + "-edit";
+        User user = new User();
+        user.setId(userId);
+        user.setUsername(newUserName);
+        user.setNickname(newUserName);
+        user.setUserType(0);
+        user.setEnabled(true);
+        user.setIsDelete(false);
+
+        mockMvc.perform(
+                        MockMvcRequestBuilders.put(userPath)
+                                .cookie(cookie)
+                                .content(ObjectMapperUtils.toJSON(user))
+                                .contentType(MediaType.APPLICATION_JSON_VALUE)
+                                .accept(MediaType.APPLICATION_JSON_VALUE))
+                .andExpect(MockMvcResultMatchers.status().isOk());
+
         String responseString =
                 mockMvc.perform(
                                 MockMvcRequestBuilders.get(userPath + "/" + 
userId)
@@ -94,28 +120,29 @@ public class UserControllerTest extends ControllerTestBase 
{
         R<UserVO> r = ObjectMapperUtils.fromJSON(responseString, new 
TypeReference<R<UserVO>>() {});
         assertEquals(200, r.getCode());
         assertNotNull(r.getData());
-        assertEquals(r.getData().getUsername(), username);
+        assertEquals(r.getData().getUsername(), newUserName);
     }
 
     @Test
-    @Order(3)
-    public void testUpdateUser() throws Exception {
-        String newUserName = username + "-edit";
+    @Order(4)
+    public void testAllocateRole() throws Exception {
         User user = new User();
         user.setId(userId);
-        user.setUsername(newUserName);
-        user.setNickname(newUserName);
+        user.setUsername(username);
+        user.setNickname(username);
         user.setUserType(0);
         user.setEnabled(true);
         user.setIsDelete(false);
+        user.setRoleIds(new Integer[] {2});
 
         mockMvc.perform(
-                        MockMvcRequestBuilders.put(userPath)
+                        MockMvcRequestBuilders.post(userPath + "/allocate")
                                 .cookie(cookie)
                                 .content(ObjectMapperUtils.toJSON(user))
                                 .contentType(MediaType.APPLICATION_JSON_VALUE)
                                 .accept(MediaType.APPLICATION_JSON_VALUE))
-                .andExpect(MockMvcResultMatchers.status().isOk());
+                .andExpect(MockMvcResultMatchers.status().isOk())
+                .andDo(MockMvcResultHandlers.print());
 
         String responseString =
                 mockMvc.perform(
@@ -132,11 +159,12 @@ public class UserControllerTest extends 
ControllerTestBase {
         R<UserVO> r = ObjectMapperUtils.fromJSON(responseString, new 
TypeReference<R<UserVO>>() {});
         assertEquals(200, r.getCode());
         assertNotNull(r.getData());
-        assertEquals(r.getData().getUsername(), newUserName);
+        assertTrue(r.getData().getRoles().size() > 0);
+        assertEquals("common", r.getData().getRoles().get(0).getRoleName());
     }
 
     @Test
-    @Order(4)
+    @Order(5)
     public void testListUsers() throws Exception {
         String responseString =
                 mockMvc.perform(
@@ -174,6 +202,32 @@ public class UserControllerTest extends ControllerTestBase 
{
 
     @Test
     @Order(5)
+    public void testChangeUserStatus() throws Exception {
+        User user = new User();
+        user.setId(2);
+        user.setEnabled(false);
+
+        String responseString =
+                mockMvc.perform(
+                                MockMvcRequestBuilders.put(userPath + 
"/changeStatus")
+                                        .cookie(cookie)
+                                        
.content(ObjectMapperUtils.toJSON(user))
+                                        
.contentType(MediaType.APPLICATION_JSON_VALUE)
+                                        
.accept(MediaType.APPLICATION_JSON_VALUE))
+                        .andExpect(MockMvcResultMatchers.status().isOk())
+                        .andDo(MockMvcResultHandlers.print())
+                        .andReturn()
+                        .getResponse()
+                        .getContentAsString();
+
+        R<Void> r = ObjectMapperUtils.fromJSON(responseString, new 
TypeReference<R<Void>>() {});
+        assertEquals(200, r.getCode());
+        UserVO changeUser = getUser(2);
+        assertEquals(changeUser.getEnabled(), false);
+    }
+
+    @Test
+    @Order(7)
     public void testDeleteUser() throws Exception {
         String delResponseString =
                 mockMvc.perform(
@@ -209,4 +263,22 @@ public class UserControllerTest extends ControllerTestBase 
{
         User newUser = userMapper.selectById(2);
         assertEquals("9efab2399c7c560b34de477b9aa0a465", 
newUser.getPassword());
     }
+
+    private UserVO getUser(Integer userId) throws Exception {
+        String responseString =
+                mockMvc.perform(
+                                MockMvcRequestBuilders.get(userPath + "/" + 
userId)
+                                        .cookie(cookie)
+                                        
.contentType(MediaType.APPLICATION_JSON_VALUE)
+                                        
.accept(MediaType.APPLICATION_JSON_VALUE))
+                        .andExpect(MockMvcResultMatchers.status().isOk())
+                        .andDo(MockMvcResultHandlers.print())
+                        .andReturn()
+                        .getResponse()
+                        .getContentAsString();
+
+        R<UserVO> r = ObjectMapperUtils.fromJSON(responseString, new 
TypeReference<R<UserVO>>() {});
+        assertEquals(200, r.getCode());
+        return r.getData();
+    }
 }

Reply via email to