|
@@ -6,8 +6,6 @@ import org.gitlab4j.api.GitLabApiException;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.domain.Page;
|
|
import org.springframework.data.domain.Page;
|
|
|
-import org.springframework.data.domain.PageRequest;
|
|
|
|
|
-import org.springframework.data.domain.Pageable;
|
|
|
|
|
import org.springframework.data.domain.Sort;
|
|
import org.springframework.data.domain.Sort;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
@@ -19,11 +17,11 @@ import seecoder.devcloud.common.exceptions.Asserts;
|
|
|
import seecoder.devcloud.common.exceptions.InvalidRequestException;
|
|
import seecoder.devcloud.common.exceptions.InvalidRequestException;
|
|
|
import seecoder.devcloud.common.exceptions.ServiceException;
|
|
import seecoder.devcloud.common.exceptions.ServiceException;
|
|
|
import seecoder.devcloud.common.util.ToolKit;
|
|
import seecoder.devcloud.common.util.ToolKit;
|
|
|
-import seecoder.devcloud.web.dao.user.UserDAO;
|
|
|
|
|
|
|
+import seecoder.devcloud.web.dao.user.UserMapper;
|
|
|
import seecoder.devcloud.web.vo.user.ChangePasswordVO;
|
|
import seecoder.devcloud.web.vo.user.ChangePasswordVO;
|
|
|
import seecoder.devcloud.web.vo.user.RegisterVO;
|
|
import seecoder.devcloud.web.vo.user.RegisterVO;
|
|
|
|
|
|
|
|
-import seecoder.devcloud.web.dto.user.UserDTO;
|
|
|
|
|
|
|
+import seecoder.devcloud.web.vo.user.UserDTO;
|
|
|
import seecoder.devcloud.web.enums.UserIdentity;
|
|
import seecoder.devcloud.web.enums.UserIdentity;
|
|
|
import seecoder.devcloud.web.po.user.User;
|
|
import seecoder.devcloud.web.po.user.User;
|
|
|
import seecoder.devcloud.web.vo.user.UserSerializer;
|
|
import seecoder.devcloud.web.vo.user.UserSerializer;
|
|
@@ -35,19 +33,20 @@ import seecoder.devcloud.web.vo.user.UserVO;
|
|
|
public class UserServiceImpl implements UserService {
|
|
public class UserServiceImpl implements UserService {
|
|
|
private static final Sort PAGE_SORT = Sort.by(Sort.Direction.ASC, "id");
|
|
private static final Sort PAGE_SORT = Sort.by(Sort.Direction.ASC, "id");
|
|
|
|
|
|
|
|
- private final UserDAO userDAO;
|
|
|
|
|
|
|
+ private final UserMapper userMapper;
|
|
|
private final MailService mailService;
|
|
private final MailService mailService;
|
|
|
private final GitlabApi gitlabApi;
|
|
private final GitlabApi gitlabApi;
|
|
|
private final JenkinsApi jenkinsApi;
|
|
private final JenkinsApi jenkinsApi;
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
|
- public UserServiceImpl(UserDAO userDAO, MailService mailService, GitlabApi gitlabApi, JenkinsApi jenkinsApi) {
|
|
|
|
|
- this.userDAO = userDAO;
|
|
|
|
|
|
|
+ public UserServiceImpl(UserMapper userMapper, MailService mailService, GitlabApi gitlabApi, JenkinsApi jenkinsApi) {
|
|
|
|
|
+ this.userMapper = userMapper;
|
|
|
this.mailService = mailService;
|
|
this.mailService = mailService;
|
|
|
this.gitlabApi = gitlabApi;
|
|
this.gitlabApi = gitlabApi;
|
|
|
this.jenkinsApi = jenkinsApi;
|
|
this.jenkinsApi = jenkinsApi;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
@Transactional
|
|
@Transactional
|
|
|
public UserVO create(UserDTO userDTO) throws ServiceException {
|
|
public UserVO create(UserDTO userDTO) throws ServiceException {
|
|
@@ -57,9 +56,9 @@ public class UserServiceImpl implements UserService {
|
|
|
@Override
|
|
@Override
|
|
|
@Transactional
|
|
@Transactional
|
|
|
public User register(String username, String name, String email, String password, String phone, UserIdentity role, Integer pid) throws ServiceException {
|
|
public User register(String username, String name, String email, String password, String phone, UserIdentity role, Integer pid) throws ServiceException {
|
|
|
- User user = userDAO.findByUsername(username);
|
|
|
|
|
|
|
+ User user = userMapper.findByUsername(username);
|
|
|
Asserts.isNull(user, "用户名已被使用!");
|
|
Asserts.isNull(user, "用户名已被使用!");
|
|
|
- user = userDAO.findByEmail(email);
|
|
|
|
|
|
|
+ user = userMapper.findByEmail(email);
|
|
|
Asserts.isNull(user, "邮箱已被使用!");
|
|
Asserts.isNull(user, "邮箱已被使用!");
|
|
|
String realPassword = password == null ? ToolKit.randomPassword() : password;
|
|
String realPassword = password == null ? ToolKit.randomPassword() : password;
|
|
|
GitlabUser gitlabUser;
|
|
GitlabUser gitlabUser;
|
|
@@ -77,10 +76,9 @@ public class UserServiceImpl implements UserService {
|
|
|
user.setUsername(username);
|
|
user.setUsername(username);
|
|
|
user.setEmail(email);
|
|
user.setEmail(email);
|
|
|
user.setPassword(DigestUtils.sha256Hex(realPassword));
|
|
user.setPassword(DigestUtils.sha256Hex(realPassword));
|
|
|
- user.setPid(pid);
|
|
|
|
|
user.setPhone(phone);
|
|
user.setPhone(phone);
|
|
|
user.setRole(role);
|
|
user.setRole(role);
|
|
|
- user = userDAO.save(user);
|
|
|
|
|
|
|
+ userMapper.insert(user);
|
|
|
if (password == null) {
|
|
if (password == null) {
|
|
|
mailService.send("尊敬的" + username + "您好, 您的初始密码为" + realPassword + ", 请及时修改", "注册成功 | SEECODER", email);
|
|
mailService.send("尊敬的" + username + "您好, 您的初始密码为" + realPassword + ", 请及时修改", "注册成功 | SEECODER", email);
|
|
|
}
|
|
}
|
|
@@ -89,19 +87,21 @@ public class UserServiceImpl implements UserService {
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public Page<UserVO> retrievePage(int page, int limit, String search) {
|
|
public Page<UserVO> retrievePage(int page, int limit, String search) {
|
|
|
- Pageable pageable = PageRequest.of(page, limit, PAGE_SORT);
|
|
|
|
|
- Page<User> pageResult;
|
|
|
|
|
- if (search == null || search.isEmpty()) {
|
|
|
|
|
- pageResult = userDAO.findAll(pageable);
|
|
|
|
|
- } else {
|
|
|
|
|
- pageResult = userDAO.findAllByUsernameLike("%" + search + "%", pageable);
|
|
|
|
|
- }
|
|
|
|
|
- return pageResult.map(UserVO::new);
|
|
|
|
|
|
|
+ //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
|
|
@Override
|
|
|
public UserVO retrieve(int userId) throws ServiceException {
|
|
public UserVO retrieve(int userId) throws ServiceException {
|
|
|
- User user = userDAO.findById(userId);
|
|
|
|
|
|
|
+ User user = userMapper.findById(userId);
|
|
|
Asserts.notNull(user, "所选用户不存在");
|
|
Asserts.notNull(user, "所选用户不存在");
|
|
|
return new UserVO(user);
|
|
return new UserVO(user);
|
|
|
}
|
|
}
|
|
@@ -109,7 +109,7 @@ public class UserServiceImpl implements UserService {
|
|
|
@Override
|
|
@Override
|
|
|
@Transactional
|
|
@Transactional
|
|
|
public void delete(int userId) throws ServiceException {
|
|
public void delete(int userId) throws ServiceException {
|
|
|
- User user = userDAO.findById(userId);
|
|
|
|
|
|
|
+ User user = userMapper.findById(userId);
|
|
|
Assert.notNull(user, "找不到要删除的用户");
|
|
Assert.notNull(user, "找不到要删除的用户");
|
|
|
Assert.isTrue(user.getRole() == UserIdentity.STUDENT, "目标用户只能是学生");
|
|
Assert.isTrue(user.getRole() == UserIdentity.STUDENT, "目标用户只能是学生");
|
|
|
try {
|
|
try {
|
|
@@ -118,7 +118,7 @@ public class UserServiceImpl implements UserService {
|
|
|
throw new ServiceException("删除GitLab用户时发生异常[" + e.getMessage() + "]");
|
|
throw new ServiceException("删除GitLab用户时发生异常[" + e.getMessage() + "]");
|
|
|
}
|
|
}
|
|
|
//todo 删除用户需要连带删除什么
|
|
//todo 删除用户需要连带删除什么
|
|
|
- userDAO.delete(user);
|
|
|
|
|
|
|
+ userMapper.delete(user);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -132,9 +132,9 @@ public class UserServiceImpl implements UserService {
|
|
|
return new UserSerializer(register(form.getUsername(), form.getEmail(), form.getPassword(),form.getNickname(), UserIdentity.TEACHER));
|
|
return new UserSerializer(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 {
|
|
private UserVO register(String username, String email, String password, String nickname,UserIdentity role) throws ServiceException {
|
|
|
- User user = userDAO.findByUsername(username);
|
|
|
|
|
|
|
+ User user = userMapper.findByUsername(username);
|
|
|
Asserts.isNull(user, "用户名已被使用!");
|
|
Asserts.isNull(user, "用户名已被使用!");
|
|
|
- user = userDAO.findByEmail(email);
|
|
|
|
|
|
|
+ user = userMapper.findByEmail(email);
|
|
|
Asserts.isNull(user, "邮箱已被使用!");
|
|
Asserts.isNull(user, "邮箱已被使用!");
|
|
|
String realPassword = password == null ? ToolKit.randomPassword() : password;
|
|
String realPassword = password == null ? ToolKit.randomPassword() : password;
|
|
|
GitlabUser gitlabUser;
|
|
GitlabUser gitlabUser;
|
|
@@ -153,7 +153,8 @@ public class UserServiceImpl implements UserService {
|
|
|
user.setEmail(email);
|
|
user.setEmail(email);
|
|
|
user.setPassword(DigestUtils.sha256Hex(realPassword));
|
|
user.setPassword(DigestUtils.sha256Hex(realPassword));
|
|
|
user.setRole(role);
|
|
user.setRole(role);
|
|
|
- final UserVO userVO = new UserVO(userDAO.save(user));
|
|
|
|
|
|
|
+ userMapper.insert(user);
|
|
|
|
|
+ final UserVO userVO = new UserVO(user);
|
|
|
if (password == null) {
|
|
if (password == null) {
|
|
|
mailService.send("尊敬的" + username + "您好, 您的初始密码为" + realPassword + ", 请及时修改", "注册成功 | SEECODER", email);
|
|
mailService.send("尊敬的" + username + "您好, 您的初始密码为" + realPassword + ", 请及时修改", "注册成功 | SEECODER", email);
|
|
|
}
|
|
}
|
|
@@ -163,27 +164,25 @@ public class UserServiceImpl implements UserService {
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public UserSerializer getUserById(Integer userid) {
|
|
public UserSerializer getUserById(Integer userid) {
|
|
|
- return new UserSerializer(userDAO.getOne(userid));
|
|
|
|
|
|
|
+ return new UserSerializer(userMapper.findById(userid));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public UserSerializer changeUserInfo(Integer userid,String nickName, String bio) throws ServiceException {
|
|
public UserSerializer changeUserInfo(Integer userid,String nickName, String bio) throws ServiceException {
|
|
|
- User one = userDAO.findById(userid).orElse(null);
|
|
|
|
|
|
|
+ User one = userMapper.findById(userid);
|
|
|
if (one == null) {
|
|
if (one == null) {
|
|
|
throw new ServiceException("用户不存在");
|
|
throw new ServiceException("用户不存在");
|
|
|
}
|
|
}
|
|
|
if(nickName!=null){
|
|
if(nickName!=null){
|
|
|
one.setNickname(nickName);
|
|
one.setNickname(nickName);
|
|
|
}
|
|
}
|
|
|
- if(bio!=null){
|
|
|
|
|
- one.setBio(bio);
|
|
|
|
|
- }
|
|
|
|
|
- return new UserSerializer(userDAO.save(one));
|
|
|
|
|
|
|
+ userMapper.updateById(one);
|
|
|
|
|
+ return new UserSerializer(one);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public UserSerializer changeUserPassword(Integer userid,String oldPassword, String newPassword) throws ServiceException {
|
|
public UserSerializer changeUserPassword(Integer userid,String oldPassword, String newPassword) throws ServiceException {
|
|
|
- User one = userDAO.findById(userid).orElse(null);
|
|
|
|
|
|
|
+ User one = userMapper.findById(userid);
|
|
|
if (one == null) {
|
|
if (one == null) {
|
|
|
throw new ServiceException("用户不存在");
|
|
throw new ServiceException("用户不存在");
|
|
|
}
|
|
}
|
|
@@ -198,12 +197,13 @@ public class UserServiceImpl implements UserService {
|
|
|
throw new ServiceException("修改GitLab用户密码时发生异常[" + e.getMessage() + "]");
|
|
throw new ServiceException("修改GitLab用户密码时发生异常[" + e.getMessage() + "]");
|
|
|
}
|
|
}
|
|
|
mailService.send( "您的新密码为"+newPassword,"密码变更提醒 | SEECODER",one.getEmail());
|
|
mailService.send( "您的新密码为"+newPassword,"密码变更提醒 | SEECODER",one.getEmail());
|
|
|
- return new UserSerializer(userDAO.save(one));
|
|
|
|
|
|
|
+ userMapper.updateById(one);
|
|
|
|
|
+ return new UserSerializer(one);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public void changePassword(int userId, ChangePasswordVO form) throws ServiceException {
|
|
public void changePassword(int userId, ChangePasswordVO form) throws ServiceException {
|
|
|
- User user = userDAO.findById(userId);
|
|
|
|
|
|
|
+ User user = userMapper.findById(userId);
|
|
|
if (!user.getPassword().equals(DigestUtils.sha256Hex(form.getOrigin()))) {
|
|
if (!user.getPassword().equals(DigestUtils.sha256Hex(form.getOrigin()))) {
|
|
|
throw new InvalidRequestException("原密码验证失败,如忘记密码请联系管理员重置");
|
|
throw new InvalidRequestException("原密码验证失败,如忘记密码请联系管理员重置");
|
|
|
}
|
|
}
|
|
@@ -212,7 +212,7 @@ public class UserServiceImpl implements UserService {
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public void resetPassword(int userId) throws ServiceException {
|
|
public void resetPassword(int userId) throws ServiceException {
|
|
|
- User user = userDAO.findById(userId);
|
|
|
|
|
|
|
+ User user = userMapper.findById(userId);
|
|
|
Asserts.notNull(user, "所选用户不存在");
|
|
Asserts.notNull(user, "所选用户不存在");
|
|
|
String password = ToolKit.randomPassword();
|
|
String password = ToolKit.randomPassword();
|
|
|
changePassword(user, password, true);
|
|
changePassword(user, password, true);
|
|
@@ -227,7 +227,7 @@ public class UserServiceImpl implements UserService {
|
|
|
throw new ServiceException("修改GitLab用户密码时发生异常[" + e.getMessage() + "]");
|
|
throw new ServiceException("修改GitLab用户密码时发生异常[" + e.getMessage() + "]");
|
|
|
}
|
|
}
|
|
|
user.setPassword(DigestUtils.sha256Hex(password));
|
|
user.setPassword(DigestUtils.sha256Hex(password));
|
|
|
- user = userDAO.save(user);
|
|
|
|
|
|
|
+ userMapper.updateById(user);
|
|
|
if (reset) {
|
|
if (reset) {
|
|
|
mailService.send( "您的新密码为"+password,"密码变更提醒 | SEECODER",user.getEmail());
|
|
mailService.send( "您的新密码为"+password,"密码变更提醒 | SEECODER",user.getEmail());
|
|
|
}
|
|
}
|