24
24
*/
25
25
package org .opengrok .indexer .web ;
26
26
27
+ import static org .opengrok .indexer .index .Indexer .PATH_SEPARATOR ;
28
+ import static org .opengrok .indexer .index .Indexer .PATH_SEPARATOR_STRING ;
29
+
27
30
import java .io .BufferedReader ;
28
31
import java .io .File ;
29
32
import java .io .FileNotFoundException ;
75
78
import org .suigeneris .jrcs .diff .Diff ;
76
79
import org .suigeneris .jrcs .diff .DifferentiationFailedException ;
77
80
78
- import static org .opengrok .indexer .index .Indexer .PATH_SEPARATOR ;
79
- import static org .opengrok .indexer .index .Indexer .PATH_SEPARATOR_STRING ;
80
-
81
81
/**
82
82
* A simple container to lazy initialize common vars wrt. a single request. It
83
83
* MUST NOT be shared between several requests and
@@ -101,8 +101,10 @@ public final class PageConfig {
101
101
102
102
private static final Logger LOGGER = LoggerFactory .getLogger (PageConfig .class );
103
103
104
- public static final String OPEN_GROK_PROJECT = "OpenGrokProject" ;
105
-
104
+ protected static final String OPEN_GROK_PROJECT = "OpenGrokProject" ;
105
+ protected static final String PROJECT_PARAM_NAME = "project" ;
106
+ protected static final String GROUP_PARAM_NAME = "group" ;
107
+
106
108
// TODO if still used, get it from the app context
107
109
108
110
private final AuthorizationFramework authFramework ;
@@ -794,24 +796,33 @@ public String getRequestedProjectsAsString() {
794
796
* <p>
795
797
* NOTE: This method assumes, that project names do <b>not</b> contain a
796
798
* comma (','), since this character is used as name separator!
799
+ * <p>
800
+ * It is determined as follows:
801
+ * <ol>
802
+ * <li>If there is no project in the configuration an empty set is returned. Otherwise:</li>
803
+ * <li>If there is only one project in the configuration,
804
+ * this one gets returned (no matter, what the request actually says). Otherwise</li>
805
+ * <li>If the request parameter {@code PROJECT_PARAM_NAME} contains any available project,
806
+ * the set with invalid projects removed gets returned. Otherwise:</li>
807
+ * <li>If the request parameter {@code GROUP_PARAM_NAME} contains any available group,
808
+ * then all projects from that group will be added to the result set. Otherwise:</li>
809
+ * <li>If the request has a cookie with the name {@code OPEN_GROK_PROJECT}
810
+ * and it contains any available project,
811
+ * the set with invalid projects removed gets returned. Otherwise:</li>
812
+ * <li>If a default project is set in the configuration,
813
+ * this project gets returned. Otherwise:</li>
814
+ * <li>an empty set</li>
815
+ * </ol>
797
816
*
798
- * @return a possible empty set of project names but never
799
- * {@code null}. It is determined as follows: <ol> <li>If there is no
800
- * project in the runtime environment (RTE) an empty set is returned.
801
- * Otherwise:</li> <li>If there is only one project in the RTE, this one
802
- * gets returned (no matter, what the request actually says). Otherwise</li>
803
- * <li>If the request parameter {@code project} contains any available
804
- * project, the set with invalid projects removed gets returned.
805
- * Otherwise:</li> <li>If the request has a cookie with the name
806
- * {@code OpenGrokProject} and it contains any available project, the set
807
- * with invalid projects removed gets returned. Otherwise:</li> <li>If a
808
- * default project is set in the RTE, this project gets returned.
809
- * Otherwise:</li> <li>an empty set</li> </ol>
817
+ * @return a possible empty set of project names but never {@code null}.
818
+ * @see #PROJECT_PARAM_NAME
819
+ * @see #GROUP_PARAM_NAME
820
+ * @see #OPEN_GROK_PROJECT
810
821
*/
811
822
public SortedSet <String > getRequestedProjects () {
812
823
if (requestedProjects == null ) {
813
824
requestedProjects
814
- = getRequestedProjects ("project" , OPEN_GROK_PROJECT );
825
+ = getRequestedProjects (PROJECT_PARAM_NAME , GROUP_PARAM_NAME , OPEN_GROK_PROJECT );
815
826
}
816
827
return requestedProjects ;
817
828
}
@@ -875,71 +886,90 @@ private List<String> getParamVals(String paramName) {
875
886
876
887
/**
877
888
* Same as {@link #getRequestedProjects()}, but with a variable cookieName
878
- * and parameter name. This way it is trivial to implement a project filter
879
- * ...
889
+ * and parameter name.
880
890
*
881
- * @param paramName the name of the request parameter, which possibly
882
- * contains the project list in question.
883
- * @param cookieName name of the cookie which possible contains project
884
- * lists used as fallback
891
+ * @param projectParamName the name of the request parameter corresponding to a project name.
892
+ * @param groupParamName the name of the request parameter corresponding to a group name
893
+ * @param cookieName name of the cookie which possible contains project
894
+ * names used as fallback
885
895
* @return set of project names. Possibly empty set but never {@code null}.
886
896
*/
887
- protected SortedSet <String > getRequestedProjects (String paramName ,
888
- String cookieName ) {
897
+ protected SortedSet <String > getRequestedProjects (
898
+ String projectParamName ,
899
+ String groupParamName ,
900
+ String cookieName
901
+ ) {
889
902
890
- TreeSet <String > set = new TreeSet <>();
903
+ TreeSet <String > projectNames = new TreeSet <>();
891
904
List <Project > projects = getEnv ().getProjectList ();
892
905
893
906
if (projects == null ) {
894
- return set ;
907
+ return projectNames ;
895
908
}
896
909
897
910
/**
898
- * If the project was determined from the URL, use this project.
911
+ * Use a project determined directly from the URL
899
912
*/
900
913
if (getProject () != null ) {
901
- set .add (getProject ().getName ());
902
- return set ;
914
+ projectNames .add (getProject ().getName ());
915
+ return projectNames ;
903
916
}
904
917
918
+ /**
919
+ * Use a project if the application has only single project.
920
+ */
905
921
if (projects .size () == 1 ) {
906
922
Project p = projects .get (0 );
907
923
if (authFramework .isAllowed (req , p )) {
908
- set .add (p .getName ());
924
+ projectNames .add (p .getName ());
909
925
}
910
- return set ;
926
+ return projectNames ;
911
927
}
912
928
913
- List <String > vals = getParamVals (paramName );
914
- for (String s : vals ) {
915
- Project x = Project .getByName (s );
916
- if (x != null && authFramework .isAllowed (req , x )) {
917
- set .add (s );
929
+ /**
930
+ * Add all projects which match the project parameter name values
931
+ */
932
+ List <String > names = getParamVals (projectParamName );
933
+ for (String projectName : names ) {
934
+ Project project = Project .getByName (projectName );
935
+ if (project != null && authFramework .isAllowed (req , project )) {
936
+ projectNames .add (projectName );
937
+ }
938
+ }
939
+
940
+ /**
941
+ * Add all projects which are part of a group that matches the group parameter name
942
+ */
943
+ names = getParamVals (groupParamName );
944
+ for (String groupName : names ) {
945
+ Group group = Group .getByName (groupName );
946
+ if (group != null ) {
947
+ projectNames .addAll (getProjectHelper ().getAllGrouped (group ).stream ().map (Project ::getName ).collect (Collectors .toSet ()));
918
948
}
919
949
}
920
950
921
- if (set .isEmpty ()) {
951
+ if (projectNames .isEmpty ()) {
922
952
List <String > cookies = getCookieVals (cookieName );
923
953
for (String s : cookies ) {
924
954
Project x = Project .getByName (s );
925
955
if (x != null && authFramework .isAllowed (req , x )) {
926
- set .add (s );
956
+ projectNames .add (s );
927
957
}
928
958
}
929
959
}
930
960
931
- if (set .isEmpty ()) {
961
+ if (projectNames .isEmpty ()) {
932
962
Set <Project > defaultProjects = env .getDefaultProjects ();
933
963
if (defaultProjects != null ) {
934
964
for (Project project : defaultProjects ) {
935
965
if (authFramework .isAllowed (req , project )) {
936
- set .add (project .getName ());
966
+ projectNames .add (project .getName ());
937
967
}
938
968
}
939
969
}
940
970
}
941
971
942
- return set ;
972
+ return projectNames ;
943
973
}
944
974
945
975
public ProjectHelper getProjectHelper () {
0 commit comments