-
-
Save KostyaSha/0473936bdcc87f7d36f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.jenkinsci.plugins; | |
import com.michelin.cio.hudson.plugins.rolestrategy.Role; | |
import com.michelin.cio.hudson.plugins.rolestrategy.RoleBasedAuthorizationStrategy; | |
import hudson.Extension; | |
import hudson.model.Failure; | |
import hudson.security.AuthorizationStrategy; | |
import jenkins.model.Jenkins; | |
import jenkins.model.Messages; | |
import jenkins.model.ProjectNamingStrategy; | |
import org.apache.commons.lang.StringUtils; | |
import org.kohsuke.stapler.DataBoundConstructor; | |
import java.io.Serializable; | |
import java.util.Set; | |
import java.util.SortedMap; | |
import java.util.regex.Pattern; | |
/** | |
* Created with IntelliJ IDEA. | |
* User: integer | |
* Date: 5/20/14 | |
* Time: 1:00 | |
*/ | |
class RoleBasedProjectNamingStrategy extends ProjectNamingStrategy implements Serializable { | |
private static final long serialVersionUID = 1L; | |
private final String description; | |
private boolean forceExistingJobs; | |
@DataBoundConstructor | |
public RoleBasedProjectNamingStrategy(String description, boolean forceExistingJobs) { | |
this.description = description; | |
this.forceExistingJobs = forceExistingJobs; | |
} | |
@Override | |
public void checkName(String name) throws Failure { | |
//get namePattern from role based plugin | |
AuthorizationStrategy auth = Jenkins.getInstance().getAuthorizationStrategy(); | |
if (auth instanceof RoleBasedAuthorizationStrategy){ | |
RoleBasedAuthorizationStrategy rbas = (RoleBasedAuthorizationStrategy) auth; | |
// get and check patterns | |
SortedMap<Role, Set<String>> roles = rbas.getGrantedRoles(RoleBasedAuthorizationStrategy.PROJECT); | |
for (SortedMap.Entry<Role, Set<String>> entry: roles.entrySet()) { | |
Role key = entry.getKey(); | |
// Set<String> value = entry.getValue(); | |
String namePattern = key.getPattern().toString(); | |
if (StringUtils.isNotBlank(namePattern) && StringUtils.isNotBlank(name)) { | |
if (!Pattern.matches(namePattern, name)) { | |
throw new Failure(StringUtils.isEmpty(description) ? | |
Messages.Hudson_JobNameConventionNotApplyed(name, namePattern) : description); | |
} | |
} | |
} | |
} | |
} | |
public String getDescription() { | |
return description; | |
} | |
public boolean isForceExistingJobs() { | |
return forceExistingJobs; | |
} | |
@Extension | |
public static final class DescriptorImpl extends ProjectNamingStrategyDescriptor { | |
@Override | |
public String getDisplayName() { | |
return "Role base project naming strategy"; | |
} | |
@Override | |
public String getHelpFile() { | |
return "/help/system-config/roleBasedJobNamingStrategy.html"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment