Search in sources :

Example 6 with AccessLevel

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

the class GitlabManagerRestricted method getRolesPerResource.

public Map<String, RBACRole> getRolesPerResource(String projectId) throws IOException {
    try {
        Group group = restrictedGitLabApi.getGroupApi().getGroup(projectId);
        Map<String, AccessLevel> permMap = getResourcePermissions(group.getId());
        return permMap.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> RBACRole.forValue(e.getValue().value)));
    } catch (GitLabApiException e) {
        throw new IOException("Permission retrieval failed!", 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) Group(org.gitlab4j.api.models.Group) GitLabApiException(org.gitlab4j.api.GitLabApiException) IOException(java.io.IOException) Map(java.util.Map) HashMap(java.util.HashMap) AccessLevel(org.gitlab4j.api.models.AccessLevel)

Example 7 with AccessLevel

use of org.gitlab4j.api.models.AccessLevel in project ArTEMiS by ls1intum.

the class GitLabUserManagementService method removeOrUpdateUserFromGroups.

/**
 * Removes or updates the user to or from the groups.
 *
 * @param gitlabUserId the Gitlab user id
 * @param userGroups groups that the user belongs to
 * @param groupsToRemove groups where the user should be removed from
 */
private void removeOrUpdateUserFromGroups(int gitlabUserId, Set<String> userGroups, Set<String> groupsToRemove) throws GitLabApiException {
    if (groupsToRemove == null || groupsToRemove.isEmpty()) {
        return;
    }
    // Gitlab groups are identified by the project key of the programming exercise
    var exercises = programmingExerciseRepository.findAllByInstructorOrEditorOrTAGroupNameIn(groupsToRemove);
    log.info("Update Gitlab permissions for programming exercises: " + exercises.stream().map(ProgrammingExercise::getProjectKey).toList());
    for (var exercise : exercises) {
        // TODO: in case we update a tutor group / role here, the tutor should NOT get access to exam exercises before the exam has finished
        Course course = exercise.getCourseViaExerciseGroupOrCourseMember();
        Optional<AccessLevel> accessLevel = getAccessLevelFromUserGroups(userGroups, course);
        // Do not remove the user from the group and only update it's access level
        var shouldUpdateGroupAccess = accessLevel.isPresent();
        if (shouldUpdateGroupAccess) {
            gitlabApi.getGroupApi().updateMember(exercise.getProjectKey(), gitlabUserId, accessLevel.get());
        } else {
            removeUserFromGroup(gitlabUserId, exercise.getProjectKey());
        }
    }
}
Also used : Course(de.tum.in.www1.artemis.domain.Course) AccessLevel(org.gitlab4j.api.models.AccessLevel)

Example 8 with AccessLevel

use of org.gitlab4j.api.models.AccessLevel in project ArTEMiS by ls1intum.

the class GitLabUserManagementService method setPermissionsForNewGroupMembers.

/**
 * Sets the permission for users that have not been in a group before.
 * The permissions are updated for all programming exercises of a course, according to the user groups the user is part of.
 *
 * @param programmingExercises  all programming exercises of the passed updatedCourse
 * @param newUsers              users of the passed course that have not been in a group before
 * @param updatedCourse         course with updated groups
 */
private void setPermissionsForNewGroupMembers(List<ProgrammingExercise> programmingExercises, Set<User> newUsers, Course updatedCourse) {
    final var userApi = gitlabApi.getUserApi();
    for (User user : newUsers) {
        try {
            var gitlabUser = userApi.getUser(user.getLogin());
            if (gitlabUser == null) {
                log.warn("User {} does not exist in Gitlab and cannot be updated!", user.getLogin());
                continue;
            }
            Optional<AccessLevel> accessLevel = getAccessLevelFromUserGroups(user.getGroups(), updatedCourse);
            if (accessLevel.isPresent()) {
                addUserToGroupsOfExercises(gitlabUser.getId(), programmingExercises, accessLevel.get());
            } else {
                removeMemberFromExercises(programmingExercises, gitlabUser.getId());
            }
        } catch (GitLabApiException e) {
            throw new GitLabException("Error while trying to set permission for user in GitLab: " + user, e);
        }
    }
}
Also used : User(de.tum.in.www1.artemis.domain.User) GitLabApiException(org.gitlab4j.api.GitLabApiException) AccessLevel(org.gitlab4j.api.models.AccessLevel)

Example 9 with AccessLevel

use of org.gitlab4j.api.models.AccessLevel in project ArTEMiS by ls1intum.

the class GitLabUserManagementService method addUserToGroups.

/**
 * Adds the Gitlab user to the groups. It will be given a different access level
 * based on the group type (instructors are given the MAINTAINER level and teaching
 * assistants REPORTED).
 *
 * @param gitlabUserId the user id of the Gitlab user
 * @param groups the new groups
 */
private void addUserToGroups(Long gitlabUserId, Set<String> groups) {
    if (groups == null || groups.isEmpty()) {
        return;
    }
    List<ProgrammingExercise> exercises = programmingExerciseRepository.findAllByInstructorOrEditorOrTAGroupNameIn(groups);
    log.info("Update Gitlab permissions for programming exercises: " + exercises.stream().map(ProgrammingExercise::getProjectKey).toList());
    // TODO: in case we update a tutor group / role here, the tutor should NOT get access to exam exercises before the exam has finished
    for (var exercise : exercises) {
        Course course = exercise.getCourseViaExerciseGroupOrCourseMember();
        Optional<AccessLevel> accessLevel = getAccessLevelFromUserGroups(groups, course);
        accessLevel.ifPresent(level -> addUserToGroup(exercise.getProjectKey(), gitlabUserId, level));
    }
}
Also used : ProgrammingExercise(de.tum.in.www1.artemis.domain.ProgrammingExercise) Course(de.tum.in.www1.artemis.domain.Course) AccessLevel(org.gitlab4j.api.models.AccessLevel)

Example 10 with AccessLevel

use of org.gitlab4j.api.models.AccessLevel in project Artemis by ls1intum.

the class GitLabUserManagementService method setPermissionsForNewGroupMembers.

/**
 * Sets the permission for users that have not been in a group before.
 * The permissions are updated for all programming exercises of a course, according to the user groups the user is part of.
 *
 * @param programmingExercises  all programming exercises of the passed updatedCourse
 * @param newUsers              users of the passed course that have not been in a group before
 * @param updatedCourse         course with updated groups
 */
private void setPermissionsForNewGroupMembers(List<ProgrammingExercise> programmingExercises, Set<User> newUsers, Course updatedCourse) {
    final var userApi = gitlabApi.getUserApi();
    for (User user : newUsers) {
        try {
            var gitlabUser = userApi.getUser(user.getLogin());
            if (gitlabUser == null) {
                log.warn("User {} does not exist in Gitlab and cannot be updated!", user.getLogin());
                continue;
            }
            Optional<AccessLevel> accessLevel = getAccessLevelFromUserGroups(user.getGroups(), updatedCourse);
            if (accessLevel.isPresent()) {
                addUserToGroupsOfExercises(gitlabUser.getId(), programmingExercises, accessLevel.get());
            } else {
                removeMemberFromExercises(programmingExercises, gitlabUser.getId());
            }
        } catch (GitLabApiException e) {
            throw new GitLabException("Error while trying to set permission for user in GitLab: " + user, e);
        }
    }
}
Also used : User(de.tum.in.www1.artemis.domain.User) GitLabApiException(org.gitlab4j.api.GitLabApiException) AccessLevel(org.gitlab4j.api.models.AccessLevel)

Aggregations

AccessLevel (org.gitlab4j.api.models.AccessLevel)21 Course (de.tum.in.www1.artemis.domain.Course)8 Permissions (org.gitlab4j.api.models.Permissions)6 GitLabApiException (org.gitlab4j.api.GitLabApiException)5 ProgrammingExercise (de.tum.in.www1.artemis.domain.ProgrammingExercise)4 User (de.tum.in.www1.artemis.domain.User)4 Collections (java.util.Collections)4 List (java.util.List)4 Objects (java.util.Objects)4 Set (java.util.Set)4 Collectors (java.util.stream.Collectors)4 LegendSDLCServerException (org.finos.legend.sdlc.server.error.LegendSDLCServerException)3 GitLabProjectId (org.finos.legend.sdlc.server.gitlab.GitLabProjectId)3 GitLabApi (org.gitlab4j.api.GitLabApi)3 Pager (org.gitlab4j.api.Pager)3 Visibility (org.gitlab4j.api.models.Visibility)3 Cache (com.google.common.cache.Cache)2 CacheBuilder (com.google.common.cache.CacheBuilder)2 Maps (com.google.common.collect.Maps)2 EventBus (com.google.common.eventbus.EventBus)2