Skip to content

Commit

Permalink
add: administrator can update user information
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarrettluo committed Jul 27, 2024
1 parent 99f71de commit 03eab94
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 26 deletions.
13 changes: 13 additions & 0 deletions src/main/java/com/jiaruiblog/auth/PermissionEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

@Getter
@AllArgsConstructor
Expand Down Expand Up @@ -30,4 +31,16 @@ public enum PermissionEnum {
*/
private final String msg;

public static PermissionEnum getRoleByName(String name) {
if (StringUtils.isEmpty(name)) {
return null;
}
for (PermissionEnum value : PermissionEnum.values()) {
if (value.name().equals(name)) {
return value;
}
}
return null;
}

}
40 changes: 25 additions & 15 deletions src/main/java/com/jiaruiblog/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ public BaseApiResult getByUsername(@RequestBody RegistryUserDTO user) {
return BaseApiResult.success(one);
}

/**
* @Author luojiarui
* @Description 仅限普通用户对自身的信息进行更新;不能更新其权限信息
* @Date 23:25 2024/7/26
* @Param [userDTO]
* @return com.jiaruiblog.util.BaseApiResult
**/
@ApiOperation(value = "更新用户hobby和company", notes = "更新用户hobby和company")
@PutMapping(value = "/updateUser")
public BaseApiResult updateUser(@RequestBody UserDTO userDTO) {
Expand All @@ -103,6 +110,7 @@ public BaseApiResult updateUser(@RequestBody UserDTO userDTO) {
}
// 检查修改参数信息
UserBO userBO = DTO2BO.userDTO2BO(userDTO);
// 个人用户对自己的信息进行更改
boolean result = userService.updateUserBySelf(userBO);
if (result) {
return BaseApiResult.success("更新成功!");
Expand Down Expand Up @@ -247,14 +255,14 @@ public String login(HttpServletRequest request) {
* @Param [userDTO]
**/
@Permission(PermissionEnum.ADMIN)
@PutMapping("updateUserInfo")
@PutMapping("/auth/updateUserInfo")
public BaseApiResult updateUserInfo(@RequestBody UserDTO userDTO) {
// 传入的参数数据不对,则返回参数不正确
if (checkUserDTOParams(userDTO)) {
return BaseApiResult.error(MessageConstant.PARAMS_ERROR_CODE, MessageConstant.PARAMS_FORMAT_ERROR);
}
UserBO userBO = DTO2BO.userDTO2BO(userDTO);
boolean b = userService.updateUserBySelf(userBO);
boolean b = userService.updateUserByAdmin(userBO);
if (b) {
return BaseApiResult.success(MessageConstant.SUCCESS);
}
Expand All @@ -279,10 +287,6 @@ public BaseApiResult removeUserAvatar(HttpServletRequest request) {
return userService.removeUserAvatar((String) request.getAttribute("id"));
}

private boolean patternMatch(String s, String regex) {
return !Pattern.compile(regex).matcher(s).matches();
}

@ApiOperation(value = "重置用户密码", notes = "管理员对用户进行密码重置")
@PostMapping("auth/resetUserPwd")
public BaseApiResult resetUserPwd(@RequestBody String userId, HttpServletRequest request) {
Expand Down Expand Up @@ -311,27 +315,33 @@ public BaseApiResult resetPassword() {
return BaseApiResult.success();
}

private static boolean patternMatch(String s, String regex) {
return !Pattern.compile(regex).matcher(s).matches();
}

/**
* @Author luojiarui
* @Description 检查用户更新的信息符合要求
* @Date 23:04 2024/7/23
* @Param [userDTO]
* @return boolean 符合要求返回true,不符合要求返回false
**/
private boolean checkUserDTOParams(UserDTO userDTO) {
public static boolean checkUserDTOParams(UserDTO userDTO) {
if (StringUtils.hasText(userDTO.getPassword())
&& patternMatch(userDTO.getPassword(), fieldRegx.get("password"))) {
return true;
&& !patternMatch(userDTO.getPassword(), fieldRegx.get("password"))) {
return false;
}
if (StringUtils.hasText(userDTO.getMail()) && patternMatch(userDTO.getMail(), fieldRegx.get("mail"))) {
return true;
if (StringUtils.hasText(userDTO.getMail())
&& !patternMatch(userDTO.getMail(), fieldRegx.get("mail"))) {
return false;
}

if (StringUtils.hasText(userDTO.getPhone()) && patternMatch(userDTO.getPhone(), fieldRegx.get("phone"))) {
return true;
if (StringUtils.hasText(userDTO.getPhone())
&& !patternMatch(userDTO.getPhone(), fieldRegx.get("phone"))) {
return false;
}
return StringUtils.hasText(userDTO.getDescription())
&& patternMatch(userDTO.getDescription(), fieldRegx.get("description"));
return !(StringUtils.hasText(userDTO.getDescription())
&& !patternMatch(userDTO.getDescription(), fieldRegx.get("description")));
}

}
3 changes: 3 additions & 0 deletions src/main/java/com/jiaruiblog/entity/bo/UserBO.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.jiaruiblog.entity.bo;

import com.jiaruiblog.auth.PermissionEnum;
import lombok.Data;

import java.util.Date;
Expand Down Expand Up @@ -27,4 +28,6 @@ public class UserBO {
private String description;

private Date birthtime;

private PermissionEnum role;
}
2 changes: 2 additions & 0 deletions src/main/java/com/jiaruiblog/entity/dto/UserDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ public class UserDTO {

private Date birthtime;

private String role;

}
9 changes: 9 additions & 0 deletions src/main/java/com/jiaruiblog/service/IUserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,14 @@ public interface IUserService {
* @return boolean 更新个人信息结果
**/
boolean updateUserBySelf(UserBO userBO);

/**
* @Author luojiarui
* @Description 管理员对某个用户的信息进行修改
* @Date 23:34 2024/7/26
* @Param [userBO]
* @return boolean
**/
boolean updateUserByAdmin(UserBO userBO);

}
42 changes: 31 additions & 11 deletions src/main/java/com/jiaruiblog/service/impl/UserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ public BaseApiResult getUserList(BasePageDTO page) {
return BaseApiResult.success(result);
}


@Override
public BaseApiResult changeUserRole(UserRoleDTO userRoleDTO) {
User user = mongoTemplate.findById(userRoleDTO.getUserId(), User.class, COLLECTION_NAME);
Expand Down Expand Up @@ -213,16 +212,16 @@ public boolean isExist(String userId) {
@Override
public boolean updateUserBySelf(UserBO user) {
Query query = new Query(Criteria.where("_id").is(user.getId()));
Update update = new Update();
if (StringUtils.hasText(user.getPassword())) {
update.set("password", user.getPassword());
}
update.set("phone", user.getPhone());
update.set("mail", user.getMail());
update.set("male", user.getMale());
update.set("description", user.getDescription());
update.set("updateDate", new Date());
update.set("birthtime", user.getBirthtime());
Update update = getUserUpdate(user);
UpdateResult updateResult1 = mongoTemplate.updateFirst(query, update, User.class, COLLECTION_NAME);
return updateResult1.getModifiedCount() > 0;
}

@Override
public boolean updateUserByAdmin(UserBO userBO) {
Query query = new Query().addCriteria(Criteria.where("_id").is(userBO.getId()));
Update update = getUserUpdate(userBO);
update.set(ROLE, Optional.ofNullable(userBO.getRole()).orElse(PermissionEnum.USER));
UpdateResult updateResult1 = mongoTemplate.updateFirst(query, update, User.class, COLLECTION_NAME);
return updateResult1.getModifiedCount() > 0;
}
Expand Down Expand Up @@ -396,4 +395,25 @@ public BaseApiResult resetUserPwd(String userId, String adminId) {
return BaseApiResult.success(MessageConstant.SUCCESS);
}

/**
* @Author luojiarui
* @Description 用户自行更新或者管理员更新用户信息的时候操作
* @Date 23:47 2024/7/26
* @Param [user]
* @return org.springframework.data.mongodb.core.query.Update
**/
private Update getUserUpdate(UserBO user) {
Update update = new Update();
if (StringUtils.hasText(user.getPassword())) {
update.set("password", user.getPassword());
}
update.set("phone", user.getPhone());
update.set("mail", user.getMail());
update.set("male", user.getMale());
update.set("description", user.getDescription());
update.set(UPDATE_TIME, new Date());
update.set("birthtime", user.getBirthtime());
return update;
}

}
3 changes: 3 additions & 0 deletions src/main/java/com/jiaruiblog/transformer/DTO2BO.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.jiaruiblog.transformer;

import com.jiaruiblog.auth.PermissionEnum;
import com.jiaruiblog.entity.bo.UserBO;
import com.jiaruiblog.entity.dto.UserDTO;

Expand All @@ -21,12 +22,14 @@ public static UserBO userDTO2BO(UserDTO userDTO) {
if (Objects.isNull(userDTO)) {
return userBO;
}
userBO.setId(userDTO.getId());
userBO.setPassword(userDTO.getPassword());
userBO.setPhone(userDTO.getPhone());
userBO.setMail(userDTO.getMail());
userBO.setMale(userDTO.isMale());
userBO.setBirthtime(userDTO.getBirthtime());
userBO.setDescription(userDTO.getDescription());
userBO.setRole(PermissionEnum.getRoleByName(userDTO.getRole()));
return userBO;
}

Expand Down
16 changes: 16 additions & 0 deletions src/test/java/com/jiaruiblog/controller/UserControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.jiaruiblog.DocumentSharingSiteApplication;
import com.jiaruiblog.common.MessageConstant;
import com.jiaruiblog.entity.dto.RegistryUserDTO;
import com.jiaruiblog.entity.dto.UserDTO;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.After;
import org.junit.Assert;
Expand All @@ -25,6 +26,7 @@
import org.springframework.web.context.WebApplicationContext;

import java.nio.charset.Charset;
import java.util.Date;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = DocumentSharingSiteApplication.class)
Expand Down Expand Up @@ -247,4 +249,18 @@ public void allUsers() {
@Test
public void testLogin() {
}

@Test
public void testCheckUserDTOParams() {
UserDTO userDTO = new UserDTO();
userDTO.setId("6662d1f26238ff0284d4c957");
userDTO.setPhone("123456789098");
userDTO.setMail("[email protected]");
userDTO.setMale(false);
userDTO.setDescription("这是我的个人签名");
userDTO.setBirthtime(new Date());

boolean b = UserController.checkUserDTOParams(userDTO);
Assert.assertEquals(true, b);
}
}

0 comments on commit 03eab94

Please sign in to comment.