Parcourir la source

user与group service

blumonilr il y a 5 ans
Parent
commit
d759148e30

+ 12 - 0
src/main/java/com/nju/edu/gitlab/service/api/GroupService.java

@@ -0,0 +1,12 @@
+package com.nju.edu.gitlab.service.api;
+
+import com.nju.edu.gitlab.service.vo.GitlabGroup;
+import com.nju.edu.gitlab.web.dto.GroupMemberDTO;
+import org.gitlab4j.api.GitLabApiException;
+
+public interface GroupService {
+
+    public GitlabGroup createGroup(String groupName) throws GitLabApiException;
+
+    public boolean addMember(GroupMemberDTO groupMemberDTO) throws GitLabApiException;
+}

+ 16 - 0
src/main/java/com/nju/edu/gitlab/service/api/UserService.java

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

+ 55 - 0
src/main/java/com/nju/edu/gitlab/service/impl/GroupServiceImpl.java

@@ -0,0 +1,55 @@
+package com.nju.edu.gitlab.service.impl;
+
+import com.nju.edu.gitlab.service.api.GroupService;
+import com.nju.edu.gitlab.service.vo.GitlabGroup;
+import com.nju.edu.gitlab.web.dto.GroupMemberDTO;
+import org.gitlab4j.api.GitLabApi;
+import org.gitlab4j.api.GitLabApiException;
+import org.gitlab4j.api.models.AccessLevel;
+import org.gitlab4j.api.models.Group;
+import org.gitlab4j.api.models.Namespace;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+@Component
+public class GroupServiceImpl implements GroupService {
+    private final GitLabApi gitlabApi;
+
+    @Value("${gitlab.host}")
+    private String gitlabHost;
+    @Value("${gitlab.token}")
+    private String token;
+
+    @Autowired
+    public GroupServiceImpl() {
+        this.gitlabApi = new GitLabApi(gitlabHost,token);
+    }
+    @Override
+    public synchronized GitlabGroup createGroup(String groupName) throws GitLabApiException {
+        Group group=gitlabApi.getGroupApi().addGroup(groupName,groupName);
+        GitlabGroup gitlabGroup=new GitlabGroup();
+        gitlabGroup.setGroupId(group.getId());
+        gitlabGroup.setNamespaceId(fetchGroupNameSpace(groupName));
+        return gitlabGroup;
+    }
+
+    @Override
+    public synchronized boolean addMember(GroupMemberDTO groupMemberDTO) throws GitLabApiException {
+        int groupId=groupMemberDTO.getGroupId();
+        int userId=groupMemberDTO.getUserId();
+        AccessLevel accessLevel=groupMemberDTO.getAccessLevel();
+        gitlabApi.getGroupApi().addMember(groupId,userId,accessLevel);
+        return true;
+    }
+
+    private int fetchGroupNameSpace(String groupName) throws GitLabApiException {
+        Namespace namespace;
+        namespace = gitlabApi.getNamespaceApi().findNamespaces(groupName)
+                .stream()
+                .filter(ns -> "group".equals(ns.getKind()))
+                .reduce((ns1, ns2) -> ns1.getId() > ns2.getId() ? ns1 : ns2)
+                .orElse(null);
+        return namespace != null ? namespace.getId() : null;
+    }
+}

+ 79 - 0
src/main/java/com/nju/edu/gitlab/service/impl/UserServiceImpl.java

@@ -0,0 +1,79 @@
+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.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;
+
+    @Value("${gitlab.host}")
+    private String gitlabHost;
+    @Value("${gitlab.token}")
+    private String token;
+
+    @Autowired
+    public UserServiceImpl() {
+        this.gitlabApi = new GitLabApi(gitlabHost,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,1000);
+        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().modifyUser(user,password,user.getProjectsLimit());
+        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;
+    }
+}

+ 14 - 0
src/main/java/com/nju/edu/gitlab/service/vo/GitlabGroup.java

@@ -0,0 +1,14 @@
+package com.nju.edu.gitlab.service.vo;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+public class GitlabGroup {
+
+    private int groupId;
+    private int namespaceId;
+}

+ 15 - 0
src/main/java/com/nju/edu/gitlab/service/vo/GitlabUser.java

@@ -0,0 +1,15 @@
+package com.nju.edu.gitlab.service.vo;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+public class GitlabUser {
+
+    private int userId;
+    private int namespaceId;
+    private String token;
+}

+ 10 - 0
src/main/java/com/nju/edu/gitlab/util/Tools.java

@@ -0,0 +1,10 @@
+package com.nju.edu.gitlab.util;
+
+import java.util.UUID;
+
+public class Tools {
+
+    public static String randomUUID(){
+        return UUID.randomUUID().toString().replace("-","");
+    }
+}

+ 8 - 1
src/main/resources/application.yml

@@ -1 +1,8 @@
-
+server:
+  port: 8080
+gitlab:
+  host: http://106.14.170.111:8082
+  username: root
+  token: 4z9a5Sykdrw7z7x8fCs6
+  administrator: root
+  email: noreply@mooc.com