Skip to content

Commit 03eab94

Browse files
committed
add: administrator can update user information
1 parent 99f71de commit 03eab94

File tree

8 files changed

+102
-26
lines changed

8 files changed

+102
-26
lines changed

src/main/java/com/jiaruiblog/auth/PermissionEnum.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import lombok.AllArgsConstructor;
44
import lombok.Getter;
5+
import org.apache.commons.lang3.StringUtils;
56

67
@Getter
78
@AllArgsConstructor
@@ -30,4 +31,16 @@ public enum PermissionEnum {
3031
*/
3132
private final String msg;
3233

34+
public static PermissionEnum getRoleByName(String name) {
35+
if (StringUtils.isEmpty(name)) {
36+
return null;
37+
}
38+
for (PermissionEnum value : PermissionEnum.values()) {
39+
if (value.name().equals(name)) {
40+
return value;
41+
}
42+
}
43+
return null;
44+
}
45+
3346
}

src/main/java/com/jiaruiblog/controller/UserController.java

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ public BaseApiResult getByUsername(@RequestBody RegistryUserDTO user) {
9494
return BaseApiResult.success(one);
9595
}
9696

97+
/**
98+
* @Author luojiarui
99+
* @Description 仅限普通用户对自身的信息进行更新;不能更新其权限信息
100+
* @Date 23:25 2024/7/26
101+
* @Param [userDTO]
102+
* @return com.jiaruiblog.util.BaseApiResult
103+
**/
97104
@ApiOperation(value = "更新用户hobby和company", notes = "更新用户hobby和company")
98105
@PutMapping(value = "/updateUser")
99106
public BaseApiResult updateUser(@RequestBody UserDTO userDTO) {
@@ -103,6 +110,7 @@ public BaseApiResult updateUser(@RequestBody UserDTO userDTO) {
103110
}
104111
// 检查修改参数信息
105112
UserBO userBO = DTO2BO.userDTO2BO(userDTO);
113+
// 个人用户对自己的信息进行更改
106114
boolean result = userService.updateUserBySelf(userBO);
107115
if (result) {
108116
return BaseApiResult.success("更新成功!");
@@ -247,14 +255,14 @@ public String login(HttpServletRequest request) {
247255
* @Param [userDTO]
248256
**/
249257
@Permission(PermissionEnum.ADMIN)
250-
@PutMapping("updateUserInfo")
258+
@PutMapping("/auth/updateUserInfo")
251259
public BaseApiResult updateUserInfo(@RequestBody UserDTO userDTO) {
252260
// 传入的参数数据不对,则返回参数不正确
253261
if (checkUserDTOParams(userDTO)) {
254262
return BaseApiResult.error(MessageConstant.PARAMS_ERROR_CODE, MessageConstant.PARAMS_FORMAT_ERROR);
255263
}
256264
UserBO userBO = DTO2BO.userDTO2BO(userDTO);
257-
boolean b = userService.updateUserBySelf(userBO);
265+
boolean b = userService.updateUserByAdmin(userBO);
258266
if (b) {
259267
return BaseApiResult.success(MessageConstant.SUCCESS);
260268
}
@@ -279,10 +287,6 @@ public BaseApiResult removeUserAvatar(HttpServletRequest request) {
279287
return userService.removeUserAvatar((String) request.getAttribute("id"));
280288
}
281289

282-
private boolean patternMatch(String s, String regex) {
283-
return !Pattern.compile(regex).matcher(s).matches();
284-
}
285-
286290
@ApiOperation(value = "重置用户密码", notes = "管理员对用户进行密码重置")
287291
@PostMapping("auth/resetUserPwd")
288292
public BaseApiResult resetUserPwd(@RequestBody String userId, HttpServletRequest request) {
@@ -311,27 +315,33 @@ public BaseApiResult resetPassword() {
311315
return BaseApiResult.success();
312316
}
313317

318+
private static boolean patternMatch(String s, String regex) {
319+
return !Pattern.compile(regex).matcher(s).matches();
320+
}
321+
314322
/**
315323
* @Author luojiarui
316324
* @Description 检查用户更新的信息符合要求
317325
* @Date 23:04 2024/7/23
318326
* @Param [userDTO]
319327
* @return boolean 符合要求返回true,不符合要求返回false
320328
**/
321-
private boolean checkUserDTOParams(UserDTO userDTO) {
329+
public static boolean checkUserDTOParams(UserDTO userDTO) {
322330
if (StringUtils.hasText(userDTO.getPassword())
323-
&& patternMatch(userDTO.getPassword(), fieldRegx.get("password"))) {
324-
return true;
331+
&& !patternMatch(userDTO.getPassword(), fieldRegx.get("password"))) {
332+
return false;
325333
}
326-
if (StringUtils.hasText(userDTO.getMail()) && patternMatch(userDTO.getMail(), fieldRegx.get("mail"))) {
327-
return true;
334+
if (StringUtils.hasText(userDTO.getMail())
335+
&& !patternMatch(userDTO.getMail(), fieldRegx.get("mail"))) {
336+
return false;
328337
}
329338

330-
if (StringUtils.hasText(userDTO.getPhone()) && patternMatch(userDTO.getPhone(), fieldRegx.get("phone"))) {
331-
return true;
339+
if (StringUtils.hasText(userDTO.getPhone())
340+
&& !patternMatch(userDTO.getPhone(), fieldRegx.get("phone"))) {
341+
return false;
332342
}
333-
return StringUtils.hasText(userDTO.getDescription())
334-
&& patternMatch(userDTO.getDescription(), fieldRegx.get("description"));
343+
return !(StringUtils.hasText(userDTO.getDescription())
344+
&& !patternMatch(userDTO.getDescription(), fieldRegx.get("description")));
335345
}
336346

337347
}

src/main/java/com/jiaruiblog/entity/bo/UserBO.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.jiaruiblog.entity.bo;
22

3+
import com.jiaruiblog.auth.PermissionEnum;
34
import lombok.Data;
45

56
import java.util.Date;
@@ -27,4 +28,6 @@ public class UserBO {
2728
private String description;
2829

2930
private Date birthtime;
31+
32+
private PermissionEnum role;
3033
}

src/main/java/com/jiaruiblog/entity/dto/UserDTO.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@ public class UserDTO {
2828

2929
private Date birthtime;
3030

31+
private String role;
32+
3133
}

src/main/java/com/jiaruiblog/service/IUserService.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,14 @@ public interface IUserService {
102102
* @return boolean 更新个人信息结果
103103
**/
104104
boolean updateUserBySelf(UserBO userBO);
105+
106+
/**
107+
* @Author luojiarui
108+
* @Description 管理员对某个用户的信息进行修改
109+
* @Date 23:34 2024/7/26
110+
* @Param [userBO]
111+
* @return boolean
112+
**/
113+
boolean updateUserByAdmin(UserBO userBO);
105114

106115
}

src/main/java/com/jiaruiblog/service/impl/UserServiceImpl.java

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ public BaseApiResult getUserList(BasePageDTO page) {
164164
return BaseApiResult.success(result);
165165
}
166166

167-
168167
@Override
169168
public BaseApiResult changeUserRole(UserRoleDTO userRoleDTO) {
170169
User user = mongoTemplate.findById(userRoleDTO.getUserId(), User.class, COLLECTION_NAME);
@@ -213,16 +212,16 @@ public boolean isExist(String userId) {
213212
@Override
214213
public boolean updateUserBySelf(UserBO user) {
215214
Query query = new Query(Criteria.where("_id").is(user.getId()));
216-
Update update = new Update();
217-
if (StringUtils.hasText(user.getPassword())) {
218-
update.set("password", user.getPassword());
219-
}
220-
update.set("phone", user.getPhone());
221-
update.set("mail", user.getMail());
222-
update.set("male", user.getMale());
223-
update.set("description", user.getDescription());
224-
update.set("updateDate", new Date());
225-
update.set("birthtime", user.getBirthtime());
215+
Update update = getUserUpdate(user);
216+
UpdateResult updateResult1 = mongoTemplate.updateFirst(query, update, User.class, COLLECTION_NAME);
217+
return updateResult1.getModifiedCount() > 0;
218+
}
219+
220+
@Override
221+
public boolean updateUserByAdmin(UserBO userBO) {
222+
Query query = new Query().addCriteria(Criteria.where("_id").is(userBO.getId()));
223+
Update update = getUserUpdate(userBO);
224+
update.set(ROLE, Optional.ofNullable(userBO.getRole()).orElse(PermissionEnum.USER));
226225
UpdateResult updateResult1 = mongoTemplate.updateFirst(query, update, User.class, COLLECTION_NAME);
227226
return updateResult1.getModifiedCount() > 0;
228227
}
@@ -396,4 +395,25 @@ public BaseApiResult resetUserPwd(String userId, String adminId) {
396395
return BaseApiResult.success(MessageConstant.SUCCESS);
397396
}
398397

398+
/**
399+
* @Author luojiarui
400+
* @Description 用户自行更新或者管理员更新用户信息的时候操作
401+
* @Date 23:47 2024/7/26
402+
* @Param [user]
403+
* @return org.springframework.data.mongodb.core.query.Update
404+
**/
405+
private Update getUserUpdate(UserBO user) {
406+
Update update = new Update();
407+
if (StringUtils.hasText(user.getPassword())) {
408+
update.set("password", user.getPassword());
409+
}
410+
update.set("phone", user.getPhone());
411+
update.set("mail", user.getMail());
412+
update.set("male", user.getMale());
413+
update.set("description", user.getDescription());
414+
update.set(UPDATE_TIME, new Date());
415+
update.set("birthtime", user.getBirthtime());
416+
return update;
417+
}
418+
399419
}

src/main/java/com/jiaruiblog/transformer/DTO2BO.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.jiaruiblog.transformer;
22

3+
import com.jiaruiblog.auth.PermissionEnum;
34
import com.jiaruiblog.entity.bo.UserBO;
45
import com.jiaruiblog.entity.dto.UserDTO;
56

@@ -21,12 +22,14 @@ public static UserBO userDTO2BO(UserDTO userDTO) {
2122
if (Objects.isNull(userDTO)) {
2223
return userBO;
2324
}
25+
userBO.setId(userDTO.getId());
2426
userBO.setPassword(userDTO.getPassword());
2527
userBO.setPhone(userDTO.getPhone());
2628
userBO.setMail(userDTO.getMail());
2729
userBO.setMale(userDTO.isMale());
2830
userBO.setBirthtime(userDTO.getBirthtime());
2931
userBO.setDescription(userDTO.getDescription());
32+
userBO.setRole(PermissionEnum.getRoleByName(userDTO.getRole()));
3033
return userBO;
3134
}
3235

src/test/java/com/jiaruiblog/controller/UserControllerTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.jiaruiblog.DocumentSharingSiteApplication;
66
import com.jiaruiblog.common.MessageConstant;
77
import com.jiaruiblog.entity.dto.RegistryUserDTO;
8+
import com.jiaruiblog.entity.dto.UserDTO;
89
import org.apache.commons.lang3.RandomStringUtils;
910
import org.junit.After;
1011
import org.junit.Assert;
@@ -25,6 +26,7 @@
2526
import org.springframework.web.context.WebApplicationContext;
2627

2728
import java.nio.charset.Charset;
29+
import java.util.Date;
2830

2931
@RunWith(SpringRunner.class)
3032
@SpringBootTest(classes = DocumentSharingSiteApplication.class)
@@ -247,4 +249,18 @@ public void allUsers() {
247249
@Test
248250
public void testLogin() {
249251
}
252+
253+
@Test
254+
public void testCheckUserDTOParams() {
255+
UserDTO userDTO = new UserDTO();
256+
userDTO.setId("6662d1f26238ff0284d4c957");
257+
userDTO.setPhone("123456789098");
258+
userDTO.setMail("jiarui.luo@163.com");
259+
userDTO.setMale(false);
260+
userDTO.setDescription("这是我的个人签名");
261+
userDTO.setBirthtime(new Date());
262+
263+
boolean b = UserController.checkUserDTOParams(userDTO);
264+
Assert.assertEquals(true, b);
265+
}
250266
}

0 commit comments

Comments
 (0)