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);
}
}
}
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);
}
}
}
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());
}
}
}
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);
}
}
}
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());
}
}
}
Aggregations