Search in sources :

Example 1 with Group

use of org.gitlab4j.api.models.Group in project OpenUnison by TremoloSecurity.

the class GitlabUserProvider method createUser.

@Override
public void createUser(User user, Set<String> attributes, Map<String, Object> request) throws ProvisioningException {
    int approvalID = 0;
    if (request.containsKey("APPROVAL_ID")) {
        approvalID = (Integer) request.get("APPROVAL_ID");
    }
    Workflow workflow = (Workflow) request.get("WORKFLOW");
    org.gitlab4j.api.models.User newUser = new org.gitlab4j.api.models.User();
    newUser.setUsername(user.getUserID());
    for (String attrName : attributes) {
        Attribute attr = user.getAttribs().get(attrName);
        if (attr != null) {
            try {
                this.beanUtils.setProperty(newUser, attrName, attr.getValues().get(0));
            } catch (IllegalAccessException | InvocationTargetException e) {
                throw new ProvisioningException("Could not set " + attrName + " for " + user.getUserID(), e);
            }
        }
    }
    try {
        this.userApi.createUser(newUser, new GenPasswd(50).getPassword(), false);
    } catch (GitLabApiException e) {
        throw new ProvisioningException("Could not create user", e);
    }
    newUser = this.findUserByName(user.getUserID());
    int numTries = 0;
    while (newUser == null) {
        if (numTries > 10) {
            throw new ProvisioningException("User " + user.getUserID() + " never created");
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        newUser = this.findUserByName(user.getUserID());
        numTries++;
    }
    this.cfgMgr.getProvisioningEngine().logAction(this.name, true, ActionType.Add, approvalID, workflow, "id", newUser.getId().toString());
    for (String attrName : attributes) {
        Attribute attr = user.getAttribs().get(attrName);
        if (attr != null) {
            this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Add, approvalID, workflow, attrName, attr.getValues().get(0));
        }
    }
    List<GitlabFedIdentity> ids = (List<GitlabFedIdentity>) request.get(GitlabUserProvider.GITLAB_IDENTITIES);
    if (ids != null) {
        ArrayList<Header> defheaders = new ArrayList<Header>();
        defheaders.add(new BasicHeader("Private-Token", this.token));
        BasicHttpClientConnectionManager bhcm = new BasicHttpClientConnectionManager(cfgMgr.getHttpClientSocketRegistry());
        RequestConfig rc = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).setRedirectsEnabled(false).build();
        CloseableHttpClient http = HttpClients.custom().setConnectionManager(bhcm).setDefaultHeaders(defheaders).setDefaultRequestConfig(rc).build();
        try {
            for (GitlabFedIdentity id : ids) {
                HttpPut getmembers = new HttpPut(new StringBuilder().append(this.url).append("/api/v4/users/").append(newUser.getId()).append("?provider=").append(id.getProvider()).append("&extern_uid=").append(URLEncoder.encode(user.getUserID(), "UTF-8")).toString());
                CloseableHttpResponse resp = http.execute(getmembers);
                if (resp.getStatusLine().getStatusCode() != 200) {
                    throw new IOException("Invalid response " + resp.getStatusLine().getStatusCode());
                }
                this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Add, approvalID, workflow, "identity-provider", id.getProvider());
                this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Add, approvalID, workflow, "identity-externid", id.getExternalUid());
            }
        } catch (IOException e) {
            throw new ProvisioningException("Could not set identity", e);
        } finally {
            try {
                http.close();
            } catch (IOException e) {
            }
            bhcm.close();
        }
    }
    HashMap<String, Integer> groupmap = (HashMap<String, Integer>) request.get(GitlabUserProvider.GITLAB_GROUP_ENTITLEMENTS);
    if (groupmap == null) {
        groupmap = new HashMap<String, Integer>();
    }
    for (String group : user.getGroups()) {
        try {
            Group groupObj = this.findGroupByName(group);
            if (groupObj == null) {
                logger.warn("Group " + group + " does not exist");
            } else {
                int accessLevel = AccessLevel.DEVELOPER.ordinal();
                if (groupmap.containsKey(group)) {
                    accessLevel = groupmap.get(group);
                }
                this.groupApi.addMember(groupObj.getId(), newUser.getId(), accessLevel);
                this.cfgMgr.getProvisioningEngine().logAction(this.name, false, ActionType.Add, approvalID, workflow, "group", group);
            }
        } catch (GitLabApiException e) {
            throw new ProvisioningException("Could not find group " + group, e);
        }
    }
}
Also used : Group(org.gitlab4j.api.models.Group) UserStoreProviderWithAddGroup(com.tremolosecurity.provisioning.core.UserStoreProviderWithAddGroup) User(com.tremolosecurity.provisioning.core.User) Attribute(com.tremolosecurity.saml.Attribute) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HttpPut(org.apache.http.client.methods.HttpPut) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ArrayList(java.util.ArrayList) List(java.util.List) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) GenPasswd(com.tremolosecurity.provisioning.util.GenPasswd) RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Workflow(com.tremolosecurity.provisioning.core.Workflow) GitLabApiException(org.gitlab4j.api.GitLabApiException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) BasicHeader(org.apache.http.message.BasicHeader)

