| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- package com.nju.edu.gitlab.service.impl;
- import com.nju.edu.gitlab.service.api.UserService;
- import com.nju.edu.gitlab.service.vo.GitlabUser;
- import com.nju.edu.gitlab.util.ApplicationProperties;
- import com.nju.edu.gitlab.util.Tools;
- import com.nju.edu.gitlab.web.dto.ChangePasswordDTO;
- import com.nju.edu.gitlab.web.dto.GitlabUserDTO;
- import org.gitlab4j.api.GitLabApi;
- import org.gitlab4j.api.GitLabApiException;
- import org.gitlab4j.api.models.*;
- import org.gitlab4j.api.models.ImpersonationToken.Scope;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
- @Component
- public class UserServiceImpl implements UserService {
- private final GitLabApi gitlabApi;
- private final String gitlabHost;
- private final String token;
- @Autowired
- public UserServiceImpl(ApplicationProperties properties) {
- this.gitlabHost=properties.getHost();
- this.token= properties.getToken();
- this.gitlabApi = new GitLabApi(this.gitlabHost, this.token);
- }
- @Override
- public synchronized GitlabUser createGitlabUser(GitlabUserDTO gitlabUserDTO) throws GitLabApiException {
- String username=gitlabUserDTO.getUsername();
- String password=gitlabUserDTO.getPassword();
- String email= gitlabUserDTO.getEmail();
- User user=new User();
- user.setName(username);
- user.setUsername(username);
- user.setEmail(email);
- user.setSkipConfirmation(true);
- user= gitlabApi.getUserApi().createUser(user, password, true);
- GitlabUser gitlabUser=new GitlabUser();
- gitlabUser.setUserId(user.getId());
- gitlabUser.setNamespaceId(fetchNameSpaceId(username));
- gitlabUser.setToken(createToken(user.getId()));
- return gitlabUser;
- }
- @Override
- public synchronized boolean changePassword(ChangePasswordDTO changePasswordDTO) throws GitLabApiException {
- int userId=changePasswordDTO.getUserId();
- String password=changePasswordDTO.getPassword();
- User user=gitlabApi.getUserApi().getUser(userId);
- gitlabApi.getUserApi().updateUser(user,password);
- return true;
- }
- @Override
- public synchronized boolean deleteUser(int userId) throws GitLabApiException {
- gitlabApi.getUserApi().deleteUser(userId);
- return true;
- }
- private String createToken(int userId) throws GitLabApiException {
- Scope[] scopes = new Scope[]{Scope.API};
- ImpersonationToken token;
- token = gitlabApi.getUserApi().createImpersonationToken(userId, "AES-" + Tools.randomUUID(), null, scopes);
- return token.getToken();
- }
- private Integer fetchNameSpaceId(String username) throws GitLabApiException {
- Namespace namespace;
- namespace = gitlabApi.getNamespaceApi().findNamespaces(username)
- .stream()
- .filter(ns -> "user".equals(ns.getKind()))
- .reduce((ns1, ns2) -> ns1.getId() > ns2.getId() ? ns1 : ns2)
- .orElse(null);
- return namespace != null ? namespace.getId() : null;
- }
- }
|