Search in sources :

Example 1 with Group

use of org.opengrok.indexer.configuration.Group in project OpenGrok by OpenGrok.

the class PageConfig method getRequestedProjects.

/**
 * Same as {@link #getRequestedProjects()}, but with a variable cookieName
 * and parameter name.
 *
 * @param searchAllParamName the name of the request parameter corresponding to search all projects.
 * @param projectParamName   the name of the request parameter corresponding to a project name.
 * @param groupParamName     the name of the request parameter corresponding to a group name
 * @param cookieName         name of the cookie which possible contains project
 *                           names used as fallback
 * @return set of project names. Possibly empty set but never {@code null}.
 */
protected SortedSet<String> getRequestedProjects(String searchAllParamName, String projectParamName, String groupParamName, String cookieName) {
    TreeSet<String> projectNames = new TreeSet<>();
    List<Project> projects = getEnv().getProjectList();
    if (Boolean.parseBoolean(req.getParameter(searchAllParamName))) {
        return getProjectHelper().getAllProjects().stream().map(Project::getName).collect(Collectors.toCollection(TreeSet::new));
    }
    // Use a project determined directly from the URL
    if (getProject() != null && getProject().isIndexed()) {
        projectNames.add(getProject().getName());
        return projectNames;
    }
    // Use a project if there is just a single project.
    if (projects.size() == 1) {
        Project p = projects.get(0);
        if (p.isIndexed() && authFramework.isAllowed(req, p)) {
            projectNames.add(p.getName());
        }
        return projectNames;
    }
    // Add all projects which match the project parameter name values/
    List<String> names = getParameterValues(projectParamName);
    for (String projectName : names) {
        Project project = Project.getByName(projectName);
        if (project != null && project.isIndexed() && authFramework.isAllowed(req, project)) {
            projectNames.add(projectName);
        }
    }
    // Add all projects which are part of a group that matches the group parameter name.
    names = getParameterValues(groupParamName);
    for (String groupName : names) {
        Group group = Group.getByName(groupName);
        if (group != null) {
            projectNames.addAll(getProjectHelper().getAllGrouped(group).stream().filter(Project::isIndexed).map(Project::getName).collect(Collectors.toSet()));
        }
    }
    // Add projects based on cookie.
    if (projectNames.isEmpty() && getIntParam(QueryParameters.NUM_SELECTED_PARAM, -1) != 0) {
        List<String> cookies = getCookieVals(cookieName);
        for (String s : cookies) {
            Project x = Project.getByName(s);
            if (x != null && x.isIndexed() && authFramework.isAllowed(req, x)) {
                projectNames.add(s);
            }
        }
    }
    // Add default projects.
    if (projectNames.isEmpty()) {
        Set<Project> defaultProjects = env.getDefaultProjects();
        if (defaultProjects != null) {
            for (Project project : defaultProjects) {
                if (project.isIndexed() && authFramework.isAllowed(req, project)) {
                    projectNames.add(project.getName());
                }
            }
        }
    }
    return projectNames;
}
Also used : Project(org.opengrok.indexer.configuration.Project) Group(org.opengrok.indexer.configuration.Group) TreeSet(java.util.TreeSet)

Example 2 with Group

use of org.opengrok.indexer.configuration.Group in project OpenGrok by OpenGrok.

the class ProjectHelper method hasFavourite.

/**
 * Checks if given group contains a favourite project.
 *
 * Favourite project is a project which is contained in the OpenGrokProject
 * cookie, i. e. it has been searched or viewed by the user.
 *
 * This should by used to determine if this group should be displayed
 * expanded or rolled up.
 *
 * @param group group
 * @return true if it has favourite project
 */
