Skip to content
Permalink
Browse files
[JENKINS-28298] Added a feature to enable / disable strategies. Imple…
…mented only the configuration page, not yet execution time check.
  • Loading branch information
ikedam committed Jun 30, 2015
1 parent 9365f68 commit 4bc59e08925e3ea63033681c1c461428c69ed098
Showing 4 changed files with 69 additions and 8 deletions.
@@ -63,7 +63,7 @@ protected final XmlFile getConfigFile() {
}

/**
* @return return a page to shown in "Configure Global Security" as a child of {@link ProjectQueueItemAuthenticator}.
* @return descriptors with configuration views in "Configure Global Security" as a child of {@link ProjectQueueItemAuthenticator}.
*/
public String getGlobalSecurityConfigPage() {
for (String cand: getPossibleViewNames("global-security")) {
@@ -107,4 +107,11 @@ public static List<AuthorizeProjectStrategyDescriptor> getDescriptorsForGlobalSe
*/
public void configureFromGlobalSecurity(StaplerRequest req, JSONObject js) throws FormException {
}

/**
* @return this strategy can be enabled by default.
*/
public boolean isEnabledByDefault() {
return true;
}
}
@@ -24,10 +24,13 @@

package org.jenkinsci.plugins.authorizeproject;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import hudson.Extension;
import hudson.model.AbstractProject;
import hudson.model.Descriptor;
import hudson.model.Job;
import hudson.model.Queue;

@@ -39,6 +42,7 @@
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

import jenkins.model.Jenkins;
import jenkins.security.QueueItemAuthenticatorConfiguration;
import jenkins.security.QueueItemAuthenticatorDescriptor;
import jenkins.security.QueueItemAuthenticator;
@@ -47,11 +51,25 @@
* Authorize builds of projects configured with {@link AuthorizeProjectProperty}.
*/
public class ProjectQueueItemAuthenticator extends QueueItemAuthenticator {
private final Map<String,Boolean> strategyEnabledMap;

/**
*
*/
@DataBoundConstructor
@Deprecated
public ProjectQueueItemAuthenticator() {
this(Collections.<String, Boolean>emptyMap());
}

public ProjectQueueItemAuthenticator(Map<String,Boolean> strategyEnableMap) {
this.strategyEnabledMap = strategyEnableMap;
}

public Object readResolve() {
if(strategyEnabledMap == null) {
return new ProjectQueueItemAuthenticator(Collections.<String, Boolean>emptyMap());
}
return this;
}

/**
@@ -76,6 +94,22 @@ public Authentication authenticate(Queue.Item item) {
return prop.authenticate(item);
}

public Map<String, Boolean> getStrategyEnabledMap() {
return strategyEnabledMap;
}

public boolean isStrategyEnabled(Descriptor<?> d)
{
Boolean b = getStrategyEnabledMap().get(d.getId());
if(b != null) {
return b.booleanValue();
}
if(!(d instanceof AuthorizeProjectStrategyDescriptor)) {
return true;
}
return ((AuthorizeProjectStrategyDescriptor)d).isEnabledByDefault();
}

/**
*
*/
@@ -94,6 +128,10 @@ public List<AuthorizeProjectStrategyDescriptor> getDescriptorsForGlobalSecurityC
return AuthorizeProjectStrategyDescriptor.getDescriptorsForGlobalSecurityConfigPage();
}

public List<Descriptor<AuthorizeProjectStrategy>> getAvailableDescriptorList() {
return Jenkins.getInstance().getDescriptorList(AuthorizeProjectStrategy.class);
}

/**
* Creates new {@link ProjectQueueItemAuthenticator} from inputs.
* Additional to that, configure global configurations of {@link AuthorizeProjectStrategy}.
@@ -112,8 +150,9 @@ public ProjectQueueItemAuthenticator newInstance(StaplerRequest req, JSONObject

for (AuthorizeProjectStrategyDescriptor d : getDescriptorsForGlobalSecurityConfigPage()) {
String name = d.getJsonSafeClassName();
JSONObject js = formData.has(name) ? formData.getJSONObject(name) : new JSONObject();
d.configureFromGlobalSecurity(req, js);
if (formData.has(name)) {
d.configureFromGlobalSecurity(req, formData.getJSONObject(name));
}
}

return r;
@@ -424,5 +424,15 @@ public FormValidation doCheckNoNeedReauthentication(@QueryParameter boolean noNe
}
return FormValidation.ok();
}

/**
* {@link SpecificUsersAuthorizationStrategy} should be disabled by default for JENKINS-28298
* @return false
* @see org.jenkinsci.plugins.authorizeproject.AuthorizeProjectStrategyDescriptor#isEnabledByDefault()
*/
@Override
public boolean isEnabledByDefault() {
return false;
}
}
}
@@ -24,10 +24,15 @@ THE SOFTWARE.
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<!-- configurations for AuthorizeProjectStrategy -->
<j:forEach var="d" items="${descriptor.descriptorsForGlobalSecurityConfigPage}">
<j:forEach var="d" items="${descriptor.availableDescriptorList}">
<j:scope>
<j:set var="checked" value="${instance.isStrategyEnabled(d)}" />
<j:set var="instance" value="${d}" />
<f:rowSet name="${d.jsonSafeClassName}">
<st:include page="${d.globalSecurityConfigPage}" from="${d}" />
</f:rowSet>
<j:set var="descriptor" value="${d}" />
<f:optionalBlock name="${d.jsonSafeClassName}" checked="${checked}" help="${d.helpFile}" title="${d.displayName}">
<j:set var="instance" value="${d}" />
<st:include page="${d.globalSecurityConfigPage}" class="${d.clazz}" optional="true" />
</f:optionalBlock>
</j:scope>
</j:forEach>
</j:jelly>

0 comments on commit 4bc59e0

Please sign in to comment.