Переглянути джерело

添加接口,添加数据库存储GitlabUser与门户User映射

blumonilr 5 роки тому
батько
коміт
1cd67382f3
16 змінених файлів з 289 додано та 43 видалено
  1. 9 3
      seecoder-gitlab-client/src/main/java/com.nju.edu.gitlab/SeecoderGitlabApi.java
  2. 86 11
      seecoder-gitlab-client/src/main/java/com.nju.edu.gitlab/SeecoderGitlabClient.java
  3. 1 0
      seecoder-gitlab-common/src/main/java/com/nju/edu/gitlab/dto/GitlabUserDTO.java
  4. 17 0
      seecoder-gitlab-common/src/main/java/com/nju/edu/gitlab/dto/UpdateMemberDTO.java
  5. 2 1
      seecoder-gitlab-common/src/main/java/com/nju/edu/gitlab/vo/GitlabUserVO.java
  6. 15 0
      seecoder-gitlab-common/src/main/java/com/nju/edu/gitlab/vo/ProjectVO.java
  7. 5 0
      seecoder-gitlab-server/pom.xml
  8. 15 0
      seecoder-gitlab-server/src/main/java/com/nju/edu/gitlab/controller/GitlabProjectController.java
  9. 21 0
      seecoder-gitlab-server/src/main/java/com/nju/edu/gitlab/entity/GitlabUser.java
  10. 10 0
      seecoder-gitlab-server/src/main/java/com/nju/edu/gitlab/repository/GitlabUserRepository.java
  11. 9 2
      seecoder-gitlab-server/src/main/java/com/nju/edu/gitlab/service/api/ProjectService.java
  12. 5 2
      seecoder-gitlab-server/src/main/java/com/nju/edu/gitlab/service/api/UserService.java
  13. 15 2
      seecoder-gitlab-server/src/main/java/com/nju/edu/gitlab/service/impl/GroupServiceImpl.java
  14. 42 15
      seecoder-gitlab-server/src/main/java/com/nju/edu/gitlab/service/impl/ProjectServiceImpl.java
  15. 37 6
      seecoder-gitlab-server/src/main/java/com/nju/edu/gitlab/service/impl/UserServiceImpl.java
  16. 0 1
      seecoder-gitlab-server/src/main/java/com/nju/edu/gitlab/util/ApplicationProperties.java

+ 9 - 3
seecoder-gitlab-client/src/main/java/com.nju.edu.gitlab/SeecoderGitlabApi.java

@@ -1,11 +1,11 @@
 package com.nju.edu.gitlab;
 
-import com.nju.edu.gitlab.dto.*;
 import com.nju.edu.gitlab.dto.commit.CommitActionForm;
 import com.nju.edu.gitlab.dto.member.AccessLevelForm;
 import com.nju.edu.gitlab.dto.project.VisibilityForm;
 import com.nju.edu.gitlab.vo.*;
 import com.nju.edu.gitlab.vo.commit.CommitVO;
+import org.gitlab4j.api.GitLabApiException;
 
 
 import java.util.List;
