提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.system.controller.admin.oauth2; |
H |
2 |
|
|
3 |
import cn.hutool.core.collection.CollUtil; |
|
4 |
import com.iailab.framework.common.pojo.CommonResult; |
|
5 |
import com.iailab.framework.common.util.object.BeanUtils; |
|
6 |
import com.iailab.module.system.controller.admin.oauth2.vo.user.OAuth2UserInfoRespVO; |
|
7 |
import com.iailab.module.system.controller.admin.oauth2.vo.user.OAuth2UserUpdateReqVO; |
|
8 |
import com.iailab.module.system.controller.admin.user.vo.profile.UserProfileUpdateReqVO; |
|
9 |
import com.iailab.module.system.dal.dataobject.dept.DeptDO; |
|
10 |
import com.iailab.module.system.dal.dataobject.dept.PostDO; |
|
11 |
import com.iailab.module.system.dal.dataobject.user.AdminUserDO; |
|
12 |
import com.iailab.module.system.service.dept.DeptService; |
|
13 |
import com.iailab.module.system.service.dept.PostService; |
|
14 |
import com.iailab.module.system.service.user.AdminUserService; |
|
15 |
import io.swagger.v3.oas.annotations.Operation; |
|
16 |
import io.swagger.v3.oas.annotations.tags.Tag; |
|
17 |
import lombok.extern.slf4j.Slf4j; |
|
18 |
import org.springframework.security.access.prepost.PreAuthorize; |
|
19 |
import org.springframework.validation.annotation.Validated; |
|
20 |
import org.springframework.web.bind.annotation.*; |
|
21 |
|
|
22 |
import javax.annotation.Resource; |
|
23 |
import javax.validation.Valid; |
|
24 |
import java.util.List; |
|
25 |
|
|
26 |
import static com.iailab.framework.common.pojo.CommonResult.success; |
|
27 |
import static com.iailab.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; |
|
28 |
|
|
29 |
/** |
|
30 |
* 提供给外部应用调用为主 |
|
31 |
* |
|
32 |
* 1. 在 getUserInfo 方法上,添加 @PreAuthorize("@ss.hasScope('user.read')") 注解,声明需要满足 scope = user.read |
|
33 |
* 2. 在 updateUserInfo 方法上,添加 @PreAuthorize("@ss.hasScope('user.write')") 注解,声明需要满足 scope = user.write |
|
34 |
* |
|
35 |
* @author iailab |
|
36 |
*/ |
|
37 |
@Tag(name = "管理后台 - OAuth2.0 用户") |
|
38 |
@RestController |
|
39 |
@RequestMapping("/system/oauth2/user") |
|
40 |
@Validated |
|
41 |
@Slf4j |
|
42 |
public class OAuth2UserController { |
|
43 |
|
|
44 |
@Resource |
|
45 |
private AdminUserService userService; |
|
46 |
@Resource |
|
47 |
private DeptService deptService; |
|
48 |
@Resource |
|
49 |
private PostService postService; |
|
50 |
|
|
51 |
@GetMapping("/get") |
|
52 |
@Operation(summary = "获得用户基本信息") |
|
53 |
@PreAuthorize("@ss.hasScope('user.read')") // |
|
54 |
public CommonResult<OAuth2UserInfoRespVO> getUserInfo() { |
|
55 |
// 获得用户基本信息 |
|
56 |
AdminUserDO user = userService.getUser(getLoginUserId()); |
|
57 |
OAuth2UserInfoRespVO resp = BeanUtils.toBean(user, OAuth2UserInfoRespVO.class); |
|
58 |
// 获得部门信息 |
|
59 |
if (user.getDeptId() != null) { |
|
60 |
DeptDO dept = deptService.getDept(user.getDeptId()); |
|
61 |
resp.setDept(BeanUtils.toBean(dept, OAuth2UserInfoRespVO.Dept.class)); |
|
62 |
} |
|
63 |
// 获得岗位信息 |
|
64 |
if (CollUtil.isNotEmpty(user.getPostIds())) { |
|
65 |
List<PostDO> posts = postService.getPostList(user.getPostIds()); |
|
66 |
resp.setPosts(BeanUtils.toBean(posts, OAuth2UserInfoRespVO.Post.class)); |
|
67 |
} |
|
68 |
return success(resp); |
|
69 |
} |
|
70 |
|
|
71 |
@PutMapping("/update") |
|
72 |
@Operation(summary = "更新用户基本信息") |
|
73 |
@PreAuthorize("@ss.hasScope('user.write')") |
|
74 |
public CommonResult<Boolean> updateUserInfo(@Valid @RequestBody OAuth2UserUpdateReqVO reqVO) { |
|
75 |
// 这里将 UserProfileUpdateReqVO =》UserProfileUpdateReqVO 对象,实现接口的复用。 |
|
76 |
// 主要是,AdminUserService 没有自己的 BO 对象,所以复用只能这么做 |
|
77 |
userService.updateUserProfile(getLoginUserId(), BeanUtils.toBean(reqVO, UserProfileUpdateReqVO.class)); |
|
78 |
return success(true); |
|
79 |
} |
|
80 |
|
|
81 |
} |