Example 2 with Group

use of org.gitlab4j.api.models.Group in project OpenUnison by TremoloSecurity.

the class GitlabUserProvider method addGroup.

@Override
public void addGroup(String name, Map<String, String> additionalAttributes, User user, Map<String, Object> request) throws ProvisioningException {
    if (this.isGroupExists(name, null, request)) {
        return;
    }
    int approvalID = 0;
    if (request.containsKey("APPROVAL_ID")) {
        approvalID = (Integer) request.get("APPROVAL_ID");
    }
    Workflow workflow = (Workflow) request.get("WORKFLOW");
    Group groupToCreate = new Group();
    groupToCreate.setName(name);
    groupToCreate.setPath(name);
    for (String prop : additionalAttributes.keySet()) {
        try {
            this.beanUtils.setProperty(groupToCreate, prop, additionalAttributes.get(prop));
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new ProvisioningException("Could not set properties", e);
        }
    }
    try {
        this.groupApi.addGroup(groupToCreate);
    } catch (GitLabApiException e) {
        throw new ProvisioningException("Could not create group " + name, e);
    }
    this.cfgMgr.getProvisioningEngine().logAction(this.name, true, ActionType.Add, approvalID, workflow, "group-object", name);
}
Also used : Group(org.gitlab4j.api.models.Group) UserStoreProviderWithAddGroup(com.tremolosecurity.provisioning.core.UserStoreProviderWithAddGroup) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) Workflow(com.tremolosecurity.provisioning.core.Workflow) GitLabApiException(org.gitlab4j.api.GitLabApiException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 3 with Group

use of org.gitlab4j.api.models.Group in project catma by forTEXT.

the class GitlabManagerRestricted method getResourcePermissions.

private Map<String, AccessLevel> getResourcePermissions(Integer groupId) throws GitLabApiException {
    Map<String, AccessLevel> resultMap = Maps.newHashMap();
    ProjectApi projectApi = new ProjectApi(restrictedGitLabApi);
    logger.info("Loading project permissions");
    List<Project> resourceAndContainerProjects = projectApi.getProjects(new ProjectFilter().withMembership(true));
    logger.info(String.format("Filtering %1$d resources on group #%2$d", resourceAndContainerProjects.size(), groupId));
    Set<Project> filteredOnGroupProjects = resourceAndContainerProjects.stream().filter(p -> p.getNamespace().getId().equals(groupId)).collect(Collectors.toSet());
    logger.info(String.format("Updating accesslevel registry for %1$d resources", filteredOnGroupProjects.size()));
    for (Project p : filteredOnGroupProjects) {
        Permissions permission = p.getPermissions();
        if (permission.getGroupAccess() != null) {
            resultMap.put(p.getName(), permission.getGroupAccess().getAccessLevel());
        }
        if (permission.getProjectAccess() != null && (!resultMap.containsKey(p.getName()) || resultMap.get(p.getName()).value.intValue() < permission.getProjectAccess().getAccessLevel().value.intValue())) {
            resultMap.put(p.getName(), permission.getProjectAccess().getAccessLevel());
        }
    }
    return resultMap;
}
Also used : NotesApi(org.gitlab4j.api.NotesApi) JsonObject(com.google.gson.JsonObject) Reply(de.catma.document.comment.Reply) AccessLevel(org.gitlab4j.api.models.AccessLevel) ProjectFilter(org.gitlab4j.api.models.ProjectFilter) StringUtils(org.apache.commons.lang3.StringUtils) Author(org.gitlab4j.api.models.Author) IssuesApi(org.gitlab4j.api.IssuesApi) ChangeUserAttributeEvent(de.catma.ui.events.ChangeUserAttributeEvent) Map(java.util.Map) Group(org.gitlab4j.api.models.Group) IssueState(org.gitlab4j.api.Constants.IssueState) GroupApi(org.gitlab4j.api.GroupApi) Visibility(org.gitlab4j.api.models.Visibility) CATMAPropertyKey(de.catma.properties.CATMAPropertyKey) IGitUserInformation(de.catma.repository.git.interfaces.IGitUserInformation) GitMember(de.catma.repository.git.GitMember) Pager(org.gitlab4j.api.Pager) GitlabUtils(de.catma.repository.git.GitlabUtils) Set(java.util.Set) Logger(java.util.logging.Logger) SerializationHelper(de.catma.repository.git.serialization.SerializationHelper) Collectors(java.util.stream.Collectors) ProjectReference(de.catma.project.ProjectReference) Objects(java.util.Objects) List(java.util.List) ProjectApi(org.gitlab4j.api.ProjectApi) Optional(java.util.Optional) GitLabApiException(org.gitlab4j.api.GitLabApiException) Status(org.gitlab4j.api.models.ImportStatus.Status) CacheBuilder(com.google.common.cache.CacheBuilder) GitLabApi(org.gitlab4j.api.GitLabApi) RBACPermission(de.catma.rbac.RBACPermission) Permissions(org.gitlab4j.api.models.Permissions) GroupFilter(org.gitlab4j.api.models.GroupFilter) Namespace(org.gitlab4j.api.models.Namespace) HashMap(java.util.HashMap) RBACRole(de.catma.rbac.RBACRole) JsonParser(com.google.gson.JsonParser) User(de.catma.user.User) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) EventBus(com.google.common.eventbus.EventBus) IRemoteGitManagerRestricted(de.catma.repository.git.interfaces.IRemoteGitManagerRestricted) Comment(de.catma.document.comment.Comment) ForkStatus(de.catma.project.ForkStatus) GitProjectManager(de.catma.repository.git.GitProjectManager) Note(org.gitlab4j.api.models.Note) IDGenerator(de.catma.util.IDGenerator) Subscribe(com.google.common.eventbus.Subscribe) CreateRepositoryResponse(de.catma.repository.git.CreateRepositoryResponse) Issue(org.gitlab4j.api.models.Issue) IOException(java.io.IOException) Project(org.gitlab4j.api.models.Project) Maps(com.google.common.collect.Maps) IssueFilter(org.gitlab4j.api.models.IssueFilter) TimeUnit(java.util.concurrent.TimeUnit) Member(org.gitlab4j.api.models.Member) Cache(com.google.common.cache.Cache) Collections(java.util.Collections) BackgroundService(de.catma.backgroundservice.BackgroundService) GitUser(de.catma.repository.git.GitUser) Project(org.gitlab4j.api.models.Project) ProjectFilter(org.gitlab4j.api.models.ProjectFilter) ProjectApi(org.gitlab4j.api.ProjectApi) Permissions(org.gitlab4j.api.models.Permissions) AccessLevel(org.gitlab4j.api.models.AccessLevel)

