Search in sources :

Example 1 with Permissions

use of org.gitlab4j.api.models.Permissions in project legend-sdlc by finos.

the class GitLabProjectApi method getUserAccess.

private AccessLevel getUserAccess(org.gitlab4j.api.models.Project gitLabProject) {
    Permissions permissions = gitLabProject.getPermissions();
    if (permissions != null) {
        ProjectAccess projectAccess = permissions.getProjectAccess();
        AccessLevel projectAccessLevel = (projectAccess == null) ? null : projectAccess.getAccessLevel();
        if (projectAccessLevel != null) {
            return projectAccessLevel;
        }
        ProjectAccess groupAccess = permissions.getGroupAccess();
        return (groupAccess == null) ? null : groupAccess.getAccessLevel();
    }
    return null;
}
Also used : Permissions(org.gitlab4j.api.models.Permissions) ProjectAccess(org.gitlab4j.api.models.ProjectAccess) AccessLevel(org.gitlab4j.api.models.AccessLevel)

Example 2 with Permissions

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

the class GitlabManagerRestricted method getResourcePermissions.

private Map<String, AccessLevel> getResourcePermissions(Integer groupId) throws GitLabApiException {
    Map<String, AccessLevel> resultMap = Maps.newHashMap();
    ProjectApi projectApi = new ProjectApi(restrictedGitLabApi);
    logger.info("Loading project permissions");
    List<Project> resourceAndContainerProjects = projectApi.getProjects(new ProjectFilter().withMembership(true));
    logger.info(String.format("Filtering %1$d resources on group #%2$d", resourceAndContainerProjects.size(), groupId));
    Set<Project> filteredOnGroupProjects = resourceAndContainerProjects.stream().filter(p -> p.getNamespace().getId().equals(groupId)).collect(Collectors.toSet());
    logger.info(String.format("Updating accesslevel registry for %1$d resources", filteredOnGroupProjects.size()));
    for (Project p : filteredOnGroupProjects) {
        Permissions permission = p.getPermissions();
        if (permission.getGroupAccess() != null) {
            resultMap.put(p.getName(), permission.getGroupAccess().getAccessLevel());
        }
        if (permission.getProjectAccess() != null && (!resultMap.containsKey(p.getName()) || resultMap.get(p.getName()).value.intValue() < permission.getProjectAccess().getAccessLevel().value.intValue())) {
            resultMap.put(p.getName(), permission.getProjectAccess().getAccessLevel());
        }
    }
    return resultMap;
}
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) Project(org.gitlab4j.api.models.Project) ProjectFilter(org.gitlab4j.api.models.ProjectFilter) ProjectApi(org.gitlab4j.api.ProjectApi) Permissions(org.gitlab4j.api.models.Permissions) AccessLevel(org.gitlab4j.api.models.AccessLevel)

Example 3 with Permissions

use of org.gitlab4j.api.models.Permissions in project fastjson by alibaba.

the class Issue2040 method test_for_issue_2040_2.

public void test_for_issue_2040_2() throws Exception {
    String json = "{\n" + "      \"project_access\": null,\n" + "      \"group_access\": {\n" + "        \"access_level\": 50,\n" + "        \"notification_level\": 3\n" + "      }\n" + "    }";
    ObjectMapper objectMapper = new ObjectMapper();
    // Permissions permissions = objectMapper.readValue(json, Permissions.class);
    Permissions permissions = JSON.parseObject(json, Permissions.class, config);
    System.out.println(JSON.toJSONString(permissions));
}
Also used : Permissions(org.gitlab4j.api.models.Permissions) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 4 with Permissions

