|
|
@@ -2,11 +2,13 @@ package cn.seecoder.seecoderportalserver.service;
|
|
|
|
|
|
import cn.seecoder.seecoderportalcommon.vo.UserRole;
|
|
|
import cn.seecoder.seecoderportalcommon.vo.UserVO;
|
|
|
+import cn.seecoder.seecoderportalcommon.vo.dto.other.PlatformRegisterDTO;
|
|
|
import cn.seecoder.seecoderportalserver.domain.User;
|
|
|
import cn.seecoder.seecoderportalserver.mapper.UserMapper;
|
|
|
import cn.seecoder.seecoderportalserver.repository.UserRepository;
|
|
|
import cn.seecoder.seecoderportalserver.web.rest.errors.CustomErrorException;
|
|
|
import com.nju.edu.gitlab.SeecoderGitlabException;
|
|
|
+import com.nju.edu.gitlab.vo.GitlabUserVO;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
|
@@ -16,7 +18,6 @@ import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
-import java.util.Optional;
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
/**
|
|
|
@@ -43,7 +44,7 @@ public class UserService {
|
|
|
this.gitlabService = gitlabService;
|
|
|
}
|
|
|
|
|
|
- public List<User> findAllUsers(){
|
|
|
+ public List<User> findAllUsers() {
|
|
|
return userRepository.findAll();
|
|
|
}
|
|
|
|
|
|
@@ -56,7 +57,7 @@ public class UserService {
|
|
|
}
|
|
|
|
|
|
@Transactional(rollbackFor = RuntimeException.class)
|
|
|
- public User updateUserName (Long userId, String name) {
|
|
|
+ public User updateUserName(Long userId, String name) {
|
|
|
return userRepository.findById(userId)
|
|
|
.map(user -> {
|
|
|
user.setName(name);
|
|
|
@@ -116,7 +117,7 @@ public class UserService {
|
|
|
User user = userMapper.userVOToUser(userVO);
|
|
|
String encodedPassword = passwordEncoder.encode(password);
|
|
|
user.setPassword(encodedPassword);
|
|
|
- User savedUser=userRepository.save(user);
|
|
|
+ User savedUser = userRepository.save(user);
|
|
|
try {
|
|
|
gitlabService.createGitlabUser(Math.toIntExact(savedUser.getId()), savedUser.getUsername(), password, savedUser.getEmail());
|
|
|
} catch (SeecoderGitlabException e) {
|
|
|
@@ -182,4 +183,41 @@ public class UserService {
|
|
|
studentRoles.add(UserRole.SEEC);
|
|
|
return studentRoles.contains(role);
|
|
|
}
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = RuntimeException.class)
|
|
|
+ public UserVO registerPlatformUser(PlatformRegisterDTO platformRegisterDTO) {
|
|
|
+ // todo: 注册的安全验证
|
|
|
+ UserVO userVO = new UserVO();
|
|
|
+ userVO.setUsername(platformRegisterDTO.getUserId());
|
|
|
+ userRepository.findByUsername(userVO.getUsername()).ifPresent(user -> {
|
|
|
+ throw CustomErrorException.usernameAlreadyExist();
|
|
|
+ });
|
|
|
+ userVO.setEmail(platformRegisterDTO.getEmail());
|
|
|
+ userRepository.findByEmail(userVO.getEmail()).ifPresent(user -> {
|
|
|
+ throw CustomErrorException.emailAlreadyExist();
|
|
|
+ });
|
|
|
+ userVO.setName(platformRegisterDTO.getName());
|
|
|
+ userVO.setPhone("platform" + userVO.getUsername());
|
|
|
+ userVO.setRole(UserRole.STUDENT);
|
|
|
+ User user = userMapper.userVOToUser(userVO);
|
|
|
+ String encodedPassword = passwordEncoder.encode(userVO.getUsername());
|
|
|
+ user.setPassword(encodedPassword);
|
|
|
+ User savedUser = userRepository.save(user);
|
|
|
+ try {
|
|
|
+ GitlabUserVO gitlabUserVO = generateGitlabUser(savedUser);
|
|
|
+ } catch (CustomErrorException customErrorException) {
|
|
|
+ userRepository.delete(savedUser);
|
|
|
+ throw customErrorException;
|
|
|
+ }
|
|
|
+ log.debug("Created Information for Platform User: {}", savedUser);
|
|
|
+ return userMapper.userToUserVO(savedUser);
|
|
|
+ }
|
|
|
+
|
|
|
+ private GitlabUserVO generateGitlabUser(User user) throws CustomErrorException{
|
|
|
+ try {
|
|
|
+ return gitlabService.createGitlabUser(Math.toIntExact(user.getId()), user.getUsername(), user.getPassword(), user.getEmail());
|
|
|
+ } catch (SeecoderGitlabException e) {
|
|
|
+ throw new CustomErrorException("用户名或邮箱已存在,请联系管理人员!");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|