Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FIXED JENKINS-19934] Job create permission for RoleBasedProjectNamin…
…gStrategy.
- Loading branch information
Showing
6 changed files
with
139 additions
and
2 deletions.
There are no files selected for viewing
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
89 changes: 89 additions & 0 deletions
89
src/main/java/org/jenkinsci/plugins/rolestrategy/RoleBasedProjectNamingStrategy.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package org.jenkinsci.plugins.rolestrategy; | ||
|
||
import com.michelin.cio.hudson.plugins.rolestrategy.Messages; | ||
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.model.Item; | ||
import hudson.security.AuthorizationStrategy; | ||
import jenkins.model.Jenkins; | ||
import jenkins.model.ProjectNamingStrategy; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
|
||
import java.io.Serializable; | ||
import java.util.ArrayList; | ||
import java.util.Set; | ||
import java.util.SortedMap; | ||
import java.util.logging.Logger; | ||
import java.util.regex.Pattern; | ||
|
||
|
||
public class RoleBasedProjectNamingStrategy extends ProjectNamingStrategy implements Serializable { | ||
// public static final Logger LOGGER = Logger.getLogger(RoleBasedProjectNamingStrategy.class.getName()); | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
private boolean forceExistingJobs; | ||
|
||
@DataBoundConstructor | ||
public RoleBasedProjectNamingStrategy(boolean forceExistingJobs) { | ||
this.forceExistingJobs = forceExistingJobs; | ||
} | ||
|
||
@Override | ||
public void checkName(String name) throws Failure { | ||
boolean matches = false; | ||
ArrayList<String> badList = null; | ||
AuthorizationStrategy auth = Jenkins.getInstance().getAuthorizationStrategy(); | ||
if (auth instanceof RoleBasedAuthorizationStrategy){ | ||
RoleBasedAuthorizationStrategy rbas = (RoleBasedAuthorizationStrategy) auth; | ||
//firstly check global role | ||
SortedMap<Role, Set<String>> gRole = rbas.getGrantedRoles(RoleBasedAuthorizationStrategy.GLOBAL); | ||
for (SortedMap.Entry<Role, Set<String>> entry: gRole.entrySet()){ | ||
if (entry.getKey().hasPermission(Item.CREATE)) | ||
return; | ||
} | ||
// check project role with pattern | ||
SortedMap<Role, Set<String>> roles = rbas.getGrantedRoles(RoleBasedAuthorizationStrategy.PROJECT); | ||
badList = new ArrayList<String>(roles.size()); | ||
for (SortedMap.Entry<Role, Set<String>> entry: roles.entrySet()) { | ||
Role key = entry.getKey(); | ||
if (key.hasPermission(Item.CREATE)) { | ||
String namePattern = key.getPattern().toString(); | ||
if (StringUtils.isNotBlank(namePattern) && StringUtils.isNotBlank(name)) { | ||
if (Pattern.matches(namePattern, name)){ | ||
matches = true; | ||
} else { | ||
badList.add(namePattern); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
if (!matches) { | ||
String error; | ||
if (badList != null && !badList.isEmpty()) | ||
//TODO beatify long outputs? | ||
error = jenkins.model.Messages.Hudson_JobNameConventionNotApplyed(name, badList.toString()); | ||
else | ||
error = Messages.RoleBasedProjectNamingStrategy_NoPermissions(); | ||
throw new Failure(error); | ||
} | ||
} | ||
|
||
public boolean isForceExistingJobs() { | ||
return forceExistingJobs; | ||
} | ||
|
||
@Extension | ||
public static final class DescriptorImpl extends ProjectNamingStrategyDescriptor { | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return Messages.RoleBasedAuthorizationStrategy_DisplayName(); | ||
} | ||
|
||
} | ||
} |
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
7 changes: 7 additions & 0 deletions
7
...resources/org/jenkinsci/plugins/rolestrategy/RoleBasedProjectNamingStrategy/config.groovy
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package org.jenkinsci.plugins.rolestrategy.RoleBasedProjectNamingStrategy | ||
|
||
def f=namespace(lib.FormTagLib) | ||
|
||
f.entry(title:_("forceExistingJobs"), field:"forceExistingJobs") { | ||
f.checkbox(name:"forceExistingJobs") | ||
} |
24 changes: 24 additions & 0 deletions
24
...urces/org/jenkinsci/plugins/rolestrategy/RoleBasedProjectNamingStrategy/config.properties
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# The MIT License | ||
# | ||
# Copyright (c) 2014, Kanstantsin Shautsou | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in | ||
# all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
# THE SOFTWARE. | ||
|
||
description = Description | ||
forceExistingJobs = Force existing |
4 changes: 4 additions & 0 deletions
4
...ain/resources/org/jenkinsci/plugins/rolestrategy/RoleBasedProjectNamingStrategy/help.html
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<p> | ||
Restricts Job creation according to role based settings. | ||
Global role allows create with any name, project role according to defined pattern. | ||
</p> |