use of org.gitlab4j.api.models.Permissions 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;
}
Also used : ProjectType(org.finos.legend.sdlc.domain.model.project.ProjectType) ProjectStructureConfiguration(org.finos.legend.sdlc.server.project.config.ProjectStructureConfiguration) Branch(org.gitlab4j.api.models.Branch) GitLabConfiguration(org.finos.legend.sdlc.server.gitlab.GitLabConfiguration) ProjectAuthorizationAction(org.finos.legend.sdlc.domain.model.project.accessRole.ProjectAuthorizationAction) LoggerFactory(org.slf4j.LoggerFactory) AccessLevel(org.gitlab4j.api.models.AccessLevel) GitLabApiTools(org.finos.legend.sdlc.server.gitlab.tools.GitLabApiTools) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) ProjectStructure(org.finos.legend.sdlc.server.project.ProjectStructure) MergeRequest(org.gitlab4j.api.models.MergeRequest) BackgroundTaskProcessor(org.finos.legend.sdlc.server.tools.BackgroundTaskProcessor) EnumSet(java.util.EnumSet) Visibility(org.gitlab4j.api.models.Visibility) RepositoryApi(org.gitlab4j.api.RepositoryApi) Pager(org.gitlab4j.api.Pager) Set(java.util.Set) Iterate(org.eclipse.collections.impl.utility.Iterate) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ProjectConfigurationUpdateBuilder(org.finos.legend.sdlc.server.project.ProjectConfigurationUpdateBuilder) List(java.util.List) Stream(java.util.stream.Stream) ProjectStructurePlatformExtensions(org.finos.legend.sdlc.server.project.ProjectStructurePlatformExtensions) Pattern(java.util.regex.Pattern) GitLabApi(org.gitlab4j.api.GitLabApi) ProjectApi(org.finos.legend.sdlc.server.domain.api.project.ProjectApi) Permissions(org.gitlab4j.api.models.Permissions) Lists(org.eclipse.collections.api.factory.Lists) GitLabUserContext(org.finos.legend.sdlc.server.gitlab.auth.GitLabUserContext) ProjectStructureExtensionProvider(org.finos.legend.sdlc.server.project.extension.ProjectStructureExtensionProvider) ProjectCreationConfiguration(org.finos.legend.sdlc.server.project.config.ProjectCreationConfiguration) AccessRole(org.finos.legend.sdlc.domain.model.project.accessRole.AccessRole) Inject(javax.inject.Inject) StreamSupport(java.util.stream.StreamSupport) PagerTools(org.finos.legend.sdlc.server.gitlab.tools.PagerTools) Status(javax.ws.rs.core.Response.Status) Sets(org.eclipse.collections.api.factory.Sets) Logger(org.slf4j.Logger) GitLabMode(org.finos.legend.sdlc.server.gitlab.mode.GitLabMode) ProtectedTag(org.gitlab4j.api.models.ProtectedTag) ProjectFileAccessProvider(org.finos.legend.sdlc.server.project.ProjectFileAccessProvider) ListIterate(org.eclipse.collections.impl.utility.ListIterate) WorkspaceType(org.finos.legend.sdlc.domain.model.project.workspace.WorkspaceType) ProjectStructureVersion(org.finos.legend.sdlc.domain.model.project.configuration.ProjectStructureVersion) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) Project(org.finos.legend.sdlc.domain.model.project.Project) ProjectConfiguration(org.finos.legend.sdlc.domain.model.project.configuration.ProjectConfiguration) Revision(org.finos.legend.sdlc.domain.model.revision.Revision) Comparator(java.util.Comparator) ProjectAccess(org.gitlab4j.api.models.ProjectAccess) Collections(java.util.Collections) ProtectedTag(org.gitlab4j.api.models.ProtectedTag) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException)

Example 5 with Permissions

use of org.gitlab4j.api.models.Permissions 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);
    }
}
Also used : LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) Permissions(org.gitlab4j.api.models.Permissions) ProjectAccess(org.gitlab4j.api.models.ProjectAccess) AccessLevel(org.gitlab4j.api.models.AccessLevel) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException)

Aggregations

Permissions (org.gitlab4j.api.models.Permissions)5 AccessLevel (org.gitlab4j.api.models.AccessLevel)4 Collections (java.util.Collections)2 List (java.util.List)2 Objects (java.util.Objects)2 Set (java.util.Set)2 LegendSDLCServerException (org.finos.legend.sdlc.server.error.LegendSDLCServerException)2 GitLabProjectId (org.finos.legend.sdlc.server.gitlab.GitLabProjectId)2 ProjectAccess (org.gitlab4j.api.models.ProjectAccess)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Cache (com.google.common.cache.Cache)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 Maps (com.google.common.collect.Maps)1 EventBus (com.google.common.eventbus.EventBus)1 Subscribe (com.google.common.eventbus.Subscribe)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 BackgroundService (de.catma.backgroundservice.BackgroundService)1 Comment (de.catma.document.comment.Comment)1 Reply (de.catma.document.comment.Reply)1