Example 4 with Group

use of org.gitlab4j.api.models.Group in project catma by forTEXT.

the class GitlabManagerRestricted method getCommentReplies.

@Override
public List<Reply> getCommentReplies(String projectId, Comment comment) throws IOException {
    String resourceId = comment.getDocumentId();
    String projectPath = projectId + "/" + resourceId;
    NotesApi notesApi = restrictedGitLabApi.getNotesApi();
    List<Reply> result = new ArrayList<Reply>();
    try {
        List<Note> notes = notesApi.getIssueNotes(projectPath, comment.getIid());
        for (Note note : notes.stream().filter(n -> !n.getSystem()).collect(Collectors.toList())) {
            // filter system notes
            String noteBody = note.getBody();
            Reply reply = null;
            try {
                reply = new SerializationHelper<Reply>().deserialize(noteBody, Reply.class);
                reply.setCommentUuid(comment.getUuid());
                reply.setId(note.getId());
                reply.setUserId(note.getAuthor().getId());
                reply.setUsername(note.getAuthor().getName());
            } catch (Exception e) {
                logger.log(Level.SEVERE, String.format("Error deserializing Reply #%1$d %2$s", note.getId(), noteBody), e);
                IDGenerator idGenerator = new IDGenerator();
                reply = new Reply(idGenerator.generate(), noteBody, note.getAuthor().getUsername(), note.getAuthor().getId(), comment.getUuid(), note.getId());
            }
            result.add(reply);
        }
        comment.setReplies(result);
        return result;
    } catch (GitLabApiException e) {
        throw new IOException(String.format("Failed to retrieve Replies for Comment %1$s %2$d for resource %3$s in group %4$s!", comment.getUuid(), comment.getIid(), resourceId, projectId), e);
    }
}
Also used : NotesApi(org.gitlab4j.api.NotesApi) JsonObject(com.google.gson.JsonObject) Reply(de.catma.document.comment.Reply) AccessLevel(org.gitlab4j.api.models.AccessLevel) ProjectFilter(org.gitlab4j.api.models.ProjectFilter) StringUtils(org.apache.commons.lang3.StringUtils) Author(org.gitlab4j.api.models.Author) IssuesApi(org.gitlab4j.api.IssuesApi) ChangeUserAttributeEvent(de.catma.ui.events.ChangeUserAttributeEvent) Map(java.util.Map) Group(org.gitlab4j.api.models.Group) IssueState(org.gitlab4j.api.Constants.IssueState) GroupApi(org.gitlab4j.api.GroupApi) Visibility(org.gitlab4j.api.models.Visibility) CATMAPropertyKey(de.catma.properties.CATMAPropertyKey) IGitUserInformation(de.catma.repository.git.interfaces.IGitUserInformation) GitMember(de.catma.repository.git.GitMember) Pager(org.gitlab4j.api.Pager) GitlabUtils(de.catma.repository.git.GitlabUtils) Set(java.util.Set) Logger(java.util.logging.Logger) SerializationHelper(de.catma.repository.git.serialization.SerializationHelper) Collectors(java.util.stream.Collectors) ProjectReference(de.catma.project.ProjectReference) Objects(java.util.Objects) List(java.util.List) ProjectApi(org.gitlab4j.api.ProjectApi) Optional(java.util.Optional) GitLabApiException(org.gitlab4j.api.GitLabApiException) Status(org.gitlab4j.api.models.ImportStatus.Status) CacheBuilder(com.google.common.cache.CacheBuilder) GitLabApi(org.gitlab4j.api.GitLabApi) RBACPermission(de.catma.rbac.RBACPermission) Permissions(org.gitlab4j.api.models.Permissions) GroupFilter(org.gitlab4j.api.models.GroupFilter) Namespace(org.gitlab4j.api.models.Namespace) HashMap(java.util.HashMap) RBACRole(de.catma.rbac.RBACRole) JsonParser(com.google.gson.JsonParser) User(de.catma.user.User) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) EventBus(com.google.common.eventbus.EventBus) IRemoteGitManagerRestricted(de.catma.repository.git.interfaces.IRemoteGitManagerRestricted) Comment(de.catma.document.comment.Comment) ForkStatus(de.catma.project.ForkStatus) GitProjectManager(de.catma.repository.git.GitProjectManager) Note(org.gitlab4j.api.models.Note) IDGenerator(de.catma.util.IDGenerator) Subscribe(com.google.common.eventbus.Subscribe) CreateRepositoryResponse(de.catma.repository.git.CreateRepositoryResponse) Issue(org.gitlab4j.api.models.Issue) IOException(java.io.IOException) Project(org.gitlab4j.api.models.Project) Maps(com.google.common.collect.Maps) IssueFilter(org.gitlab4j.api.models.IssueFilter) TimeUnit(java.util.concurrent.TimeUnit) Member(org.gitlab4j.api.models.Member) Cache(com.google.common.cache.Cache) Collections(java.util.Collections) BackgroundService(de.catma.backgroundservice.BackgroundService) GitUser(de.catma.repository.git.GitUser) ArrayList(java.util.ArrayList) GitLabApiException(org.gitlab4j.api.GitLabApiException) IOException(java.io.IOException) GitLabApiException(org.gitlab4j.api.GitLabApiException) IOException(java.io.IOException) SerializationHelper(de.catma.repository.git.serialization.SerializationHelper) Note(org.gitlab4j.api.models.Note) Reply(de.catma.document.comment.Reply) IDGenerator(de.catma.util.IDGenerator) NotesApi(org.gitlab4j.api.NotesApi)

