Skip to content

Commit d9fadb4

Browse files
tulinkryVladimir Kotal
authored andcommitted
adding the group discovery for forGroups also into the plugin (#1577)
1 parent 94d43f4 commit d9fadb4

File tree

4 files changed

+168
-20
lines changed

4 files changed

+168
-20
lines changed

src/org/opensolaris/opengrok/authorization/AuthorizationEntity.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import java.util.TreeMap;
2929
import java.util.TreeSet;
3030
import java.util.function.Predicate;
31+
import java.util.stream.Collectors;
32+
import org.opensolaris.opengrok.configuration.Group;
3133
import org.opensolaris.opengrok.configuration.Nameable;
3234

3335
/**
@@ -356,6 +358,38 @@ public void setForGroups(String[] groups) {
356358
}
357359
}
358360

361+
/**
362+
* Discover all targeted groups and projects for every group given by
363+
* {@link #forGroups()}.
364+
*
365+
* <ul>
366+
* <li>add to the {@link #forGroups()} all groups which are descendant
367+
* groups to the group</li>
368+
* <li>add to the {@link #forGroups()} all groups which are parent groups to
369+
* the group</li>
370+
* <li>add to the {@link #forProjects()} all projects and repositories which
371+
* are in the descendant groups or in the group itself</li>
372+
* </ul>
373+
*/
374+
protected void discoverGroups() {
375+
Set<String> groups = new TreeSet<>();
376+
for (String x : forGroups()) {
377+
/**
378+
* Full group discovery takes place here. All projects/repositories
379+
* in the group are added into "forProjects" and all subgroups
380+
* (including projects/repositories) and parent groups (excluding
381+
* the projects/repositories) are added into "forGroups".
382+
*/
383+
Group g;
384+
if ((g = Group.getByName(x)) != null) {
385+
forProjects().addAll(g.getAllProjects().stream().map((t) -> t.getName()).collect(Collectors.toSet()));
386+
groups.addAll(g.getRelatedGroups().stream().map((t) -> t.getName()).collect(Collectors.toSet()));
387+
groups.add(x);
388+
}
389+
}
390+
setForGroups(groups);
391+
}
392+
359393
/**
360394
* Check if the plugin exists and has not failed while loading.
361395
*

src/org/opensolaris/opengrok/authorization/AuthorizationPlugin.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.logging.Level;
2828
import java.util.logging.Logger;
2929
import javax.servlet.http.HttpServletRequest;
30+
import org.opensolaris.opengrok.configuration.Group;
3031
import org.opensolaris.opengrok.configuration.Nameable;
3132
import org.opensolaris.opengrok.configuration.Project;
3233
import org.opensolaris.opengrok.logger.LoggerFactory;
@@ -111,6 +112,9 @@ public synchronized void load(Map<String, Object> parameters) {
111112
s.putAll(parameters);
112113
s.putAll(getSetup());
113114

115+
// fill properly the "forGroups" and "forProjects" fields
116+
discoverGroups();
117+
114118
try {
115119
plugin.load(s);
116120
setWorking();

src/org/opensolaris/opengrok/authorization/AuthorizationStack.java

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,9 @@
2525
import java.util.ArrayList;
2626
import java.util.List;
2727
import java.util.Map;
28-
import java.util.Set;
2928
import java.util.TreeMap;
30-
import java.util.TreeSet;
3129
import java.util.logging.Level;
3230
import java.util.logging.Logger;
33-
import java.util.stream.Collectors;
34-
import org.opensolaris.opengrok.configuration.Group;
3531
import org.opensolaris.opengrok.configuration.Nameable;
3632
import org.opensolaris.opengrok.logger.LoggerFactory;
3733

@@ -137,22 +133,8 @@ public void load(Map<String, Object> parameters) {
137133
getFlag().toString().toUpperCase(),
138134
getName()});
139135

140-
Set<String> groups = new TreeSet<>();
141-
for (String x : forGroups()) {
142-
/**
143-
* Full group discovery takes place here. All projects/repositories
144-
* in the group are added into "forProjects" and all subgroups
145-
* (including projects/repositories) and parent groups (excluding
146-
* the projects/repositories) are added into "forGroups".
147-
*/
148-
Group g;
149-
if ((g = Group.getByName(x)) != null) {
150-
forProjects().addAll(g.getAllProjects().stream().map((t) -> t.getName()).collect(Collectors.toSet()));
151-
groups.addAll(g.getRelatedGroups().stream().map((t) -> t.getName()).collect(Collectors.toSet()));
152-
groups.add(x);
153-
}
154-
}
155-
setForGroups(groups);
136+
// fill properly the "forGroups" and "forProjects" fields
137+
discoverGroups();
156138

