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