Skip to content

Commit

Permalink
Merge pull request #26 from jenkinsci/permissions_fix
Browse files Browse the repository at this point in the history
Fix permissions for UnprotectedRootAction
  • Loading branch information
froque authored Nov 12, 2021
2 parents de25b82 + e4502fd commit 9fc89d8
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,38 @@ public class PluginUsageView implements RootAction{
public static final Permission PLUGIN_VIEW = new Permission(PERMISSIONS,
"PluginView", Messages._PluginUsageView_PluginViewPermission_Description(), Jenkins.ADMINISTER, PermissionScope.JENKINS);

@Override
public String getDisplayName() {
return "Plugin Usage";
if (Jenkins.get().hasPermission(PluginUsageView.PLUGIN_VIEW)){
return "Plugin Usage";
}
return null;
}

@Override
public String getIconFileName() {
Jenkins.getInstance().checkPermission(PluginUsageView.PLUGIN_VIEW);
return "plugin.png";
if (Jenkins.get().hasPermission(PluginUsageView.PLUGIN_VIEW)){
return "plugin.png";
}
return null;
}

@Override
public String getUrlName() {
Jenkins.getInstance().checkPermission(PluginUsageView.PLUGIN_VIEW);
return "pluginusage";
if (Jenkins.get().hasPermission(PluginUsageView.PLUGIN_VIEW)){
return "pluginusage";
}
return null;
}

public PluginUsageModel getData() {
Jenkins.getInstance().checkPermission(PluginUsageView.PLUGIN_VIEW);
Jenkins.get().checkPermission(PluginUsageView.PLUGIN_VIEW);
PluginUsageModel pluginUsageModel = new PluginUsageModel();
return pluginUsageModel;
}

public Api getApi() {
Jenkins.getInstance().checkPermission(PluginUsageView.PLUGIN_VIEW);
Jenkins.get().checkPermission(PluginUsageView.PLUGIN_VIEW);
return new Api(getData());
}
}
78 changes: 57 additions & 21 deletions src/test/java/org/jenkinsci/plugins/pluginusage/PluginUsageIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.google.common.collect.Lists;
import org.jenkinsci.plugins.pluginusage.api.Plugin;
Expand Down Expand Up @@ -37,6 +40,17 @@ public class PluginUsageIT {
private JenkinsClient client;
private final int maxTimeBackoffMillis = 5 * 60 * 1000;

Function<PluginUsage, List<String>> pluginNamesExtractor = p -> p.getJobsPerPlugin()
.stream()
.map(PluginProjects::getPlugin)
.map(Plugin::getShortName)
.collect(Collectors.toList());

Function<PluginUsage, List<List<Project>>> ProjectsExtractor = p -> p.getJobsPerPlugin()
.stream()
.map(PluginProjects::getProjects)
.collect(Collectors.toList());

@BeforeClass
public static void setupAll(){
assumeFalse(isWindows());
Expand Down Expand Up @@ -64,7 +78,8 @@ public void freestyle() {
new Project("freestyle1")
))
));
assertEquals(expected, actual);
assertEquals(pluginNamesExtractor.apply(expected), pluginNamesExtractor.apply(actual));
assertEquals(ProjectsExtractor.apply(expected), ProjectsExtractor.apply(actual));
}

@Test
Expand Down Expand Up @@ -92,7 +107,8 @@ public void conditionalSingle() {
new PluginProjects(
new Plugin("visual-basic-6", "1.4"), Lists.newArrayList(new Project("conditional-single1")))
));
assertEquals(expected, actual);
assertEquals(pluginNamesExtractor.apply(expected), pluginNamesExtractor.apply(actual));
assertEquals(ProjectsExtractor.apply(expected), ProjectsExtractor.apply(actual));
}

@Test
Expand Down Expand Up @@ -120,7 +136,8 @@ public void conditionalMultiple() {
new PluginProjects(
new Plugin("visual-basic-6", "1.4"), Lists.newArrayList(new Project("conditional-multiple1")))
));
assertEquals(expected, actual);
assertEquals(pluginNamesExtractor.apply(expected), pluginNamesExtractor.apply(actual));
assertEquals(ProjectsExtractor.apply(expected), ProjectsExtractor.apply(actual));
}

