Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,11 @@ private String makeImg(String img, String tooltip, boolean inPlugin) {
@Override public boolean add(AuthorizationStrategy strategy, User user, Permission perm) {
if (strategy instanceof GlobalMatrixAuthorizationStrategy) {
((GlobalMatrixAuthorizationStrategy) strategy).add(perm, user.getId());
try {
Jenkins.getInstance().save();
} catch (IOException ioe) {
LOGGER.log(Level.WARNING, "Failed to save Jenkins after adding permission for user: " + user.getId(), ioe);
}
return true;
} else {
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.jenkinsci.plugins.matrixauth;

import com.gargoylesoftware.htmlunit.html.HtmlPage;
import hudson.model.User;
import hudson.security.ACL;
import hudson.security.ACLContext;
import hudson.security.GlobalMatrixAuthorizationStrategy;
import hudson.security.HudsonPrivateSecurityRealm;
import hudson.security.pages.SignupPage;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, never heard of this before. Once the private realm gets moved into a plugin (?), I guess this should go into a test-jar.

import jenkins.model.Jenkins;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runners.model.Statement;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.RestartableJenkinsRule;

public class PermissionAdderTest {

@Rule
public RestartableJenkinsRule r = new RestartableJenkinsRule();

@Test
@Issue("JENKINS-20520")
public void ensureSavingAfterInitialUser() {
r.addStep(new Statement() {
@Override
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW once you are on 2.60.x+ you can make this a lot prettier with lambdas.

public void evaluate() throws Throwable {
r.j.jenkins.setSecurityRealm(new HudsonPrivateSecurityRealm(true));
r.j.jenkins.setAuthorizationStrategy(new GlobalMatrixAuthorizationStrategy());
r.j.jenkins.save();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW in 2.51+ this is unnecessary: jenkinsci/jenkins#2790

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want this to be explicit.


JenkinsRule.WebClient wc = r.j.createWebClient();
SignupPage signup = new SignupPage(wc.goTo("signup"));
signup.enterUsername("alice");
signup.enterPassword("alice");
signup.enterFullName("Alice User");
HtmlPage success = signup.submit(r.j);

Assert.assertTrue(r.j.jenkins.getACL().hasPermission(User.get("alice").impersonate(), Jenkins.ADMINISTER));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failing in jenkinsci/bom#359 for reasons I am still struggling to understand: User.get("alice", false, Collections.emptyMap()) returns null. Only when jth.jenkins-war.path is set to the megawar.

}
});
r.addStep(new Statement() {
@Override
public void evaluate() throws Throwable {
Assert.assertTrue(r.j.jenkins.getACL().hasPermission(User.get("alice").impersonate(), Jenkins.ADMINISTER));
}
});
}
}