|
|
@@ -2,168 +2,28 @@ package seecoder.devcloud.web.service.impl.user;
|
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.gitlab4j.api.GitLabApiException;
|
|
|
-import org.springframework.beans.BeanUtils;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.data.domain.Sort;
|
|
|
import org.springframework.security.core.userdetails.UserDetails;
|
|
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
|
-import org.springframework.security.crypto.password.PasswordEncoder;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.transaction.annotation.Transactional;
|
|
|
-import org.springframework.util.Assert;
|
|
|
-import seecoder.devcloud.api.gitlab.GitlabApi;
|
|
|
-import seecoder.devcloud.api.gitlab.model.GitlabUser;
|
|
|
-import seecoder.devcloud.api.jenkins.JenkinsApi;
|
|
|
-import seecoder.devcloud.common.exceptions.InvalidRequestException;
|
|
|
-import seecoder.devcloud.common.exceptions.ServiceException;
|
|
|
-import seecoder.devcloud.common.util.ToolKit;
|
|
|
-import seecoder.devcloud.web.dao.user.UserMapper;
|
|
|
-import seecoder.devcloud.web.model.vo.user.ChangePasswordVO;
|
|
|
-import seecoder.devcloud.web.model.vo.user.RegisterVO;
|
|
|
|
|
|
-import seecoder.devcloud.web.model.enums.UserIdentity;
|
|
|
import seecoder.devcloud.web.model.po.user.User;
|
|
|
-import seecoder.devcloud.web.model.vo.user.UserVO;
|
|
|
-import seecoder.devcloud.web.service.user.MailService;
|
|
|
import seecoder.devcloud.web.service.user.UserService;
|
|
|
|
|
|
@Service
|
|
|
@Slf4j
|
|
|
public class UserServiceImpl implements UserService, UserDetailsService {
|
|
|
- private static final Sort PAGE_SORT = Sort.by(Sort.Direction.ASC, "id");
|
|
|
|
|
|
- private final UserMapper userMapper;
|
|
|
- private final MailService mailService;
|
|
|
- private final GitlabApi gitlabApi;
|
|
|
- private final JenkinsApi jenkinsApi;
|
|
|
- /**
|
|
|
- * spring security提供的加密
|
|
|
- */
|
|
|
- private final PasswordEncoder passwordEncoder;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- public UserServiceImpl(UserMapper userMapper, MailService mailService, GitlabApi gitlabApi, JenkinsApi jenkinsApi, PasswordEncoder passwordEncoder) {
|
|
|
- this.userMapper = userMapper;
|
|
|
- this.mailService = mailService;
|
|
|
- this.gitlabApi = gitlabApi;
|
|
|
- this.jenkinsApi = jenkinsApi;
|
|
|
- this.passwordEncoder = passwordEncoder;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- @Transactional
|
|
|
- public UserVO register(RegisterVO form) throws ServiceException {
|
|
|
- return register(form.getUsername(), form.getEmail(), form.getPassword(),form.getNickname(), UserIdentity.STUDENT);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- @Transactional
|
|
|
- public UserVO registerTeacher(RegisterVO form) throws ServiceException {
|
|
|
- return register(form.getUsername(), form.getEmail(), form.getPassword(),form.getNickname(), UserIdentity.TEACHER);
|
|
|
- }
|
|
|
-
|
|
|
- private UserVO register(String username, String email, String password, String nickname,UserIdentity role) throws ServiceException {
|
|
|
- User user = userMapper.findByUsername(username);
|
|
|
- Asserts.isNull(user, "用户名已被使用!");
|
|
|
- user = userMapper.findByEmail(email);
|
|
|
- Asserts.isNull(user, "邮箱已被使用!");
|
|
|
- String realPassword = password == null ? ToolKit.randomPassword() : password;
|
|
|
- GitlabUser gitlabUser;
|
|
|
- try {
|
|
|
- gitlabUser = gitlabApi.createUser(username, email, realPassword);
|
|
|
- } catch (GitLabApiException e) {
|
|
|
- log.error(e.getMessage(), e);
|
|
|
- throw new ServiceException("创建GitLab用户时发生异常[" + e.getMessage() + "]");
|
|
|
- }
|
|
|
- user = new User();
|
|
|
- BeanUtils.copyProperties(gitlabUser, user);
|
|
|
- if(nickname!=null){
|
|
|
- user.setNickname(nickname);
|
|
|
- }
|
|
|
-
|
|
|
- user.setUsername(username);
|
|
|
- user.setEmail(email);
|
|
|
- //在gitlab注册后,直接加密
|
|
|
- user.setPassword(passwordEncoder.encode(realPassword));
|
|
|
- user.setRole(role);
|
|
|
- userMapper.insert(user, "created_at");
|
|
|
- final UserVO userVO = new UserVO(user);
|
|
|
- if (password == null) {
|
|
|
- mailService.send("尊敬的" + username + "您好, 您的初始密码为" + realPassword + ", 请及时修改", "注册成功 | SEECODER", email);
|
|
|
- }
|
|
|
- return userVO;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- @Transactional
|
|
|
- public void delete(int userId) throws ServiceException {
|
|
|
- User user = userMapper.findById(userId);
|
|
|
- Assert.notNull(user, "找不到要删除的用户");
|
|
|
- Assert.isTrue(user.getRole() == UserIdentity.STUDENT, "目标用户只能是学生");
|
|
|
- try {
|
|
|
- gitlabApi.deleteUser(userId);
|
|
|
- } catch (Exception e) {
|
|
|
- log.error(e.getMessage(), e);
|
|
|
- throw new ServiceException("删除GitLab用户时发生异常[" + e.getMessage() + "]");
|
|
|
- }
|
|
|
- //todo 删除用户需要连带删除什么
|
|
|
- userMapper.delete(user);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public UserVO getUserById(Integer userid) {
|
|
|
- return new UserVO(userMapper.findById(userid));
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public UserVO changeUserInfo(Integer userid, String nickName) throws ServiceException {
|
|
|
- User one = userMapper.findById(userid);
|
|
|
- if (one == null) {
|
|
|
- throw new ServiceException("用户不存在");
|
|
|
- }
|
|
|
- one.setNickname(nickName);
|
|
|
- userMapper.updateById(one);
|
|
|
- return new UserVO(one);
|
|
|
- }
|
|
|
|
|
|
- @Override
|
|
|
- public void changePassword(ChangePasswordVO form) throws ServiceException {
|
|
|
- User user = userMapper.findById(form.getUserId());
|
|
|
|
|
|
- if (!passwordEncoder.matches(form.getOrigin(),user.getPassword())) {
|
|
|
- throw new InvalidRequestException("原密码验证失败,如忘记密码请联系管理员重置");
|
|
|
- }
|
|
|
- changePassword(user, form.getPassword());
|
|
|
- }
|
|
|
-
|
|
|
- private void changePassword(User user, String password) throws ServiceException {
|
|
|
- try {
|
|
|
- /**
|
|
|
- * todo 修改密码容易导致gitlab与devcloud不一致
|
|
|
- * 管理员每次修改密码,会导致下次普通用户登陆gitlab的时候强制重新改密码
|
|
|
- * 如果这时候学生在gitlab那边改的密码与这里改的不一样,就产生了密码不一致的结果
|
|
|
- * 而且目前官方gitlab client api不支持普通用户通过api改密码
|
|
|
- * 见 https://docs.gitlab.com/ce/api/users.html
|
|
|
- * 所以这个bug暂时无解,最好是让学生建立用户完毕之后不要乱改密码
|
|
|
- */
|
|
|
- gitlabApi.changePassword(user.getId(), password);
|
|
|
- } catch (GitLabApiException e) {
|
|
|
- log.error(e.getMessage(), e);
|
|
|
- throw new ServiceException("修改GitLab用户密码时发生异常[" + e.getMessage() + "]");
|
|
|
- }
|
|
|
- user.setPassword(passwordEncoder.encode(password));
|
|
|
- userMapper.updateById(user);
|
|
|
- }
|
|
|
|
|
|
+ /**
|
|
|
+ * 此后端整合portal门户系统,不需要登陆功能,不用重写此方法
|
|
|
+ */
|
|
|
@Override
|
|
|
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
|
|
- UserDetails u = userMapper.findByUsername(username);
|
|
|
- if (u == null){
|
|
|
- throw new UsernameNotFoundException("此账号不存在");
|
|
|
- }
|
|
|
- return u;
|
|
|
+ //todo 门户功能获取用户
|
|
|
+ return null;
|
|
|
}
|
|
|
|
|
|
|