Example 5 with Group

use of org.gitlab4j.api.models.Group in project catma by forTEXT.

the class GitlabManagerRestricted method deleteGroup.

@Override
public void deleteGroup(String path) throws IOException {
    GroupApi groupApi = restrictedGitLabApi.getGroupApi();
    try {
        // TODO: remove, deleteGroup can work with the path
        Group group = groupApi.getGroup(path);
        groupApi.deleteGroup(group);
    } catch (GitLabApiException e) {
        throw new IOException("Failed to delete remote group", e);
    }
}
Also used : Group(org.gitlab4j.api.models.Group) GroupApi(org.gitlab4j.api.GroupApi) GitLabApiException(org.gitlab4j.api.GitLabApiException) IOException(java.io.IOException)

Aggregations

Group (org.gitlab4j.api.models.Group)15 GitLabApiException (org.gitlab4j.api.GitLabApiException)13 IOException (java.io.IOException)11 Project (org.gitlab4j.api.models.Project)7 GitMember (de.catma.repository.git.GitMember)6 GroupApi (org.gitlab4j.api.GroupApi)6 Member (org.gitlab4j.api.models.Member)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 List (java.util.List)5 CreateRepositoryResponse (de.catma.repository.git.CreateRepositoryResponse)4 Cache (com.google.common.cache.Cache)3 CacheBuilder (com.google.common.cache.CacheBuilder)3 Maps (com.google.common.collect.Maps)3 EventBus (com.google.common.eventbus.EventBus)3 Subscribe (com.google.common.eventbus.Subscribe)3 JsonObject (com.google.gson.JsonObject)3 JsonParser (com.google.gson.JsonParser)3 BackgroundService (de.catma.backgroundservice.BackgroundService)3 Comment (de.catma.document.comment.Comment)3