Search in sources :

Example 11 with AccessLevel

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

the class GitLabUserManagementService method updateOldGroupMembers.

/**
 * Updates the permission for users that have 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  programming exercises of the passed updatedCourse
 * @param oldUsers              users of the passed course that have been in a group before
 * @param updatedCourse         course with updated groups
 */
private void updateOldGroupMembers(List<ProgrammingExercise> programmingExercises, Set<User> oldUsers, Course updatedCourse) {
    final var userApi = gitlabApi.getUserApi();
    for (User user : oldUsers) {
        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;
            }
            Set<String> groups = user.getGroups();
            if (groups == null) {
                removeMemberFromExercises(programmingExercises, gitlabUser.getId());
                continue;
            }
            Optional<AccessLevel> accessLevel = getAccessLevelFromUserGroups(groups, updatedCourse);
            if (accessLevel.isPresent()) {
                updateMemberExercisePermissions(programmingExercises, gitlabUser.getId(), accessLevel.get());
            } else {
                removeMemberFromExercises(programmingExercises, gitlabUser.getId());
            }
        } catch (GitLabApiException e) {
            throw new GitLabException("Error while trying to update 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 12 with AccessLevel

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

the class GitLabUserManagementService method updateOldGroupMembers.

/**
 * Updates the permission for users that have 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  programming exercises of the passed updatedCourse
 * @param oldUsers              users of the passed course that have been in a group before
 * @param updatedCourse         course with updated groups
 */
private void updateOldGroupMembers(List<ProgrammingExercise> programmingExercises, Set<User> oldUsers, Course updatedCourse) {
    final var userApi = gitlabApi.getUserApi();
    for (User user : oldUsers) {
        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;
            }
            Set<String> groups = user.getGroups();
            if (groups == null) {
                removeMemberFromExercises(programmingExercises, gitlabUser.getId());
                continue;
            }
            Optional<AccessLevel> accessLevel = getAccessLevelFromUserGroups(groups, updatedCourse);
            if (accessLevel.isPresent()) {
                updateMemberExercisePermissions(programmingExercises, gitlabUser.getId(), accessLevel.get());
            } else {
                removeMemberFromExercises(programmingExercises, gitlabUser.getId());
            }
        } catch (GitLabApiException e) {
            throw new GitLabException("Error while trying to update 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 13 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(Long 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 its 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 14 with AccessLevel

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

the class GitlabRequestMockProvider method mockSetPermissionsForNewGroupMembers.

private void mockSetPermissionsForNewGroupMembers(List<ProgrammingExercise> programmingExercises, Set<de.tum.in.www1.artemis.domain.User> newUsers, Course updatedCourse) {
    for (de.tum.in.www1.artemis.domain.User user : newUsers) {
        try {
            mockGetUserId(user.getLogin(), true, false);
            Optional<AccessLevel> accessLevel = getAccessLevelFromUserGroups(user.getGroups(), updatedCourse);
            if (accessLevel.isPresent()) {
                mockAddUserToGroups(1L, programmingExercises, accessLevel.get());
            } else {
                mockRemoveMemberFromExercises(programmingExercises);
            }
        } catch (GitLabApiException e) {
            throw new GitLabException("Error while trying to set permission for user in GitLab: " + user, e);
        }
    }
}
Also used : GitLabException(de.tum.in.www1.artemis.service.connectors.gitlab.GitLabException) AccessLevel(org.gitlab4j.api.models.AccessLevel)

Example 15 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(Long 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 its 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)

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