Skip to content

Commit 44cf5e4

Browse files
authored
[JENKINS-73038] Adapt plugin to allow user with Overall/Manage to configure global settings (#846)
1 parent c34c7a5 commit 44cf5e4

File tree

6 files changed

+39
-6
lines changed

6 files changed

+39
-6
lines changed

pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.jenkins-ci.plugins</groupId>
77
<artifactId>plugin</artifactId>
8-
<version>4.80</version>
8+
<version>4.81</version>
99
<relativePath />
1010
</parent>
1111

@@ -30,6 +30,7 @@
3030
<gitHubRepo>jenkinsci/bitbucket-branch-source-plugin</gitHubRepo>
3131
<jenkins.version>2.401.3</jenkins.version>
3232
<hpi.compatibleSinceVersion>2.0</hpi.compatibleSinceVersion>
33+
<useBeta>true</useBeta> <!-- Jenkins.MANAGE -->
3334
</properties>
3435

3536
<developers>

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketApiUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static ListBoxModel getFromBitbucket(SCMSourceOwner context,
3535
if (repoOwner == null) {
3636
return new ListBoxModel();
3737
}
38-
if (context == null && !Jenkins.get().hasPermission(Jenkins.ADMINISTER) ||
38+
if (context == null && !Jenkins.get().hasPermission(Jenkins.MANAGE) ||
3939
context != null && !context.hasPermission(Item.EXTENDED_READ)) {
4040
return new ListBoxModel(); // not supposed to be seeing this form
4141
}

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/SSHCheckoutTrait.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public ListBoxModel doFillCredentialsIdItems(@CheckForNull @AncestorInPath Item
171171
@QueryParameter String serverUrl,
172172
@QueryParameter String credentialsId) {
173173
if (context == null
174-
? !Jenkins.get().hasPermission(Jenkins.ADMINISTER)
174+
? !Jenkins.get().hasPermission(Jenkins.MANAGE)
175175
: !context.hasPermission(Item.EXTENDED_READ)) {
176176
return new StandardListBoxModel().includeCurrentValue(credentialsId);
177177
}
@@ -202,7 +202,7 @@ public FormValidation doCheckCredentialsId(@CheckForNull @AncestorInPath Item co
202202
@QueryParameter String serverUrl,
203203
@QueryParameter String value) {
204204
if (context == null
205-
? !Jenkins.get().hasPermission(Jenkins.ADMINISTER)
205+
? !Jenkins.get().hasPermission(Jenkins.MANAGE)
206206
: !context.hasPermission(Item.EXTENDED_READ)) {
207207
return FormValidation.ok();
208208
}

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/endpoints/AbstractBitbucketEndpointDescriptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public abstract class AbstractBitbucketEndpointDescriptor extends Descriptor<Abs
5656
@SuppressWarnings("unused")
5757
public ListBoxModel doFillCredentialsIdItems(@QueryParameter String serverUrl) {
5858
Jenkins jenkins = Jenkins.get();
59-
jenkins.checkPermission(Jenkins.ADMINISTER);
59+
jenkins.checkPermission(Jenkins.MANAGE);
6060
StandardListBoxModel result = new StandardListBoxModel();
6161
result.includeMatchingAs(
6262
ACL.SYSTEM,

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/endpoints/BitbucketEndpointConfiguration.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import hudson.ExtensionList;
3030
import hudson.Util;
3131
import hudson.security.ACL;
32+
import hudson.security.Permission;
3233
import hudson.util.ListBoxModel;
3334
import java.net.URI;
3435
import java.net.URISyntaxException;
@@ -79,6 +80,11 @@ public static BitbucketEndpointConfiguration get() {
7980
return ExtensionList.lookup(GlobalConfiguration.class).get(BitbucketEndpointConfiguration.class);
8081
}
8182

83+
@NonNull
84+
@Override
85+
public Permission getRequiredGlobalConfigPagePermission() {
86+
return Jenkins.MANAGE;
87+
}
8288
/**
8389
* Called from a {@code readResolve()} method only to convert the old {@code bitbucketServerUrl} field into the new
8490
* {@code serverUrl} field. When called from {@link ACL#SYSTEM} this will update the configuration with the
@@ -155,7 +161,7 @@ public synchronized List<AbstractBitbucketEndpoint> getEndpoints() {
155161
* @param endpoints the list of endpoints.
156162
*/
157163
public synchronized void setEndpoints(@CheckForNull List<? extends AbstractBitbucketEndpoint> endpoints) {
158-
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
164+
Jenkins.get().checkPermission(Jenkins.MANAGE);
159165
List<AbstractBitbucketEndpoint> eps = new ArrayList<>(Util.fixNull(endpoints));
160166
// remove duplicates and empty urls
161167
Set<String> serverUrls = new HashSet<>();

src/test/java/com/cloudbees/jenkins/plugins/bitbucket/endpoints/BitbucketEndpointConfigurationTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
3333
import com.google.common.io.Resources;
3434
import hudson.XmlFile;
35+
import hudson.model.User;
3536
import hudson.security.ACL;
3637
import hudson.security.ACLContext;
3738
import hudson.security.AuthorizationStrategy;
@@ -49,7 +50,9 @@
4950
import org.junit.Before;
5051
import org.junit.ClassRule;
5152
import org.junit.Test;
53+
import org.junit.jupiter.api.Assertions;
5254
import org.jvnet.hudson.test.JenkinsRule;
55+
import org.jvnet.hudson.test.MockAuthorizationStrategy;
5356

5457
import static org.hamcrest.Matchers.contains;
5558
import static org.hamcrest.Matchers.hasSize;
@@ -129,6 +132,29 @@ public void given__newInstance__when__configuredAsAnon__then__permissionError()
129132
}
130133

131134
@Test
135+
public void given__newInstance__when__configuredAsManage__then__OK() {
136+
BitbucketEndpointConfiguration instance = new BitbucketEndpointConfiguration();
137+
try {
138+
j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
139+
MockAuthorizationStrategy mockStrategy = new MockAuthorizationStrategy();
140+
mockStrategy.grant(Jenkins.MANAGE).onRoot().to("admin");
141+
j.jenkins.setAuthorizationStrategy(mockStrategy);
142+
try (ACLContext context = ACL.as(User.get("admin"))) {
143+
instance.setEndpoints(Arrays.<AbstractBitbucketEndpoint>asList(
144+
new BitbucketCloudEndpoint(true, "first"),
145+
new BitbucketCloudEndpoint(true, "second"),
146+
new BitbucketCloudEndpoint(true, "third")));
147+
assertThat(instance.getEndpoints(), contains(instanceOf(BitbucketCloudEndpoint.class)));
148+
assertThat(instance.getEndpoints().get(0).getCredentialsId(), is("first"));
149+
} catch (RuntimeException x) {
150+
Assertions.fail("No exception should be thrown");
151+
}
152+
} finally {
153+
j.jenkins.setAuthorizationStrategy(AuthorizationStrategy.UNSECURED);
154+
}
155+
}
156+
157+
@Test
132158
public void given__newInstance__when__configuredWithServerUsingCloudUrl__then__convertedToCloud() {
133159
BitbucketEndpointConfiguration instance = new BitbucketEndpointConfiguration();
134160
assumeThat(instance.getEndpoints().get(0).getCredentialsId(), not(is("dummy")));

0 commit comments

Comments
 (0)