@Test
Expand All @@ -145,7 +162,8 @@ public void parameters() {
new PluginProjects(
new Plugin("mailer", "1.34"), Lists.newArrayList())
));
assertEquals(expected, actual);
assertEquals(pluginNamesExtractor.apply(expected), pluginNamesExtractor.apply(actual));
assertEquals(ProjectsExtractor.apply(expected), ProjectsExtractor.apply(actual));
}

@Test
Expand All @@ -170,7 +188,8 @@ public void promotions() throws MalformedURLException, URISyntaxException {
new PluginProjects(
new Plugin("visual-basic-6", "1.4"), Lists.newArrayList(new Project("promotion-job1")))
));
assertEquals(expected, actual);
assertEquals(pluginNamesExtractor.apply(expected), pluginNamesExtractor.apply(actual));
assertEquals(ProjectsExtractor.apply(expected), ProjectsExtractor.apply(actual));
}

@Test
Expand All @@ -190,7 +209,8 @@ public void buildWrappers() {
new PluginProjects(
new Plugin("visual-basic-6", "1.4"), Lists.newArrayList(new Project("timestamper1")))
));
assertEquals(expected, actual);
assertEquals(pluginNamesExtractor.apply(expected), pluginNamesExtractor.apply(actual));
assertEquals(ProjectsExtractor.apply(expected), ProjectsExtractor.apply(actual));
}

@Test
Expand All @@ -207,7 +227,8 @@ public void publishers() {
new PluginProjects(
new Plugin("junit", "1.53"), Lists.newArrayList(new Project("publisher1")))
));
assertEquals(expected, actual);
assertEquals(pluginNamesExtractor.apply(expected), pluginNamesExtractor.apply(actual));
assertEquals(ProjectsExtractor.apply(expected), ProjectsExtractor.apply(actual));
}

@Test
Expand All @@ -230,7 +251,8 @@ public void publishersPromotions() throws URISyntaxException, MalformedURLExcept
new PluginProjects(
new Plugin("promoted-builds", "3.10"), Lists.newArrayList(new Project("publisher2")))
));
assertEquals(expected, actual);
assertEquals(pluginNamesExtractor.apply(expected), pluginNamesExtractor.apply(actual));
assertEquals(ProjectsExtractor.apply(expected), ProjectsExtractor.apply(actual));
}

@Test
Expand All @@ -251,7 +273,8 @@ public void scm() {
new PluginProjects(
new Plugin("mailer", "1.34"), Lists.newArrayList())
));
assertEquals(expected, actual);
assertEquals(pluginNamesExtractor.apply(expected), pluginNamesExtractor.apply(actual));
assertEquals(ProjectsExtractor.apply(expected), ProjectsExtractor.apply(actual));
}

@Test
Expand All @@ -274,7 +297,8 @@ public void maven() {
new PluginProjects(
new Plugin("maven-plugin", "3.15"), Lists.newArrayList(new Project("maven1")))
));
assertEquals(expected, actual);
assertEquals(pluginNamesExtractor.apply(expected), pluginNamesExtractor.apply(actual));
assertEquals(ProjectsExtractor.apply(expected), ProjectsExtractor.apply(actual));
}

@Test
Expand All @@ -300,7 +324,8 @@ public void mavenPreBuilders() {
new PluginProjects(
new Plugin("visual-basic-6", "1.4"), Lists.newArrayList(new Project("maven2")))
));
assertEquals(expected, actual);
assertEquals(pluginNamesExtractor.apply(expected), pluginNamesExtractor.apply(actual));
assertEquals(ProjectsExtractor.apply(expected), ProjectsExtractor.apply(actual));
}

@Test
Expand All @@ -326,7 +351,8 @@ public void mavenPostBuilders() {
new PluginProjects(
new Plugin("visual-basic-6", "1.4"), Lists.newArrayList(new Project("maven3")))
));
assertEquals(expected, actual);
assertEquals(pluginNamesExtractor.apply(expected), pluginNamesExtractor.apply(actual));
assertEquals(ProjectsExtractor.apply(expected), ProjectsExtractor.apply(actual));
}

@Test
Expand Down Expand Up @@ -356,7 +382,9 @@ public void mavenParameter() {
new PluginProjects(
new Plugin("maven-plugin", "3.15"), Lists.newArrayList(new Project("maven4")))
));
assertEquals(expected, actual);

assertEquals(pluginNamesExtractor.apply(expected), pluginNamesExtractor.apply(actual));
assertEquals(ProjectsExtractor.apply(expected), ProjectsExtractor.apply(actual));
}

