tj
2025-06-05 2d549a04870d1315868a7cf19952b64e8071e711
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package com.cloudroam.controller.cms;
 
import com.cloudroam.dto.projectInfo.QueryProjectInfoDTO;
import com.cloudroam.model.ProjectInfoDO;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.autoconfigure.exception.ParameterException;
import io.github.talelin.core.annotation.AdminRequired;
import io.github.talelin.core.annotation.LoginRequired;
import io.github.talelin.core.annotation.PermissionModule;
import io.github.talelin.core.annotation.RefreshRequired;
import io.github.talelin.core.token.DoubleJWT;
import io.github.talelin.core.token.Tokens;
import com.cloudroam.common.LocalUser;
import com.cloudroam.common.configuration.LoginCaptchaProperties;
import com.cloudroam.dto.user.ChangePasswordDTO;
import com.cloudroam.dto.user.LoginDTO;
import com.cloudroam.dto.user.RegisterDTO;
import com.cloudroam.dto.user.UpdateInfoDTO;
import com.cloudroam.model.GroupDO;
import com.cloudroam.model.UserDO;
import com.cloudroam.service.GroupService;
import com.cloudroam.service.UserIdentityService;
import com.cloudroam.service.UserService;
import com.cloudroam.vo.CreatedVO;
import com.cloudroam.vo.LoginCaptchaVO;
import com.cloudroam.vo.UpdatedVO;
import com.cloudroam.vo.UserInfoVO;
import com.cloudroam.vo.UserPermissionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.security.Security;
import java.util.List;
import java.util.Map;
 
/**
 * 用户控制器
 * @author 
 * @author 
 */
@RestController
@RequestMapping("/cms/user")
@PermissionModule(value = "用户")
@Validated
public class UserController {
 
    @Autowired
    private UserService userService;
 
    @Autowired
    private GroupService groupService;
 
    @Autowired
    private UserIdentityService userIdentityService;
 
    @Autowired
    private DoubleJWT jwt;
 
    @Autowired
    private LoginCaptchaProperties captchaConfig;
 
    /**
     * 用户注册
     */
    @PostMapping("/register")
    @AdminRequired
    public CreatedVO register(@RequestBody @Validated RegisterDTO validator) {
        userService.createUser(validator);
        return new CreatedVO(11);
    }
 
    /**
     * 用户登陆
     */
    @PostMapping("/login")
    public Tokens login(@RequestBody @Validated LoginDTO validator, @RequestHeader(required = false) String tag) {
        if (Boolean.TRUE.equals(captchaConfig.getEnabled())) {
            if (!StringUtils.hasText(validator.getCaptcha()) || !StringUtils.hasText(tag)) {
                throw new ParameterException(10260);
            }
            if (!userService.verifyCaptcha(validator.getCaptcha(), tag)) {
                throw new ParameterException(10260);
            }
        }
        UserDO user = userService.getUserByUsername(validator.getUsername());
        if (user == null) {
            throw new NotFoundException(10021);
        }
        boolean valid = userIdentityService.verifyUsernamePassword(
                user.getId(),
                user.getUsername(),
                validator.getPassword());
        if (!valid) {
            throw new ParameterException(10031);
        }
        return jwt.generateTokens(user.getId());
    }
 
    @PostMapping("/captcha")
    public LoginCaptchaVO userCaptcha() throws Exception {
        if (Boolean.TRUE.equals(captchaConfig.getEnabled())) {
            return userService.generateCaptcha();
        }
        return new LoginCaptchaVO();
    }
 
    /**
     * 更新用户信息
     */
    @PutMapping
    @LoginRequired
    public UpdatedVO update(@RequestBody @Validated UpdateInfoDTO validator) {
        userService.updateUserInfo(validator);
        return new UpdatedVO(6);
    }
 
    /**
     * 修改密码
     */
    @PutMapping("/change_password")
    @LoginRequired
    public UpdatedVO updatePassword(@RequestBody @Validated ChangePasswordDTO validator) {
        userService.changeUserPassword(validator);
        return new UpdatedVO(4);
    }
 
    /**
     * 刷新令牌
     */
    @GetMapping("/refresh")
    @RefreshRequired
    public Tokens getRefreshToken() {
        UserDO user = LocalUser.getLocalUser();
        return jwt.generateTokens(user.getId());
    }
 
    /**
     * 查询拥有权限
     */
    @GetMapping("/permissions")
    @LoginRequired
    public UserPermissionVO getPermissions() {
        UserDO user = LocalUser.getLocalUser();
        boolean admin = groupService.checkIsRootByUserId(user.getId());
        List<Map<String, List<Map<String, String>>>> permissions = userService.getStructuralUserPermissions(user.getId());
        UserPermissionVO userPermissions = new UserPermissionVO(user, permissions);
        userPermissions.setAdmin(admin);
        return userPermissions;
    }
 
    /**
     * 查询自己信息
     */
    @LoginRequired
    @GetMapping("/information")
    public UserInfoVO getInformation() {
        UserDO user = LocalUser.getLocalUser();
        List<GroupDO> groups = groupService.getUserGroupsByUserId(user.getId());
        return new UserInfoVO(user, groups);
    }
}