@SuppressWarnings(value = "unchecked")
public boolean hasFavourite(Group group) {
    Boolean val;
    Map<String, Boolean> p = (Map<String, Boolean>) cfg.getRequestAttribute(PROJECT_HELPER_FAVOURITE_GROUP);
    if (p == null) {
        p = new TreeMap<>();
        cfg.setRequestAttribute(PROJECT_HELPER_FAVOURITE_GROUP, p);
    }
    val = p.get(group.getName());
    if (val == null) {
        Set<Project> favourite = getAllGrouped();
        favourite.removeIf(t -> {
            // project is favourite
            if (!isFavourite(t)) {
                return true;
            }
            // project is contained in group repositories
            if (getRepositories(group).contains(t)) {
                return false;
            }
            // project is contained in group projects
            if (getProjects(group).contains(t)) {
                return false;
            }
            // project is contained in subgroup's repositories and projects
            for (Group g : filterGroups(group.getDescendants())) {
                if (getProjects(g).contains(t)) {
                    return false;
                }
                if (getRepositories(g).contains(t)) {
                    return false;
                }
            }
            return true;
        });
        val = !favourite.isEmpty();
        p.put(group.getName(), val);
    }
    cfg.setRequestAttribute(PROJECT_HELPER_FAVOURITE_GROUP, p);
    return val;
}
Also used : Project(org.opengrok.indexer.configuration.Project) Group(org.opengrok.indexer.configuration.Group) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 3 with Group

use of org.opengrok.indexer.configuration.Group in project OpenGrok by OpenGrok.

the class ProjectHelperTest method testGetProjectsAllowedGroup.

/**
 * Test of getProjects method, of class ProjectHelper.
 */
@Test
public void testGetProjectsAllowedGroup() {
    for (Group g : RuntimeEnvironment.getInstance().getGroups()) {
        if (g.getName().startsWith("allowed_group_0")) {
            Set<Project> result = helper.getProjects(g);
            assertEquals(2, result.size());
            for (Project p : result) {
                assertTrue(p.getName().startsWith("allowed_"));
            }
        }
    }
}
Also used : Group(org.opengrok.indexer.configuration.Group) Project(org.opengrok.indexer.configuration.Project) Test(org.junit.jupiter.api.Test)

Example 4 with Group

use of org.opengrok.indexer.configuration.Group in project OpenGrok by OpenGrok.

the class ProjectHelperTest method testGetRepositoriesAllowedGroup.

/**
 * Test of getRepositories method, of class ProjectHelper.
 */
@Test
public void testGetRepositoriesAllowedGroup() {
    for (Group g : RuntimeEnvironment.getInstance().getGroups()) {
        if (g.getName().startsWith("allowed_group_0")) {
            Set<Project> result = helper.getRepositories(g);
            assertEquals(2, result.size());
            for (Project p : result) {
                assertTrue(p.getName().startsWith("allowed_"));
            }
        }
    }
}
Also used : Group(org.opengrok.indexer.configuration.Group) Project(org.opengrok.indexer.configuration.Project) Test(org.junit.jupiter.api.Test)

Example 5 with Group

use of org.opengrok.indexer.configuration.Group in project OpenGrok by OpenGrok.

the class ProjectHelperExtendedTest method testHasAllowedSubgroupAllowedSubgroups.

@Test
public void testHasAllowedSubgroupAllowedSubgroups() {
    Group g = getAllowedGroupWithSubgroups();
    assertTrue(helper.hasAllowedSubgroup(g));
}
Also used : Group(org.opengrok.indexer.configuration.Group) Test(org.junit.jupiter.api.Test)

Aggregations

Group (org.opengrok.indexer.configuration.Group)33 Test (org.junit.jupiter.api.Test)19 Project (org.opengrok.indexer.configuration.Project)19 DummyHttpServletRequest (org.opengrok.indexer.web.DummyHttpServletRequest)7 User (opengrok.auth.plugin.entity.User)5 BeforeEach (org.junit.jupiter.api.BeforeEach)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 MethodSource (org.junit.jupiter.params.provider.MethodSource)4 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 TreeSet (java.util.TreeSet)3 Entity (jakarta.ws.rs.client.Entity)2 GenericType (jakarta.ws.rs.core.GenericType)2 Response (jakarta.ws.rs.core.Response)2 IOException (java.io.IOException)2 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2 Paths (java.nio.file.Paths)2