Feature/admin user management - #1
Merged
Merged
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The new endpoints/service include API/operational issues (PATCH semantics vs required full payload, unconditional tokenVersion bump causing forced logout, and inefficient/racy username uniqueness check) that should be addressed before merge.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Adds admin-side user management capabilities (create/update/delete) to the existing admin user module, including password hashing and additional validation/guardrails.
Changes:
- Added admin endpoints to create users, update user details, and delete users.
- Implemented corresponding service-layer logic with password encoding, uniqueness checks, and audit logging.
- Added DTOs for create/update requests and expanded unit tests for the new behaviors.
File summaries
| File | Description |
|---|---|
| src/main/java/club/muimi/backend/controller/admin/AdminUserController.java | Exposes new admin REST endpoints for create/update/delete user operations. |
| src/main/java/club/muimi/backend/service/admin/AdminUserService.java | Implements create/update/delete logic, password hashing, uniqueness validation, and audit logs. |
| src/main/java/club/muimi/backend/repository/UserRepository.java | Adds findByUsername to support username conflict checks. |
| src/main/java/club/muimi/backend/dto/admin/CreateUserRequest.java | Introduces request DTO + validation for creating users. |
| src/main/java/club/muimi/backend/dto/admin/UpdateUserRequest.java | Introduces request DTO + validation for updating user info (including optional password change). |
| src/test/java/club/muimi/backend/service/admin/AdminUserServiceTest.java | Adds unit tests covering create/update/delete constraints and password encoding behavior. |
Review details
- Files reviewed: 6/6 changed files
- Comments generated: 3
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+59
to
+65
| @RequestMapping(value = "/{userId}", method = {RequestMethod.PUT, RequestMethod.PATCH}) | ||
| public ApiResponse<AdminUserDetailVo> updateUser( | ||
| @PathVariable Long userId, | ||
| @Valid @RequestBody UpdateUserRequest request | ||
| ) { | ||
| return ApiResponse.success(adminUserService.updateUser(userId, request), "用户信息更新成功"); | ||
| } |
Comment on lines
+232
to
+245
| User user = getUserOrThrow(userId); | ||
| ensureUsernameAndEmailAvailable(request.username(), request.email(), userId); | ||
| if (request.password() != null && !request.password().isBlank()) { | ||
| validatePasswordPair(request.password(), request.confirmPassword()); | ||
| validatePasswordStrength(request.password()); | ||
| user.setPasswordHash(requirePasswordEncoder().encode(request.password())); | ||
| } | ||
| user.setUsername(request.username().trim()); | ||
| user.setEmail(request.email().trim()); | ||
| user.setRole(request.role()); | ||
| user.setStatus(request.status()); | ||
| user.setEmailVerified(Boolean.TRUE.equals(request.emailVerified())); | ||
| user.setTokenVersion(user.getTokenVersion() + 1); | ||
| try { |
Comment on lines
+291
to
+303
| private void ensureUsernameAndEmailAvailable(String username, String email, Long excludedUserId) { | ||
| userRepository.findByEmail(email.trim()).ifPresent(existing -> { | ||
| if (!Objects.equals(existing.getId(), excludedUserId)) { | ||
| throw new ConflictException("邮箱已被注册"); | ||
| } | ||
| }); | ||
| if (userRepository.existsByUsername(username.trim())) { | ||
| User existing = userRepository.findByUsername(username.trim()).orElse(null); | ||
| if (existing == null || !Objects.equals(existing.getId(), excludedUserId)) { | ||
| throw new ConflictException("用户名已存在"); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.