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 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 legend-sdlc by finos.
the class GitLabProjectApi method checkUserReleasePermission.
private boolean checkUserReleasePermission(GitLabProjectId projectId, ProjectAuthorizationAction action, AccessLevel accessLevel) {
try {
List<ProtectedTag> protectedTags = withRetries(() -> getGitLabApi(projectId.getGitLabMode()).getTagsApi().getProtectedTags(projectId.getGitLabId()));
if (protectedTags == null || protectedTags.isEmpty()) {
// By default user can perform a release if the user has developer access or above https://docs.gitlab.com/ee/user/permissions.html#release-permissions-with-protected-tags
return defaultReleaseAction(accessLevel);
}
protectedTags = protectedTags.stream().filter(a -> a.getName().startsWith("release") || a.getName().startsWith("version")).collect(Collectors.toList());
for (ProtectedTag tag : protectedTags) {
if (tag.getCreateAccessLevels().isEmpty()) {
return defaultReleaseAction(accessLevel);
}
// with th release protected tag the user must have the min access_level
List<ProtectedTag.CreateAccessLevel> matchedTags = tag.getCreateAccessLevels().stream().filter(a -> a.getAccess_level().value >= accessLevel.value).collect(Collectors.toList());
// if the machedTags are empty or null user access does not match any of the protected tags
if (matchedTags.isEmpty()) {
return defaultReleaseAction(accessLevel);
}
// User does not meet all criteria not authorized for the action
if (matchedTags.size() != tag.getCreateAccessLevels().size()) {
return false;
}
}
} catch (Exception e) {
throw buildException(e, () -> "Failed to get protected tags for " + projectId.getGitLabId());
}
return false;
}
use of org.gitlab4j.api.models.AccessLevel in project legend-sdlc by finos.
the class GitLabProjectApi method checkUserAuthorizationAction.
@Override
public boolean checkUserAuthorizationAction(String id, ProjectAuthorizationAction action) {
try {
GitLabProjectId projectId = parseProjectId(id);
org.gitlab4j.api.models.Project gitLabProject = withRetries(() -> getGitLabApi(projectId.getGitLabMode()).getProjectApi().getProject(projectId.getGitLabId()));
if (!isLegendSDLCProject(gitLabProject)) {
throw new LegendSDLCServerException("Failed to get project " + id);
}
AccessLevel userLevel = getUserAccess(gitLabProject);
if (userLevel == null) {
return false;
}
return checkUserAction(projectId, action, userLevel);
} catch (Exception e) {
throw buildException(e, () -> "Failed to get project " + id);
}
}
use of org.gitlab4j.api.models.AccessLevel in project legend-sdlc by finos.
the class GitLabProjectApi method getCurrentUserAccessRole.
@Override
public AccessRole getCurrentUserAccessRole(String id) {
LegendSDLCServerException.validateNonNull(id, "id may not be null");
try {
GitLabProjectId projectId = parseProjectId(id);
org.gitlab4j.api.models.Project gitLabProject = withRetries(() -> getGitLabApi(projectId.getGitLabMode()).getProjectApi().getProject(projectId.getGitLabId()));
if (!isLegendSDLCProject(gitLabProject)) {
throw new LegendSDLCServerException("Failed to get project " + id);
}
Permissions permissions = gitLabProject.getPermissions();
if (permissions != null) {
ProjectAccess projectAccess = permissions.getProjectAccess();
AccessLevel projectAccessLevel = (projectAccess == null) ? null : projectAccess.getAccessLevel();
if (projectAccessLevel != null) {
return new AccessRoleWrapper(projectAccessLevel);
}
ProjectAccess groupAccess = permissions.getGroupAccess();
AccessLevel groupAccessLevel = (groupAccess == null) ? null : groupAccess.getAccessLevel();
if (groupAccessLevel != null) {
return new AccessRoleWrapper(groupAccessLevel);
}
}
return null;
} catch (Exception e) {
throw buildException(e, () -> "Failed to get project " + id);
}
}
Aggregations