@Test
Expand Down Expand Up @@ -385,7 +413,8 @@ public void mavenSingleConditionalBuilder() {
new PluginProjects(
new Plugin("visual-basic-6", "1.4"), Lists.newArrayList(new Project("maven5")))
));
assertEquals(expected, actual);
assertEquals(pluginNamesExtractor.apply(expected), pluginNamesExtractor.apply(actual));
assertEquals(ProjectsExtractor.apply(expected), ProjectsExtractor.apply(actual));
}

@Test
Expand Down Expand Up @@ -414,7 +443,8 @@ public void mavenMultiConditionalBuilder() {
new PluginProjects(
new Plugin("visual-basic-6", "1.4"), Lists.newArrayList(new Project("maven6")))
));
assertEquals(expected, actual);
assertEquals(pluginNamesExtractor.apply(expected), pluginNamesExtractor.apply(actual));
assertEquals(ProjectsExtractor.apply(expected), ProjectsExtractor.apply(actual));
}


Expand Down Expand Up @@ -447,7 +477,8 @@ public void mavenPromotions() throws URISyntaxException, MalformedURLException {
new PluginProjects(
new Plugin("visual-basic-6", "1.4"), Lists.newArrayList(new Project("maven7")))
));
assertEquals(expected, actual);
assertEquals(pluginNamesExtractor.apply(expected), pluginNamesExtractor.apply(actual));
assertEquals(ProjectsExtractor.apply(expected), ProjectsExtractor.apply(actual));
}

@Test
Expand All @@ -466,7 +497,8 @@ public void trigger() {
new PluginProjects(
new Plugin("urltrigger", "0.49"), Lists.newArrayList(new Project("trigger1")))
));
assertEquals(expected, actual);
assertEquals(pluginNamesExtractor.apply(expected), pluginNamesExtractor.apply(actual));
assertEquals(ProjectsExtractor.apply(expected), ProjectsExtractor.apply(actual));
}

@Test
Expand Down Expand Up @@ -505,7 +537,8 @@ public void pipeline() {
new PluginProjects(
new Plugin("pipeline-stage-step", "2.5"), Lists.newArrayList())
));
assertEquals(expected, actual);
assertEquals(pluginNamesExtractor.apply(expected), pluginNamesExtractor.apply(actual));
assertEquals(ProjectsExtractor.apply(expected), ProjectsExtractor.apply(actual));
}

@Test
Expand Down Expand Up @@ -547,7 +580,8 @@ public void pipeline2() {
new PluginProjects(
new Plugin("visual-basic-6", "1.4"), Lists.newArrayList(new Project("pipeline2")))
));
assertEquals(expected, actual);
assertEquals(pluginNamesExtractor.apply(expected), pluginNamesExtractor.apply(actual));
assertEquals(ProjectsExtractor.apply(expected), ProjectsExtractor.apply(actual));
}

@Test
Expand All @@ -569,7 +603,8 @@ public void matrix() {
new PluginProjects(
new Plugin("visual-basic-6", "1.4"), Lists.newArrayList(new Project("matrix1")))
));
assertEquals(expected, actual);
assertEquals(pluginNamesExtractor.apply(expected), pluginNamesExtractor.apply(actual));
assertEquals(ProjectsExtractor.apply(expected), ProjectsExtractor.apply(actual));
}

@Test
Expand All @@ -592,6 +627,7 @@ public void otherProjects() {
new Project("freestyle1")
))
));
assertEquals(expected, actual);
assertEquals(pluginNamesExtractor.apply(expected), pluginNamesExtractor.apply(actual));
assertEquals(ProjectsExtractor.apply(expected), ProjectsExtractor.apply(actual));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public Plugin(String shortName, String version) {
this.version = version;
}

public String getShortName() {
return shortName;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@ public String toString() {
.add("projects=" + projects)
.toString();
}

public Plugin getPlugin() {
return plugin;
}

public List<Project> getProjects() {
return projects;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ public String toString() {
.add("jobsPerPlugin=" + jobsPerPlugin)
.toString();
}

public List<PluginProjects> getJobsPerPlugin() {
return jobsPerPlugin;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ public String toString() {
.add("fullName='" + fullName + "'")
.toString();
}

public String getFullName() {
return fullName;
}
}

0 comments on commit 9fc89d8

Please sign in to comment.