157139
setWorking();
158140

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
22+
*/
23+
package org.opensolaris.opengrok.authorization;
24+
25+
import java.util.Arrays;
26+
import java.util.Set;
27+
import java.util.TreeMap;
28+
import java.util.TreeSet;
29+
import org.junit.After;
30+
import org.junit.Before;
31+
import org.junit.Test;
32+
import org.opensolaris.opengrok.configuration.Group;
33+
import org.opensolaris.opengrok.configuration.Project;
34+
import org.opensolaris.opengrok.configuration.RuntimeEnvironment;
35+
36+
import static org.junit.Assert.assertEquals;
37+
38+
/**
39+
*
40+
* @author Krystof Tulinger
41+
*/
42+
public class AuthorizationStackTest {
43+
44+
private Set<Group> envGroups;
45+
46+
@Before
47+
public void setUp() {
48+
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
49+
envGroups = env.getGroups();
50+
env.setGroups(new TreeSet<>());
51+
}
52+
53+
@After
54+
public void tearDown() {
55+
RuntimeEnvironment.getInstance().setGroups(envGroups);
56+
}
57+
58+
@Test
59+
public void testForGroupsAndForProjectsDiscovery() {
60+
Group g1, g2, g3;
61+
AuthorizationEntity stack;
62+
63+
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
64+
65+
/**
66+
* Structure<br>
67+
* <pre>
68+
* G1 + P1
69+
* + P2
70+
* + P3
71+
* + G2
72+
* + P4
73+
* + P5
74+
* + P6
75+
* + P7
76+
* G3 + P8
77+
* + P9
78+
* </pre>
79+
*/
80+
g1 = new Group();
81+
g1.setName("group 1");
82+
g1.addProject(new Project("project 1"));
83+
g1.addProject(new Project("project 2"));
84+
g1.addProject(new Project("project 3"));
85+
env.getGroups().add(g1);
86+
g2 = new Group();
87+
g2.setName("group 2");
88+
g2.addProject(new Project("project 4"));
89+
g2.addProject(new Project("project 5"));
90+
g2.addProject(new Project("project 6"));
91+
g2.addProject(new Project("project 7"));
92+
g1.addGroup(g2);
93+
env.getGroups().add(g2);
94+
g3 = new Group();
95+
g3.setName("group 3");
96+
g3.addProject(new Project("project 8"));
97+
g3.addProject(new Project("project 9"));
98+
env.getGroups().add(g3);
99+
100+
// add g1 and all descendants their projects
101+
stack = new AuthorizationStack(AuthControlFlag.REQUIRED, "");
102+
stack.setForGroups(new TreeSet<>());
103+
stack.setForGroups("group 1");
104+
stack.load(new TreeMap<>());
105+
106+
assertEquals(new TreeSet<>(Arrays.asList(new String[]{"group 1", "group 2"})), stack.forGroups());
107+
assertEquals(new TreeSet<>(Arrays.asList(new String[]{"project 1", "project 2", "project 3",
108+
"project 4", "project 5", "project 6", "project 7"})), stack.forProjects());
109+
110+
// add group2, its parent g1 and g2 projects
111+
stack = new AuthorizationStack(AuthControlFlag.REQUIRED, "");
112+
stack.setForGroups(new TreeSet<>());
113+
stack.setForGroups("group 2");
114+
stack.load(new TreeMap<>());
115+
116+
assertEquals(new TreeSet<>(Arrays.asList(new String[]{"group 1", "group 2"})), stack.forGroups());
117+
assertEquals(new TreeSet<>(Arrays.asList(new String[]{"project 4", "project 5", "project 6", "project 7"})), stack.forProjects());
118+
119+
// add only g3 and its projects
120+
stack = new AuthorizationStack(AuthControlFlag.REQUIRED, "");
121+
stack.setForGroups(new TreeSet<>());
122+
stack.setForGroups("group 3");
123+
stack.load(new TreeMap<>());
124+
125+
assertEquals(new TreeSet<>(Arrays.asList(new String[]{"group 3"})), stack.forGroups());
126+
assertEquals(new TreeSet<>(Arrays.asList(new String[]{"project 8", "project 9"})), stack.forProjects());
127+
}
128+
}

0 commit comments

Comments
 (0)