@@ -13,7 +13,7 @@ import java.util.Set;
 
 public interface SeecoderGitlabApi{
 
-    public GitlabUser createGitlabUser(String username,String password,String email) throws SeecoderGitlabException;
+    public GitlabUserVO createGitlabUser(int userId,String username, String password, String email) throws SeecoderGitlabException;
 
     public boolean changePassword(int userId,String password) throws SeecoderGitlabException;
 
@@ -23,7 +23,7 @@ public interface SeecoderGitlabApi{
 
     public boolean addMember(int groupId, int userId, AccessLevelForm accessLevel) throws SeecoderGitlabException;
 
-    public Integer createProject(String projectName,VisibilityForm visibility) throws SeecoderGitlabException;
+    public ProjectVO createProject(String projectName,VisibilityForm visibility) throws SeecoderGitlabException;
 
     public Integer forkProject(int projectId, int userId) throws SeecoderGitlabException;
 
@@ -44,4 +44,10 @@ public interface SeecoderGitlabApi{
     public List<CommitVO> getAllCommitMessage(int projectId, String commitHash) throws SeecoderGitlabException;
 
     public List<BranchVO> getProjectBranches(int projectId) throws SeecoderGitlabException;
+
+    public boolean addAuthorization(int projectId, int userId, AccessLevelForm accessLevel) throws GitLabApiException, SeecoderGitlabException;
+
+    public List<ProjectVO> getAllProjectsByUserId(int userId) throws SeecoderGitlabException;
+
+    public List<Integer> getJoinedUserByProject(int projectId) throws SeecoderGitlabException;
 }

+ 86 - 11
seecoder-gitlab-client/src/main/java/com.nju.edu.gitlab/SeecoderGitlabClient.java

@@ -10,11 +10,9 @@ import com.nju.edu.gitlab.dto.member.AccessLevelForm;
 import com.nju.edu.gitlab.dto.member.GroupMemberDTO;
 import com.nju.edu.gitlab.dto.project.ProjectDTO;
 import com.nju.edu.gitlab.dto.project.VisibilityForm;
-import com.nju.edu.gitlab.vo.BranchVO;
-import com.nju.edu.gitlab.vo.DiffVO;
-import com.nju.edu.gitlab.vo.GitlabGroup;
-import com.nju.edu.gitlab.vo.GitlabUser;
+import com.nju.edu.gitlab.vo.*;
 import com.nju.edu.gitlab.vo.commit.CommitVO;
+import org.gitlab4j.api.GitLabApiException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import okhttp3.*;
@@ -84,12 +82,13 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
     }
 
     @Override
-    public GitlabUser createGitlabUser(String username, String password, String email) throws SeecoderGitlabException {
+    public GitlabUserVO createGitlabUser(int userId,String username, String password, String email) throws SeecoderGitlabException {
         try {
             ObjectMapper objectMapper = new ObjectMapper();
             Request.Builder builder = new Request.Builder()
                     .addHeader("Authorization", token);
             GitlabUserDTO gitlabUserDTO=GitlabUserDTO.builder()
+                                                    .userId(userId)
                                                     .email(email)
                                                     .password(password)
                                                     .username(username)
@@ -100,8 +99,8 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
             Request request = builder.build();
             final Call call = client.newCall(request);
             Response response = call.execute();
-            GitlabUser gitlabUser = processResponse(objectMapper, response, GitlabUser.class);
-            return gitlabUser;
+            GitlabUserVO gitlabUserVO = processResponse(objectMapper, response, GitlabUserVO.class);
+            return gitlabUserVO;
         } catch (JsonProcessingException e) {
             logger.error("JSON writeValueAsString error", e);
             throw SeecoderGitlabException.JSON_ERROR;
@@ -127,8 +126,7 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
             Request request = builder.build();
             final Call call = client.newCall(request);
             Response response = call.execute();
-            boolean isChanged = processResponse(objectMapper, response, boolean.class);
-            return isChanged;
+            return processResponse(objectMapper, response, boolean.class);
         } catch (JsonProcessingException e) {
             logger.error("JSON writeValueAsString error", e);
             throw SeecoderGitlabException.JSON_ERROR;
@@ -214,7 +212,7 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
     }
 
     @Override
-    public Integer createProject(String projectName, VisibilityForm visibilityForm) throws SeecoderGitlabException {
+    public ProjectVO createProject(String projectName, VisibilityForm visibilityForm) throws SeecoderGitlabException {
         try {
             ObjectMapper objectMapper = new ObjectMapper();
             Request.Builder builder = new Request.Builder()
@@ -230,7 +228,7 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
             Request request = builder.build();
             final Call call = client.newCall(request);
             Response response = call.execute();
-            return processResponse(objectMapper, response, Integer.class);
+            return processResponse(objectMapper, response, ProjectVO.class);
         } catch (JsonProcessingException e) {
             logger.error("JSON writeValueAsString error", e);
             throw SeecoderGitlabException.JSON_ERROR;
@@ -502,4 +500,81 @@ public class SeecoderGitlabClient implements SeecoderGitlabApi{
             throw SeecoderGitlabException.IO_ERROR;
         }
     }
+
+    @Override
+    public boolean addAuthorization(int projectId, int userId, AccessLevelForm accessLevelForm) throws GitLabApiException, SeecoderGitlabException {
+        try {
+            ObjectMapper objectMapper = new ObjectMapper();
+            Request.Builder builder = new Request.Builder()
+                    .addHeader("Authorization", token);
+            AccessLevel accessLevel=AccessLevel.forValue(accessLevelForm.value);
+            UpdateMemberDTO updateMemberDTO=UpdateMemberDTO.builder()
+                    .accessLevel(accessLevel)
+                    .userId(userId)
+                    .projectId(projectId)
+                    .build();
+            builder = builder
+                    .url(host + "/api/project/updateMember")
+                    .post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), objectMapper.writeValueAsString(updateMemberDTO)));
+            Request request = builder.build();
+            final Call call = client.newCall(request);
+            Response response = call.execute();
+            boolean isAdded = processResponse(objectMapper, response, boolean.class);
+            return isAdded;
+        } catch (JsonProcessingException e) {
+            logger.error("JSON writeValueAsString error", e);
+            throw SeecoderGitlabException.JSON_ERROR;
+        } catch (IOException e) {
+            logger.error("IOException", e);
+            throw SeecoderGitlabException.IO_ERROR;
+        }
+    }
+
+    @Override
+    public List<ProjectVO> getAllProjectsByUserId(int userId) throws SeecoderGitlabException {
+        try {
+            ObjectMapper objectMapper = new ObjectMapper();
+            Request.Builder builder = new Request.Builder()
+                    .addHeader("Authorization", token);
+            builder = builder
+                    .url(host + "/api/project/allProject/"+userId)
+                    .get();
+            Request request = builder.build();
+            final Call call = client.newCall(request);
+            Response response = call.execute();
+            List<ProjectVO> projectVOList=new ArrayList<>();
+            projectVOList=processResponse(objectMapper, response, projectVOList.getClass());
+            return projectVOList;
+        } catch (JsonProcessingException e) {
+            logger.error("JSON writeValueAsString error", e);
+            throw SeecoderGitlabException.JSON_ERROR;
+        } catch (IOException e) {
+            logger.error("IOException", e);
+            throw SeecoderGitlabException.IO_ERROR;
+        }
+    }
+
+    @Override
+    public List<Integer> getJoinedUserByProject(int projectId) throws SeecoderGitlabException {
+        try {
+            ObjectMapper objectMapper = new ObjectMapper();
+            Request.Builder builder = new Request.Builder()
+                    .addHeader("Authorization", token);
+            builder = builder
+                    .url(host + "/api/project/projectMembers/"+projectId)
+                    .get();
+            Request request = builder.build();
+            final Call call = client.newCall(request);
+            Response response = call.execute();
+            List<Integer> userList=new ArrayList<>();
+            userList=processResponse(objectMapper, response, userList.getClass());
+            return userList;
+        } catch (JsonProcessingException e) {
+            logger.error("JSON writeValueAsString error", e);
+            throw SeecoderGitlabException.JSON_ERROR;
+        } catch (IOException e) {
+            logger.error("IOException", e);
+            throw SeecoderGitlabException.IO_ERROR;
+        }
+    }
 }

+ 1 - 0
seecoder-gitlab-common/src/main/java/com/nju/edu/gitlab/dto/GitlabUserDTO.java

@@ -10,6 +10,7 @@ import lombok.NoArgsConstructor;
 @NoArgsConstructor
 @AllArgsConstructor
 public class GitlabUserDTO {
+    private int userId;
     private String username;
     private String password;
     private String email;

+ 17 - 0
seecoder-gitlab-common/src/main/java/com/nju/edu/gitlab/dto/UpdateMemberDTO.java

@@ -0,0 +1,17 @@
+package com.nju.edu.gitlab.dto;
+
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import org.gitlab4j.api.models.AccessLevel;
+
+@AllArgsConstructor
+@NoArgsConstructor
+@Data
+@Builder
+public class UpdateMemberDTO {
+    private int projectId;
+    private int userId;
+    private AccessLevel accessLevel;
+}

+ 2 - 1
seecoder-gitlab-common/src/main/java/com/nju/edu/gitlab/vo/GitlabUser.java → seecoder-gitlab-common/src/main/java/com/nju/edu/gitlab/vo/GitlabUserVO.java

@@ -7,9 +7,10 @@ import lombok.NoArgsConstructor;
 @Data
 @NoArgsConstructor
 @AllArgsConstructor
-public class GitlabUser {
+public class GitlabUserVO {
 
     private int userId;
+    private int gitlabUserId;
     private int namespaceId;
     private String token;
 }

+ 15 - 0
seecoder-gitlab-common/src/main/java/com/nju/edu/gitlab/vo/ProjectVO.java

@@ -0,0 +1,15 @@
+package com.nju.edu.gitlab.vo;
+
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@AllArgsConstructor
+@NoArgsConstructor
+@Data
+@Builder
+public class ProjectVO {
+    private Integer projectId;
+    private String webUrl;
+}

+ 5 - 0
seecoder-gitlab-server/pom.xml

@@ -43,6 +43,11 @@
             <groupId>org.thymeleaf.extras</groupId>
             <artifactId>thymeleaf-extras-springsecurity5</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-configuration-processor</artifactId>
+            <optional>true</optional>
+        </dependency>
 
         <dependency>
             <groupId>org.springframework.boot</groupId>

+ 15 - 0
seecoder-gitlab-server/src/main/java/com/nju/edu/gitlab/controller/GitlabProjectController.java

@@ -89,4 +89,19 @@ public class GitlabProjectController {
     public Response getAllBranches(@PathVariable(name = "projectId") Integer projectId) throws GitLabApiException {
         return new ResourceResponse(projectService.getProjectBranches(projectId));
     }
+
+    @PostMapping("/updateMember")
+    public Response updateMember(@RequestBody UpdateMemberDTO updateMemberDTO) throws GitLabApiException{
+        return new ResourceResponse(projectService.addAuthorization(updateMemberDTO));
+    }
+
+    @GetMapping("/allProjects/{userId}")
+    public Response getAllProjectsByUserId(@PathVariable(name = "userId")int userId) throws GitLabApiException{
+        return new ResourceResponse(projectService.getAllProjectByUserId(userId));
+    }
+
+    @GetMapping("/projectMembers/{projectId}")
+    public Response getMembers(@PathVariable(name = "projectId")int projectId) throws GitLabApiException{
+        return new ResourceResponse(projectService.getProjectJoinedUserId(projectId));
+    }
 }

+ 21 - 0
seecoder-gitlab-server/src/main/java/com/nju/edu/gitlab/entity/GitlabUser.java

@@ -0,0 +1,21 @@
+package com.nju.edu.gitlab.entity;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+@Entity
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+public class GitlabUser {
+    @Id
+    private int gitlabUserId;
+
+    private int userId;
+    private int namespaceId;
+    private String token;
+}

+ 10 - 0
seecoder-gitlab-server/src/main/java/com/nju/edu/gitlab/repository/GitlabUserRepository.java

@@ -0,0 +1,10 @@
+package com.nju.edu.gitlab.repository;
+
+import com.nju.edu.gitlab.entity.GitlabUser;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.stereotype.Repository;
+
+@Repository
+public interface GitlabUserRepository extends JpaRepository<GitlabUser,Integer> {
+    public GitlabUser findByUserId(int userId);
+}

+ 9 - 2
seecoder-gitlab-server/src/main/java/com/nju/edu/gitlab/service/api/ProjectService.java

@@ -4,8 +4,9 @@ import com.nju.edu.gitlab.dto.*;
 import com.nju.edu.gitlab.dto.commit.CommitDTO;
 import com.nju.edu.gitlab.dto.project.ProjectDTO;
 import com.nju.edu.gitlab.vo.BranchVO;
-import com.nju.edu.gitlab.vo.commit.CommitVO;
 import com.nju.edu.gitlab.vo.DiffVO;
+import com.nju.edu.gitlab.vo.ProjectVO;
+import com.nju.edu.gitlab.vo.commit.CommitVO;
 import org.gitlab4j.api.GitLabApiException;
 
 import java.io.IOException;
@@ -15,7 +16,7 @@ import java.util.Set;
 
 public interface ProjectService {
 
-    public Integer createProject(ProjectDTO projectDTO) throws GitLabApiException;
+    public ProjectVO createProject(ProjectDTO projectDTO) throws GitLabApiException;
 
     public Integer forkProject(UserForkDTO userForkDTO) throws GitLabApiException;
 
@@ -36,4 +37,10 @@ public interface ProjectService {
     public List<CommitVO> getAllCommitMessage(int projectId,String commitHash) throws GitLabApiException;
 
     public List<BranchVO> getProjectBranches(int projectId) throws GitLabApiException;
+
+    public boolean addAuthorization(UpdateMemberDTO updateMemberDTO) throws GitLabApiException;
+
+    public List<ProjectVO> getAllProjectByUserId(int userId) throws GitLabApiException;
+
+    public List<Integer> getProjectJoinedUserId(int projectId) throws GitLabApiException;
 }

+ 5 - 2
seecoder-gitlab-server/src/main/java/com/nju/edu/gitlab/service/api/UserService.java

@@ -1,15 +1,18 @@
 package com.nju.edu.gitlab.service.api;
 
-import com.nju.edu.gitlab.vo.GitlabUser;
+import com.nju.edu.gitlab.vo.GitlabUserVO;
 import com.nju.edu.gitlab.dto.ChangePasswordDTO;
 import com.nju.edu.gitlab.dto.GitlabUserDTO;
 import org.gitlab4j.api.GitLabApiException;
+import org.gitlab4j.api.models.User;
 
 public interface UserService {
 
-    public GitlabUser createGitlabUser(GitlabUserDTO gitlabUserDTO) throws GitLabApiException;
+    public GitlabUserVO createGitlabUser(GitlabUserDTO gitlabUserDTO) throws GitLabApiException;
 
     public boolean changePassword(ChangePasswordDTO changePasswordDTO) throws GitLabApiException;
 
     public boolean deleteUser(int userId) throws GitLabApiException;
+
+    public User findByUsername(String username) throws GitLabApiException;
 }

+ 15 - 2
seecoder-gitlab-server/src/main/java/com/nju/edu/gitlab/service/impl/GroupServiceImpl.java

@@ -1,5 +1,7 @@
 package com.nju.edu.gitlab.service.impl;
 
+import com.nju.edu.gitlab.entity.GitlabUser;
+import com.nju.edu.gitlab.repository.GitlabUserRepository;
 import com.nju.edu.gitlab.service.api.GroupService;
 import com.nju.edu.gitlab.vo.GitlabGroup;
 import com.nju.edu.gitlab.util.ApplicationProperties;
@@ -15,12 +17,14 @@ import org.springframework.stereotype.Component;
 @Component
 public class GroupServiceImpl implements GroupService {
     private final GitLabApi gitlabApi;
+    private final GitlabUserRepository gitlabUserRepository;
 
     private final String gitlabHost;
     private final String token;
 
     @Autowired
-    public GroupServiceImpl(ApplicationProperties properties) {
+    public GroupServiceImpl(GitlabUserRepository gitlabUserRepository, ApplicationProperties properties) {
+        this.gitlabUserRepository = gitlabUserRepository;
         this.gitlabHost=properties.getHost();
         this.token= properties.getToken();
         this.gitlabApi = new GitLabApi(this.gitlabHost, this.token);
@@ -39,11 +43,13 @@ public class GroupServiceImpl implements GroupService {
     public synchronized boolean addMember(GroupMemberDTO groupMemberDTO) throws GitLabApiException {
         int groupId=groupMemberDTO.getGroupId();
         int userId=groupMemberDTO.getUserId();
+        GitlabUser gitlabUser = getGitlabUser(userId);
         AccessLevel accessLevel=groupMemberDTO.getAccessLevel();
-        gitlabApi.getGroupApi().addMember(groupId,userId,accessLevel);
+        gitlabApi.getGroupApi().addMember(groupId,gitlabUser.getGitlabUserId(),accessLevel);
         return true;
     }
 
+
     private int fetchGroupNameSpace(String groupName) throws GitLabApiException {
         Namespace namespace;
         namespace = gitlabApi.getNamespaceApi().findNamespaces(groupName)
@@ -53,4 +59,11 @@ public class GroupServiceImpl implements GroupService {
                 .orElse(null);
         return namespace != null ? namespace.getId() : null;
     }
+
+    private GitlabUser getGitlabUser(int userId) throws GitLabApiException {
+        if(gitlabUserRepository.existsById(userId))
+            return gitlabUserRepository.getOne(userId);
+        else
+            throw new GitLabApiException("user not exists");
+    }
 }

+ 42 - 15
seecoder-gitlab-server/src/main/java/com/nju/edu/gitlab/service/impl/ProjectServiceImpl.java

@@ -2,10 +2,13 @@ package com.nju.edu.gitlab.service.impl;
 
 import com.nju.edu.gitlab.dto.commit.CommitDTO;
 import com.nju.edu.gitlab.dto.project.ProjectDTO;
+import com.nju.edu.gitlab.entity.GitlabUser;
+import com.nju.edu.gitlab.repository.GitlabUserRepository;
 import com.nju.edu.gitlab.service.api.ProjectService;
 import com.nju.edu.gitlab.util.ApplicationProperties;
 import com.nju.edu.gitlab.dto.*;
 import com.nju.edu.gitlab.vo.BranchVO;
+import com.nju.edu.gitlab.vo.ProjectVO;
 import com.nju.edu.gitlab.vo.commit.CommitStatsVO;
 import com.nju.edu.gitlab.vo.commit.CommitVO;
 import com.nju.edu.gitlab.vo.DiffVO;
@@ -22,34 +25,38 @@ import java.util.stream.Collectors;
 @Component
 public class ProjectServiceImpl implements ProjectService {
     private final GitLabApi gitlabApi;
+    private final GitlabUserRepository gitlabUserRepository;
 
     private final String gitlabHost;
     private final String token;
 
     @Autowired
-    public ProjectServiceImpl(ApplicationProperties properties) {
+    public ProjectServiceImpl(GitlabUserRepository gitlabUserRepository, ApplicationProperties properties) {
+        this.gitlabUserRepository = gitlabUserRepository;
         this.gitlabHost=properties.getHost();
         this.token= properties.getToken();
         this.gitlabApi = new GitLabApi(this.gitlabHost, this.token);
     }
     @Override
-    public Integer createProject(ProjectDTO projectDTO) throws GitLabApiException {
+    public ProjectVO createProject(ProjectDTO projectDTO) throws GitLabApiException {
         String projectName=projectDTO.getProjectName();
         Visibility visibility=projectDTO.getVisibility();
         Project project=new Project();
         project.setName(projectName);
         project.setVisibility(visibility);
         project=gitlabApi.getProjectApi().createProject(project);
-        return project.getId();
+        return ProjectVO.builder()
+                .projectId(project.getId())
+                .webUrl(project.getWebUrl())
+                .build();
     }
 
     @Override
     public Integer forkProject(UserForkDTO userForkDTO) throws GitLabApiException {
         int userId=userForkDTO.getUserId();
         int projectId=userForkDTO.getProjectId();
-        Integer namespaceId=fetchNameSpaceId(gitlabApi.getUserApi().getUser(userId).getUsername());
-        if (namespaceId == null)
-            throw new NullPointerException();
+        GitlabUser gitlabUser = getGitlabUser(userId);
+        Integer namespaceId=gitlabUser.getNamespaceId();
         Project project;
         try{
             gitlabApi.setSudoAsId(userId);
@@ -215,6 +222,30 @@ public class ProjectServiceImpl implements ProjectService {
                 .collect(Collectors.toList());
     }
 
+    @Override
+    public boolean addAuthorization(UpdateMemberDTO updateMemberDTO) throws GitLabApiException {
+        int projectId = updateMemberDTO.getProjectId();
+        int userId = updateMemberDTO.getUserId();
+        GitlabUser gitlabUser = getGitlabUser(userId);
+        AccessLevel accessLevel = updateMemberDTO.getAccessLevel();
+        gitlabApi.getProjectApi().updateMember(projectId,gitlabUser.getGitlabUserId(),accessLevel);
+        return true;
+    }
+
+    @Override
+    public List<ProjectVO> getAllProjectByUserId(int userId) throws GitLabApiException {
+        GitlabUser gitlabUser = getGitlabUser(userId);
+        gitlabApi.setSudoAsId(gitlabUser.getGitlabUserId());
+        List<ProjectVO> projectVOS = gitlabApi.getProjectApi().getMemberProjects().stream().map(project->ProjectVO.builder().projectId(project.getId()).webUrl(project.getWebUrl()).build()).collect(Collectors.toList());
+        gitlabApi.unsudo();
+        return projectVOS;
+    }
+
+    @Override
+    public List<Integer> getProjectJoinedUserId(int projectId) throws GitLabApiException {
+        return gitlabApi.getProjectApi().getMembers(projectId).stream().map(Member::getId).collect(Collectors.toList());
+    }
+
     private BranchVO castBranchToVO(Branch branch) {
         return BranchVO.builder()
                 .commit(castCommitToVO(branch.getCommit(),branch.getName()))
@@ -226,14 +257,10 @@ public class ProjectServiceImpl implements ProjectService {
                 .build();
     }
 
-
-    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;
+    private GitlabUser getGitlabUser(int userId) throws GitLabApiException {
+        if(gitlabUserRepository.existsById(userId))
+            return gitlabUserRepository.getOne(userId);
+        else
+            throw new GitLabApiException("user not exists");
     }
 }

+ 37 - 6
seecoder-gitlab-server/src/main/java/com/nju/edu/gitlab/service/impl/UserServiceImpl.java

@@ -1,7 +1,9 @@
 package com.nju.edu.gitlab.service.impl;
 
+import com.nju.edu.gitlab.entity.GitlabUser;
+import com.nju.edu.gitlab.repository.GitlabUserRepository;
 import com.nju.edu.gitlab.service.api.UserService;
-import com.nju.edu.gitlab.vo.GitlabUser;
+import com.nju.edu.gitlab.vo.GitlabUserVO;
 import com.nju.edu.gitlab.util.ApplicationProperties;
 import com.nju.edu.gitlab.util.Tools;
 import com.nju.edu.gitlab.dto.ChangePasswordDTO;
@@ -13,22 +15,28 @@ import org.gitlab4j.api.models.ImpersonationToken.Scope;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+import javax.persistence.EntityNotFoundException;
+
 @Component
 public class UserServiceImpl implements UserService {
     private final GitLabApi gitlabApi;
+    private final GitlabUserRepository gitlabUserRepository;
 
     private final String gitlabHost;
     private final String token;
 
     @Autowired
-    public UserServiceImpl(ApplicationProperties properties) {
+    public UserServiceImpl(GitlabUserRepository gitlabUserRepository, ApplicationProperties properties) {
+        this.gitlabUserRepository = gitlabUserRepository;
         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 {
+    public synchronized GitlabUserVO createGitlabUser(GitlabUserDTO gitlabUserDTO) throws GitLabApiException {
+        int userId = gitlabUserDTO.getUserId();
+        check(userId);
         String username=gitlabUserDTO.getUsername();
         String password=gitlabUserDTO.getPassword();
         String email= gitlabUserDTO.getEmail();
@@ -38,11 +46,28 @@ public class UserServiceImpl implements UserService {
         user.setEmail(email);
         user.setSkipConfirmation(true);
         user= gitlabApi.getUserApi().createUser(user, password, 1000);
-        GitlabUser gitlabUser=new GitlabUser();
-        gitlabUser.setUserId(user.getId());
+        GitlabUser gitlabUser = new GitlabUser();
+        gitlabUser.setUserId(userId);
+        gitlabUser.setGitlabUserId(user.getId());
         gitlabUser.setNamespaceId(fetchNameSpaceId(username));
         gitlabUser.setToken(createToken(user.getId()));
-        return gitlabUser;
+
+        gitlabUser=gitlabUserRepository.save(gitlabUser);
+
+        if (gitlabUser!=null) {
+            GitlabUserVO gitlabUserVO = new GitlabUserVO();
+            gitlabUserVO.setUserId(userId);
+            gitlabUserVO.setGitlabUserId(gitlabUser.getGitlabUserId());
+            gitlabUserVO.setNamespaceId(gitlabUser.getNamespaceId());
+            gitlabUserVO.setToken(gitlabUser.getToken());
+            return gitlabUserVO;
+        }
+        return null;
+    }
+
+    private void check(int userId) throws GitLabApiException {
+        if (gitlabUserRepository.existsById(userId))
+            throw new GitLabApiException("user already exists");
     }
 
     @Override
@@ -60,6 +85,12 @@ public class UserServiceImpl implements UserService {
         return true;
     }
 
+    @Override
+    public User findByUsername(String username) throws GitLabApiException {
+        System.out.println(gitlabApi.getSudoAsId());
+        return gitlabApi.getUserApi().getUser(username);
+    }
+
     private String createToken(int userId) throws GitLabApiException {
         Scope[] scopes = new Scope[]{Scope.API};
         ImpersonationToken token;

+ 0 - 1
seecoder-gitlab-server/src/main/java/com/nju/edu/gitlab/util/ApplicationProperties.java

@@ -3,7 +3,6 @@ package com.nju.edu.gitlab.util;
 import lombok.Data;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.context.annotation.Configuration;
-import org.springframework.validation.annotation.Validated;
 
 @Data
 @Configuration