|
|
@@ -5,7 +5,6 @@ import org.apache.commons.codec.digest.DigestUtils;
|
|
|
import org.gitlab4j.api.GitLabApiException;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.Sort;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
@@ -21,13 +20,11 @@ import seecoder.devcloud.web.dao.user.UserMapper;
|
|
|
import seecoder.devcloud.web.vo.user.ChangePasswordVO;
|
|
|
import seecoder.devcloud.web.vo.user.RegisterVO;
|
|
|
|
|
|
-import seecoder.devcloud.web.vo.user.UserDTO;
|
|
|
import seecoder.devcloud.web.enums.UserIdentity;
|
|
|
import seecoder.devcloud.web.po.user.User;
|
|
|
-import seecoder.devcloud.web.vo.user.UserSerializer;
|
|
|
+import seecoder.devcloud.web.vo.user.UserVO;
|
|
|
import seecoder.devcloud.web.service.user.MailService;
|
|
|
import seecoder.devcloud.web.service.user.UserService;
|
|
|
-import seecoder.devcloud.web.vo.user.UserVO;
|
|
|
|
|
|
@Service
|
|
|
public class UserServiceImpl implements UserService {
|
|
|
@@ -46,16 +43,19 @@ public class UserServiceImpl implements UserService {
|
|
|
this.jenkinsApi = jenkinsApi;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
@Override
|
|
|
@Transactional
|
|
|
- public UserVO create(UserDTO userDTO) throws ServiceException {
|
|
|
- return register(userDTO.getUsername(), userDTO.getEmail(), null,userDTO.getNickname(), userDTO.getRole());
|
|
|
+ public UserVO register(RegisterVO form) throws ServiceException {
|
|
|
+ return register(form.getUsername(), form.getEmail(), form.getPassword(),form.getNickname(), UserIdentity.STUDENT);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@Transactional
|
|
|
- public User register(String username, String name, String email, String password, String phone, UserIdentity role, Integer pid) throws ServiceException {
|
|
|
+ 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);
|
|
|
@@ -65,45 +65,26 @@ public class UserServiceImpl implements UserService {
|
|
|
try {
|
|
|
gitlabUser = gitlabApi.createUser(username, email, realPassword);
|
|
|
} catch (GitLabApiException e) {
|
|
|
+ e.printStackTrace();
|
|
|
throw new ServiceException("创建GitLab用户时发生异常[" + e.getMessage() + "]");
|
|
|
}
|
|
|
user = new User();
|
|
|
BeanUtils.copyProperties(gitlabUser, user);
|
|
|
- if(name!=null){
|
|
|
- user.setNickname(name);
|
|
|
+ if(nickname!=null){
|
|
|
+ user.setNickname(nickname);
|
|
|
}
|
|
|
|
|
|
user.setUsername(username);
|
|
|
user.setEmail(email);
|
|
|
+ //在gitlab注册后,直接加密
|
|
|
user.setPassword(DigestUtils.sha256Hex(realPassword));
|
|
|
- user.setPhone(phone);
|
|
|
user.setRole(role);
|
|
|
userMapper.insert(user);
|
|
|
+ final UserVO userVO = new UserVO(user);
|
|
|
if (password == null) {
|
|
|
mailService.send("尊敬的" + username + "您好, 您的初始密码为" + realPassword + ", 请及时修改", "注册成功 | SEECODER", email);
|
|
|
}
|
|
|
- return user;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Page<UserVO> retrievePage(int page, int limit, String search) {
|
|
|
- //todo 看下分页怎么写
|
|
|
-// Pageable pageable = PageRequest.of(page, limit, PAGE_SORT);
|
|
|
-// Page<User> pageResult;
|
|
|
-// if (search == null || search.isEmpty()) {
|
|
|
-// pageResult = userMapper.findAll(pageable);
|
|
|
-// } else {
|
|
|
-// pageResult = userMapper.findAllByUsernameLike("%" + search + "%", pageable);
|
|
|
-// }
|
|
|
-// return pageResult.map(UserVO::new);
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public UserVO retrieve(int userId) throws ServiceException {
|
|
|
- User user = userMapper.findById(userId);
|
|
|
- Asserts.notNull(user, "所选用户不存在");
|
|
|
- return new UserVO(user);
|
|
|
+ return userVO;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -122,106 +103,41 @@ public class UserServiceImpl implements UserService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- @Transactional
|
|
|
- public UserSerializer register(RegisterVO form) throws ServiceException {
|
|
|
- return new UserSerializer(register(form.getUsername(), form.getEmail(), form.getPassword(),form.getNickname(), UserIdentity.STUDENT));
|
|
|
- }
|
|
|
- @Override
|
|
|
- @Transactional
|
|
|
- public UserSerializer registerTeacher(RegisterVO form) throws ServiceException {
|
|
|
- return new UserSerializer(register(form.getUsername(), form.getEmail(), form.getPassword(),form.getNickname(), UserIdentity.TEACHER));
|
|
|
+ public UserVO getUserById(Integer userid) {
|
|
|
+ return new UserVO(userMapper.findById(userid));
|
|
|
}
|
|
|
- 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) {
|
|
|
- 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);
|
|
|
- user.setPassword(DigestUtils.sha256Hex(realPassword));
|
|
|
- user.setRole(role);
|
|
|
- userMapper.insert(user);
|
|
|
- final UserVO userVO = new UserVO(user);
|
|
|
- if (password == null) {
|
|
|
- mailService.send("尊敬的" + username + "您好, 您的初始密码为" + realPassword + ", 请及时修改", "注册成功 | SEECODER", email);
|
|
|
- }
|
|
|
- return userVO;
|
|
|
- }
|
|
|
-
|
|
|
|
|
|
@Override
|
|
|
- public UserSerializer getUserById(Integer userid) {
|
|
|
- return new UserSerializer(userMapper.findById(userid));
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public UserSerializer changeUserInfo(Integer userid,String nickName, String bio) throws ServiceException {
|
|
|
+ public UserVO changeUserInfo(Integer userid, String nickName) throws ServiceException {
|
|
|
User one = userMapper.findById(userid);
|
|
|
if (one == null) {
|
|
|
throw new ServiceException("用户不存在");
|
|
|
}
|
|
|
- if(nickName!=null){
|
|
|
- one.setNickname(nickName);
|
|
|
- }
|
|
|
+ one.setNickname(nickName);
|
|
|
userMapper.updateById(one);
|
|
|
- return new UserSerializer(one);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public UserSerializer changeUserPassword(Integer userid,String oldPassword, String newPassword) throws ServiceException {
|
|
|
- User one = userMapper.findById(userid);
|
|
|
- if (one == null) {
|
|
|
- throw new ServiceException("用户不存在");
|
|
|
- }
|
|
|
- if(!one.getPassword().equals(DigestUtils.sha256Hex(oldPassword))){
|
|
|
- throw new ServiceException("原密码错误");
|
|
|
- }else{
|
|
|
- one.setPassword(DigestUtils.sha256Hex(newPassword));
|
|
|
- }
|
|
|
- try {
|
|
|
- gitlabApi.changePassword(userid, newPassword);
|
|
|
- } catch (GitLabApiException e) {
|
|
|
- throw new ServiceException("修改GitLab用户密码时发生异常[" + e.getMessage() + "]");
|
|
|
- }
|
|
|
- mailService.send( "您的新密码为"+newPassword,"密码变更提醒 | SEECODER",one.getEmail());
|
|
|
- userMapper.updateById(one);
|
|
|
- return new UserSerializer(one);
|
|
|
+ return new UserVO(one);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void changePassword(int userId, ChangePasswordVO form) throws ServiceException {
|
|
|
User user = userMapper.findById(userId);
|
|
|
+
|
|
|
if (!user.getPassword().equals(DigestUtils.sha256Hex(form.getOrigin()))) {
|
|
|
throw new InvalidRequestException("原密码验证失败,如忘记密码请联系管理员重置");
|
|
|
}
|
|
|
changePassword(user, form.getPassword(), false);
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public void resetPassword(int userId) throws ServiceException {
|
|
|
- User user = userMapper.findById(userId);
|
|
|
- Asserts.notNull(user, "所选用户不存在");
|
|
|
- String password = ToolKit.randomPassword();
|
|
|
- changePassword(user, password, true);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
private void changePassword(User user, String password, boolean reset) 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) {
|
|
|
throw new ServiceException("修改GitLab用户密码时发生异常[" + e.getMessage() + "]");
|