UserServiceImpl.java 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package com.nju.edu.gitlab.service.impl;
  2. import com.nju.edu.gitlab.service.api.UserService;
  3. import com.nju.edu.gitlab.service.vo.GitlabUser;
  4. import com.nju.edu.gitlab.util.ApplicationProperties;
  5. import com.nju.edu.gitlab.util.Tools;
  6. import com.nju.edu.gitlab.web.dto.ChangePasswordDTO;
  7. import com.nju.edu.gitlab.web.dto.GitlabUserDTO;
  8. import org.gitlab4j.api.GitLabApi;
  9. import org.gitlab4j.api.GitLabApiException;
  10. import org.gitlab4j.api.models.*;
  11. import org.gitlab4j.api.models.ImpersonationToken.Scope;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.beans.factory.annotation.Value;
  14. import org.springframework.stereotype.Component;
  15. @Component
  16. public class UserServiceImpl implements UserService {
  17. private final GitLabApi gitlabApi;
  18. private final String gitlabHost;
  19. private final String token;
  20. @Autowired
  21. public UserServiceImpl(ApplicationProperties properties) {
  22. this.gitlabHost=properties.getHost();
  23. this.token= properties.getToken();
  24. this.gitlabApi = new GitLabApi(this.gitlabHost, this.token);
  25. }
  26. @Override
  27. public synchronized GitlabUser createGitlabUser(GitlabUserDTO gitlabUserDTO) throws GitLabApiException {
  28. String username=gitlabUserDTO.getUsername();
  29. String password=gitlabUserDTO.getPassword();
  30. String email= gitlabUserDTO.getEmail();
  31. User user=new User();
  32. user.setName(username);
  33. user.setUsername(username);
  34. user.setEmail(email);
  35. user.setSkipConfirmation(true);
  36. user= gitlabApi.getUserApi().createUser(user, password, true);
  37. GitlabUser gitlabUser=new GitlabUser();
  38. gitlabUser.setUserId(user.getId());
  39. gitlabUser.setNamespaceId(fetchNameSpaceId(username));
  40. gitlabUser.setToken(createToken(user.getId()));
  41. return gitlabUser;
  42. }
  43. @Override
  44. public synchronized boolean changePassword(ChangePasswordDTO changePasswordDTO) throws GitLabApiException {
  45. int userId=changePasswordDTO.getUserId();
  46. String password=changePasswordDTO.getPassword();
  47. User user=gitlabApi.getUserApi().getUser(userId);
  48. gitlabApi.getUserApi().updateUser(user,password);
  49. return true;
  50. }
  51. @Override
  52. public synchronized boolean deleteUser(int userId) throws GitLabApiException {
  53. gitlabApi.getUserApi().deleteUser(userId);
  54. return true;
  55. }
  56. private String createToken(int userId) throws GitLabApiException {
  57. Scope[] scopes = new Scope[]{Scope.API};
  58. ImpersonationToken token;
  59. token = gitlabApi.getUserApi().createImpersonationToken(userId, "AES-" + Tools.randomUUID(), null, scopes);
  60. return token.getToken();
  61. }
  62. private Integer fetchNameSpaceId(String username) throws GitLabApiException {
  63. Namespace namespace;
  64. namespace = gitlabApi.getNamespaceApi().findNamespaces(username)
  65. .stream()
  66. .filter(ns -> "user".equals(ns.getKind()))
  67. .reduce((ns1, ns2) -> ns1.getId() > ns2.getId() ? ns1 : ns2)
  68. .orElse(null);
  69. return namespace != null ? namespace.getId() : null;
  70. }
  71. }