|
|
@@ -21,6 +21,7 @@ import space.anyi.serve.entity.PageVo;
|
|
|
import space.anyi.serve.entity.Response;
|
|
|
import space.anyi.serve.entity.user.*;
|
|
|
import space.anyi.serve.service.UserService;
|
|
|
+import space.anyi.serve.util.SecurityUtils;
|
|
|
|
|
|
import java.util.List;
|
|
|
import java.util.stream.Collectors;
|
|
|
@@ -93,9 +94,10 @@ public class UserController {
|
|
|
* @return 单条数据
|
|
|
*/
|
|
|
@Operation(summary = "查询用户详情", security = @SecurityRequirement(name = "openApiSecurityScheme"))
|
|
|
- @PreAuthorize("hasAnyRole('ROLE_admin', 'ROLE_user')")
|
|
|
+ @PreAuthorize("hasAnyRole('ROLE_admin', 'ROLE_user', 'ROLE_coach')")
|
|
|
@GetMapping("{id}")
|
|
|
public Response<UserVo> queryById(@Parameter(description = "用户ID") @NotBlank(message = "用户ID不能为空") @PathVariable String id) {
|
|
|
+ assertSelfOrAdmin(id);
|
|
|
User user = this.userService.queryById(Long.valueOf(id));
|
|
|
return Response.ok(UserVo.form(user));
|
|
|
}
|
|
|
@@ -166,9 +168,10 @@ public class UserController {
|
|
|
* @return
|
|
|
*/
|
|
|
@Operation(summary = "更新用户头像", security = @SecurityRequirement(name = "openApiSecurityScheme"))
|
|
|
- @PreAuthorize("hasAnyRole('ROLE_admin', 'ROLE_user')")
|
|
|
+ @PreAuthorize("hasAnyRole('ROLE_admin', 'ROLE_user', 'ROLE_coach')")
|
|
|
@PutMapping("/updateAvatar")
|
|
|
public Response updateUserAvatar(@Valid@RequestBody UpdateUserAvatarDto dto){
|
|
|
+ assertSelfOrAdmin(dto.getId());
|
|
|
User user = new User();
|
|
|
BeanUtils.copyProperties(dto,user);
|
|
|
user.setId(Long.valueOf(dto.getId()));
|
|
|
@@ -181,14 +184,23 @@ public class UserController {
|
|
|
* @return
|
|
|
*/
|
|
|
@Operation(summary = "更新用户密码", security = @SecurityRequirement(name = "openApiSecurityScheme"))
|
|
|
- @PreAuthorize("hasAnyRole('ROLE_admin', 'ROLE_user')")
|
|
|
+ @PreAuthorize("hasAnyRole('ROLE_admin', 'ROLE_user', 'ROLE_coach')")
|
|
|
@PutMapping("/updatePassword")
|
|
|
public Response updatePassword(@Valid @RequestBody UpdateUserPasswordDto dto){
|
|
|
+ assertSelfOrAdmin(dto.getId());
|
|
|
User user = new User();
|
|
|
BeanUtils.copyProperties(dto,user);
|
|
|
user.setId(Long.valueOf(dto.getId()));
|
|
|
return Response.ok(userService.updatePassword(user,dto.getOldPassword()));
|
|
|
}
|
|
|
|
|
|
+ private void assertSelfOrAdmin(String id) {
|
|
|
+ Long targetId = Long.valueOf(id);
|
|
|
+ if (!User.ROLE_ADMIN.equals(SecurityUtils.getCurrentRole())
|
|
|
+ && !targetId.equals(SecurityUtils.getCurrentUserId())) {
|
|
|
+ throw new IllegalArgumentException("只能操作自己的账号");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|