Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--hudson-core/src/main/java/hudson/cli/CopyJobCommand.java13
-rw-r--r--hudson-core/src/main/java/hudson/cli/CreateJobCommand.java14
-rw-r--r--hudson-core/src/main/java/hudson/cli/UpdateJobCommand.java73
-rw-r--r--hudson-core/src/main/java/hudson/model/Hudson.java64
-rw-r--r--hudson-core/src/main/java/hudson/model/WorkspaceCleanupThread.java9
-rw-r--r--hudson-core/src/main/java/org/eclipse/hudson/security/team/Team.java2
-rw-r--r--hudson-core/src/main/java/org/eclipse/hudson/security/team/TeamBasedAuthorizationStrategy.java4
-rw-r--r--hudson-core/src/main/java/org/eclipse/hudson/security/team/TeamJobListener.java14
-rw-r--r--hudson-core/src/main/java/org/eclipse/hudson/security/team/TeamManager.java122
-rw-r--r--hudson-core/src/test/java/hudson/model/FreeStyleProjectTest.java8
10 files changed, 219 insertions, 104 deletions
diff --git a/hudson-core/src/main/java/hudson/cli/CopyJobCommand.java b/hudson-core/src/main/java/hudson/cli/CopyJobCommand.java
index 2a5458c6..c6b8b49d 100644
--- a/hudson-core/src/main/java/hudson/cli/CopyJobCommand.java
+++ b/hudson-core/src/main/java/hudson/cli/CopyJobCommand.java
@@ -20,7 +20,10 @@ import hudson.model.Hudson;
import hudson.model.Job;
import hudson.model.TopLevelItem;
import hudson.Extension;
+import static hudson.cli.UpdateJobCommand.ensureJobInTeam;
+import static hudson.cli.UpdateJobCommand.validateTeam;
import hudson.model.Item;
+import org.eclipse.hudson.security.team.Team;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;
@@ -40,12 +43,19 @@ public class CopyJobCommand extends CLICommand {
public TopLevelItem src;
@Argument(metaVar = "DST", usage = "Name of the new job to be created.", index = 1, required = true)
public String dst;
+ @Argument(metaVar = "TEAM", usage = "Team to create job in", index = 1, required = false)
+ public String team;
@Option(name = "-fs", aliases = {"--force-save"}, usage = "Force saving the destination job in order to enable build functionality.")
public boolean forceSave;
protected int run() throws Exception {
Hudson h = Hudson.getInstance();
h.checkPermission(Item.CREATE);
+ Team targetTeam = validateTeam(team, true, stderr);
+
+ if (team != null && targetTeam == null) {
+ return -1;
+ }
if (h.getItem(dst) != null) {
stderr.println("Job '" + dst + "' already exists");
@@ -53,7 +63,8 @@ public class CopyJobCommand extends CLICommand {
}
h.copy(src, dst);
- Job newJob = (Job) Hudson.getInstance().getItem(dst);
+ TopLevelItem newJob = Hudson.getInstance().getItem(dst);
+ ensureJobInTeam(newJob, targetTeam, dst, stderr);
if (forceSave && null != newJob) {
newJob.save();
}
diff --git a/hudson-core/src/main/java/hudson/cli/CreateJobCommand.java b/hudson-core/src/main/java/hudson/cli/CreateJobCommand.java
index 5d35c2d1..cd9d189f 100644
--- a/hudson-core/src/main/java/hudson/cli/CreateJobCommand.java
+++ b/hudson-core/src/main/java/hudson/cli/CreateJobCommand.java
@@ -18,7 +18,11 @@ package hudson.cli;
import hudson.model.Hudson;
import hudson.Extension;
+import static hudson.cli.UpdateJobCommand.ensureJobInTeam;
+import static hudson.cli.UpdateJobCommand.validateTeam;
import hudson.model.Item;
+import hudson.model.TopLevelItem;
+import org.eclipse.hudson.security.team.Team;
import org.kohsuke.args4j.Argument;
/**
@@ -35,17 +39,25 @@ public class CreateJobCommand extends CLICommand {
}
@Argument(metaVar = "NAME", usage = "Name of the job to create")
public String name;
+ @Argument(metaVar = "TEAM", usage = "Team to create job in", index = 1, required = false)
+ public String team;
protected int run() throws Exception {
Hudson h = Hudson.getInstance();
h.checkPermission(Item.CREATE);
+ Team targetTeam = validateTeam(team, true, stderr);
+
+ if (team != null && targetTeam == null) {
+ return -1;
+ }
if (h.getItem(name) != null) {
stderr.println("Job '" + name + "' already exists");
return -1;
}
- h.createProjectFromXML(name, stdin);
+ TopLevelItem newItem = h.createProjectFromXML(name, stdin);
+ ensureJobInTeam(newItem, targetTeam, name, stderr);
return 0;
}
}
diff --git a/hudson-core/src/main/java/hudson/cli/UpdateJobCommand.java b/hudson-core/src/main/java/hudson/cli/UpdateJobCommand.java
index 41eb1285..a11778cd 100644
--- a/hudson-core/src/main/java/hudson/cli/UpdateJobCommand.java
+++ b/hudson-core/src/main/java/hudson/cli/UpdateJobCommand.java
@@ -26,7 +26,10 @@ import hudson.model.TopLevelItem;
import hudson.util.IOUtils;
import java.io.File;
import java.io.IOException;
+import java.io.PrintStream;
+import org.eclipse.hudson.security.team.Team;
import org.eclipse.hudson.security.team.TeamManager;
+import org.eclipse.hudson.security.team.TeamManager.TeamNotFoundException;
import org.kohsuke.args4j.Argument;
/**
@@ -45,11 +48,18 @@ public class UpdateJobCommand extends CLICommand {
public String name;
@Argument(metaVar = "CREATE", usage = "Create the job if needed", index = 1, required = true)
public Boolean create;
+ @Argument(metaVar = "TEAM", usage = "Team to create job in", index = 1, required = false)
+ public String team;
protected int run() throws Exception {
Hudson h = Hudson.getInstance();
TeamManager teamManager = Hudson.getInstance().getTeamManager();
TopLevelItem item = h.getItem(name);
+ Team targetTeam = validateTeam(team, create, stderr);
+
+ if (team != null && targetTeam == null) {
+ return -1;
+ }
if (item == null && !create) {
stderr.println("Job '" + name + "' does not exist and create is set to false");
@@ -58,14 +68,12 @@ public class UpdateJobCommand extends CLICommand {
if (item == null) {
h.checkPermission(Item.CREATE);
- h.createProjectFromXML(name, stdin);
+ TopLevelItem newItem = h.createProjectFromXML(name, stdin);
+ ensureJobInTeam(newItem, targetTeam, name, stderr);
} else {
try {
h.checkPermission(Job.CONFIGURE);
- String jobsFolderName = "jobs";
- if (teamManager != null){
- jobsFolderName = teamManager.getJobsFolderName(item.getName());
- }
+ String jobsFolderName = teamManager.getJobsFolderName(item.getName());
File rootDirOfJob = new File(new File(h.getRootDir(), jobsFolderName), name);
// place it as config.xml
File configXml = Items.getConfigFile(rootDirOfJob).getFile();
@@ -78,4 +86,59 @@ public class UpdateJobCommand extends CLICommand {
}
return 0;
}
+
+ /**
+ * If job is not in targetTeam, move it there.
+ *
+ * @param item job
+ * @param targetTeam desired team or null if none
+ * @param name job name requested
+ * @param stderr
+ * @throws Exception
+ */
+ public static void ensureJobInTeam(TopLevelItem item, Team targetTeam, String name, PrintStream stderr) throws Exception {
+ if (targetTeam != null) {
+ try {
+ Hudson.getInstance().getTeamManager().ensureJobInTeam(item, targetTeam);
+
+ } catch (IOException e) {
+ item.delete();
+ stderr.println("Unable to create job "+name+" in team "+targetTeam.getName()+" due to IOException");
+ throw e;
+ }
+ }
+ }
+
+ /**
+ * Validate team exists and user can access it.
+ *
+ * @param team team name
+ * @param create true if create new job
+ * @param stderr
+ * @return
+ */
+ public static Team validateTeam(String team, boolean create, PrintStream stderr) {
+ Hudson h = Hudson.getInstance();
+ TeamManager teamManager = h.getTeamManager();
+ Team targetTeam = null;
+ if (team != null) {
+ if (!create) {
+ stderr.println("team may only be used for create - for update use fully qualified name");
+ } else if (!teamManager.isTeamManagementEnabled()) {
+ stderr.println("team may not be specified unless team management is enabled");
+ } else {
+ try {
+ // check team exists first for better error message
+ targetTeam = teamManager.findTeam(team);
+ if (!teamManager.isCurrentUserHasAccessToTeam(team)) {
+ stderr.println("Current user does not have access to team "+team);
+ targetTeam = null;
+ }
+ } catch (TeamNotFoundException e) {
+ stderr.println("Team "+team+" does not exist");
+ }
+ }
+ }
+ return targetTeam;
+ }
}
diff --git a/hudson-core/src/main/java/hudson/model/Hudson.java b/hudson-core/src/main/java/hudson/model/Hudson.java
index a9afd841..1d731ee3 100644
--- a/hudson-core/src/main/java/hudson/model/Hudson.java
+++ b/hudson-core/src/main/java/hudson/model/Hudson.java
@@ -798,50 +798,28 @@ public final class Hudson extends Node implements ItemGroup<TopLevelItem>, Stapl
}
/**
- * TeamManagement is returned only if the Team based authorization is set.
- * @return TeamManager
+ * @return true if team management is enabled
* @since 3.1.0
*/
public boolean isTeamManagementEnabled() {
- return getTeamManager() != null;
+ return teamManager.isTeamManagementEnabled();
}
/**
- * TeamManager is returned only if the Team based authorization is set.
+ * TeamManager is returned whether or not Team based authorization is set.
+ *
* @return TeamManager
* @since 3.1.0
*/
public TeamManager getTeamManager() {
- HudsonSecurityManager hudsonSecurityManager = HudsonSecurityEntitiesHolder.getHudsonSecurityManager();
- if (hudsonSecurityManager != null) {
- AuthorizationStrategy authorizationStrategy = hudsonSecurityManager.getAuthorizationStrategy();
- if (authorizationStrategy instanceof TeamBasedAuthorizationStrategy) {
- return teamManager;
- }
- }
- return null;
- }
-
- /**
- * TeamManager is returned only if the Team based authorization strategy is set.
- * However, we need the team manager to set the initial system admin,
- * before the Team based authorization strategy is set. Only for internal purpose
- *
- * @deprecated Internal purpose only. Do not use. Use getTeamManager() for general purpose
- */
- public TeamManager getTeamManager(boolean byPass) {
- if (byPass) {
- return teamManager;
- } else {
- return getTeamManager();
- }
+ return teamManager;
}
/**
* Do not use this API, for internal purpose only
* @since 3.1.0
*/
- public void replaceItemId(String oldItemName, String newItemName){
+ public void replaceItem(String oldItemName, String newItemName){
if (items.containsKey(oldItemName)){
TopLevelItem item = items.get(oldItemName);
items.remove(oldItemName);
@@ -2174,18 +2152,12 @@ public final class Hudson extends Node implements ItemGroup<TopLevelItem>, Stapl
@Override
public File getRootDirFor(TopLevelItem child) {
- if (getTeamManager() != null) {
- String jobsFolderName = getTeamManager().getJobsFolderName(child.getName());
- return new File(new File(getRootDir(), jobsFolderName), child.getName());
- }
- return getRootDirFor(child.getName());
+ String jobsFolderName = getTeamManager().getJobsFolderName(child.getName());
+ return new File(new File(getRootDir(), jobsFolderName), child.getName());
}
private File getRootDirFor(String jobName) {
- String jobsFolderName = "jobs";
- if (getTeamManager() != null) {
- jobsFolderName = getTeamManager().getJobsFolderName(jobName);
- }
+ String jobsFolderName = getTeamManager().getJobsFolderName(jobName);
return new File(new File(getRootDir(), jobsFolderName), jobName);
}
@@ -2379,15 +2351,17 @@ public final class Hudson extends Node implements ItemGroup<TopLevelItem>, Stapl
}
File[] jobsRootDirs;
- if (getTeamManager() != null) {
+ //Winston - most alternatives duplicate logic in getJobsRootFolders
+ // but this one seems different. Please check.
+ //if (getTeamManager() != null) {
jobsRootDirs = getTeamManager().getJobsRootFolders();
- } else {
- jobsRootDirs = projectsDir.listFiles(new FileFilter() {
- public boolean accept(File child) {
- return child.isDirectory() && Items.getConfigFile(child).exists();
- }
- });
- }
+ //} else {
+ // jobsRootDirs = projectsDir.listFiles(new FileFilter() {
+ // public boolean accept(File child) {
+ // return child.isDirectory() && Items.getConfigFile(child).exists();
+ // }
+ // });
+ //}
TaskGraphBuilder g = new TaskGraphBuilder();
Handle loadHudson = g.requires(EXTENSIONS_AUGMENTED).attains(JOB_LOADED).add("Loading global config", new Executable() {
diff --git a/hudson-core/src/main/java/hudson/model/WorkspaceCleanupThread.java b/hudson-core/src/main/java/hudson/model/WorkspaceCleanupThread.java
index 0c163248..760ed242 100644
--- a/hudson-core/src/main/java/hudson/model/WorkspaceCleanupThread.java
+++ b/hudson-core/src/main/java/hudson/model/WorkspaceCleanupThread.java
@@ -77,14 +77,7 @@ public class WorkspaceCleanupThread extends AsyncPeriodicWork {
}
private void process(Hudson h) throws IOException, InterruptedException {
- File[] jobsRootDirs;
- TeamManager teamManager = Hudson.getInstance().getTeamManager();
- if (teamManager != null) {
- jobsRootDirs = teamManager.getJobsRootFolders();
- } else {
- File jobs = new File(h.getRootDir(), "jobs");
- jobsRootDirs = jobs.listFiles(DIR_FILTER);
- }
+ File[] jobsRootDirs = Hudson.getInstance().getTeamManager().getJobsRootFolders();
if (jobsRootDirs == null) {
return;
}
diff --git a/hudson-core/src/main/java/org/eclipse/hudson/security/team/Team.java b/hudson-core/src/main/java/org/eclipse/hudson/security/team/Team.java
index 9fdfb4fd..3084e465 100644
--- a/hudson-core/src/main/java/org/eclipse/hudson/security/team/Team.java
+++ b/hudson-core/src/main/java/org/eclipse/hudson/security/team/Team.java
@@ -191,7 +191,7 @@ public class Team implements AccessControlled {
getTeamManager().save();
}
}
-
+
public boolean isMember(String userName) {
HudsonSecurityManager hudsonSecurityManager = HudsonSecurityEntitiesHolder.getHudsonSecurityManager();
SecurityRealm securityRealm = null;
diff --git a/hudson-core/src/main/java/org/eclipse/hudson/security/team/TeamBasedAuthorizationStrategy.java b/hudson-core/src/main/java/org/eclipse/hudson/security/team/TeamBasedAuthorizationStrategy.java
index d1a25c61..cb2b8b66 100644
--- a/hudson-core/src/main/java/org/eclipse/hudson/security/team/TeamBasedAuthorizationStrategy.java
+++ b/hudson-core/src/main/java/org/eclipse/hudson/security/team/TeamBasedAuthorizationStrategy.java
@@ -95,7 +95,7 @@ public class TeamBasedAuthorizationStrategy extends AuthorizationStrategy {
return new TeamUtils.ErrorHttpResponse("Sys admin name required");
}
- TeamManager teamManager = Hudson.getInstance().getTeamManager(true);
+ TeamManager teamManager = Hudson.getInstance().getTeamManager();
if (teamManager.getSysAdmins().contains(sysAdminSid)) {
return new TeamUtils.ErrorHttpResponse(sysAdminSid + " is already a System Administrator.");
}
@@ -114,7 +114,7 @@ public class TeamBasedAuthorizationStrategy extends AuthorizationStrategy {
return new TeamUtils.ErrorHttpResponse("Sys admin name required");
}
- TeamManager teamManager = Hudson.getInstance().getTeamManager(true);
+ TeamManager teamManager = Hudson.getInstance().getTeamManager();
if (teamManager.getSysAdmins().contains(sysAdminSid)) {
teamManager.removeSysAdmin(sysAdminSid);
return HttpResponses.ok();
diff --git a/hudson-core/src/main/java/org/eclipse/hudson/security/team/TeamJobListener.java b/hudson-core/src/main/java/org/eclipse/hudson/security/team/TeamJobListener.java
index 23e2b044..b188a512 100644
--- a/hudson-core/src/main/java/org/eclipse/hudson/security/team/TeamJobListener.java
+++ b/hudson-core/src/main/java/org/eclipse/hudson/security/team/TeamJobListener.java
@@ -36,9 +36,7 @@ public class TeamJobListener extends ItemListener {
if (item instanceof Job<?, ?>) {
Job job = (Job) item;
try {
- if (getTeamManager() != null) {
- getTeamManager().addJobToCurrentUserTeam(job.getName());
- }
+ getTeamManager().addJobToCurrentUserTeam(job.getName());
} catch (IOException ex) {
logger.error("Failed to rename job in current user team", ex);
} catch (TeamManager.TeamNotFoundException ex) {
@@ -51,10 +49,8 @@ public class TeamJobListener extends ItemListener {
public void onRenamed(Item item, String oldJobName, String newJobName) {
if (item instanceof Job<?, ?>) {
try {
- if (getTeamManager() != null) {
- Team team = getTeamManager().findJobOwnerTeam(oldJobName);
- getTeamManager().renameJob(team, newJobName, newJobName);
- }
+ Team team = getTeamManager().findJobOwnerTeam(oldJobName);
+ getTeamManager().renameJob(team, newJobName, newJobName);
} catch (IOException ex) {
logger.error("Failed to rename job in current user team", ex);
}
@@ -66,9 +62,7 @@ public class TeamJobListener extends ItemListener {
if (item instanceof Job<?, ?>) {
Job job = (Job) item;
try {
- if (getTeamManager() != null) {
- getTeamManager().removeJobFromCurrentUserTeam(job.getName());
- }
+ getTeamManager().removeJobFromCurrentUserTeam(job.getName());
} catch (IOException ex) {
logger.error("Failed to rename job in current user team", ex);
}
diff --git a/hudson-core/src/main/java/org/eclipse/hudson/security/team/TeamManager.java b/hudson-core/src/main/java/org/eclipse/hudson/security/team/TeamManager.java
index a6ab0efc..ef897c18 100644
--- a/hudson-core/src/main/java/org/eclipse/hudson/security/team/TeamManager.java
+++ b/hudson-core/src/main/java/org/eclipse/hudson/security/team/TeamManager.java
@@ -17,14 +17,15 @@ import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import hudson.BulkChange;
-import hudson.Util;
import hudson.XmlFile;
import hudson.model.Hudson;
import hudson.model.Item;
import hudson.model.Job;
import hudson.model.Saveable;
+import hudson.model.TopLevelItem;
import hudson.model.listeners.SaveableListener;
import hudson.security.ACL;
+import hudson.security.AuthorizationStrategy;
import hudson.security.SecurityRealm;
import hudson.util.FormValidation;
import hudson.util.XStream2;
@@ -50,13 +51,15 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
- * Manger that manages the teams and their persistence
+ * Manager that manages the teams and their persistence
*
* @since 3.1.0
* @author Winston Prakash
*/
public final class TeamManager implements Saveable {
+ public static final String TEAM_SEPARATOR = ".";
+
private List<String> sysAdmins = new CopyOnWriteArrayList<String>();
private List<Team> teams = new CopyOnWriteArrayList<Team>();
private transient final XStream xstream = new XStream2();
@@ -78,6 +81,17 @@ public final class TeamManager implements Saveable {
ensurePublicTeam();
}
+ public boolean isTeamManagementEnabled() {
+ HudsonSecurityManager hudsonSecurityManager = HudsonSecurityEntitiesHolder.getHudsonSecurityManager();
+ if (hudsonSecurityManager != null) {
+ AuthorizationStrategy authorizationStrategy = hudsonSecurityManager.getAuthorizationStrategy();
+ if (authorizationStrategy instanceof TeamBasedAuthorizationStrategy) {
+ return true;
+ }
+ }
+ return false;
+ }
+
public void addSysAdmin(String adminName) throws IOException {
if (!sysAdmins.contains(adminName)) {
sysAdmins.add(adminName);
@@ -356,35 +370,41 @@ public final class TeamManager implements Saveable {
public HttpResponse doCheckSid(@QueryParameter String sid) throws IOException {
return FormValidation.respond(FormValidation.Kind.OK, TeamUtils.getIcon(sid));
}
+
+ public void ensureJobInTeam(TopLevelItem item, Team team) throws IOException {
+ Job job = (Job) item;
+ Team ownerTeam = findJobOwnerTeam(job.getName());
+ if (!team.equals(ownerTeam)) {
+ moveJob(job, ownerTeam, team);
+ }
+ }
private void moveJob(Job job, Team oldTeam, Team newTeam) throws IOException {
try {
-
String oldJobName = job.getName();
- String unqualifiedJobName = getUnqualifiedJobName(job.getName());
- String qualifiedNewJobName = getTeamQualifiedJobName(newTeam.getName(), unqualifiedJobName);
-
-// File jobRootDir = job.getRootDir();
-// File newJobRootDir = new File(getJobsFolder(newTeam), qualifiedNewJobName);
-// newJobRootDir.mkdirs();
-// Util.moveDirectory(jobRootDir, newJobRootDir);
+ String unqualifiedJobName = getUnqualifiedJobName(oldTeam, job.getName());
+ String qualifiedNewJobName = getTeamQualifiedJobName(newTeam, unqualifiedJobName);
+ // Add the new job, rename before removing the old job
+ // ensures team manager will find correct locations.
newTeam.addJob(new TeamJob(qualifiedNewJobName));
job.renameTo(qualifiedNewJobName);
oldTeam.removeJob(oldJobName);
- Hudson.getInstance().replaceItemId(job.getName(), qualifiedNewJobName);
+ Hudson.getInstance().replaceItem(job.getName(), qualifiedNewJobName);
} catch (Exception exc) {
throw new IOException(exc);
}
}
- private String getUnqualifiedJobName(String jobName){
- int index = jobName.indexOf('.');
- if (jobName.indexOf('.') != -1){
- jobName = jobName.substring(index + 1);
+ private String getUnqualifiedJobName(Team team, String jobName){
+ if (Team.PUBLIC_TEAM_NAME.equals(team.getName())) {
+ return jobName;
}
- return jobName;
+ if (jobName.startsWith(team.getName()+TEAM_SEPARATOR)) {
+ return jobName.substring(team.getName().length()+1);
+ }
+ throw new IllegalStateException("Job "+jobName+" does not belong to Team "+team.getName());
}
/**
@@ -434,6 +454,42 @@ public final class TeamManager implements Saveable {
teams.remove(team);
save();
}
+
+ /**
+ * Return true if current user has access to team. Team management
+ * must be enabled.
+ * @param teamName
+ * @return
+ */
+ public boolean isCurrentUserHasAccessToTeam(String teamName) {
+ if (isTeamManagementEnabled()) {
+ try {
+ Team team = findTeam(teamName);
+ if (isCurrentUserSysAdmin()) {
+ return true;
+ }
+ HudsonSecurityManager hudsonSecurityManager = HudsonSecurityEntitiesHolder.getHudsonSecurityManager();
+ SecurityRealm securityRealm = null;
+ if (hudsonSecurityManager != null) {
+ securityRealm = hudsonSecurityManager.getSecurityRealm();
+ }
+ if ((securityRealm != null) && securityRealm instanceof TeamAwareSecurityRealm) {
+ TeamAwareSecurityRealm teamAwareSecurityRealm = (TeamAwareSecurityRealm) securityRealm;
+ if (team.equals(teamAwareSecurityRealm.GetCurrentUserTeam())) {
+ return true;
+ }
+ } else {
+ String currentUser = HudsonSecurityManager.getAuthentication().getName();
+ if (team.isMember(currentUser)) {
+ return true;
+ }
+ }
+ } catch (TeamNotFoundException ex) {
+ // no access
+ }
+ }
+ return false;
+ }
public Team findCurrentUserTeam() {
Team team;
@@ -585,7 +641,7 @@ public final class TeamManager implements Saveable {
public String getTeamQualifiedJobName(String jobName) {
Team team = findCurrentUserTeam();
if ((team != null) && !Team.PUBLIC_TEAM_NAME.equals(team.getName())) {
- jobName = team.getName() + "." + jobName;
+ jobName = team.getName() + TEAM_SEPARATOR + jobName;
}
return jobName;
}
@@ -597,26 +653,34 @@ public final class TeamManager implements Saveable {
* @param jobName
* @return String, Team qualified Job ID
*/
- public String getTeamQualifiedJobName(String teamName, String jobName) {
- if ((teamName == null) || "".equals(jobName) || Team.PUBLIC_TEAM_NAME.equals(teamName)) {
+ public String getTeamQualifiedJobName(Team team, String jobName) {
+ String teamName = team.getName();
+ if (Team.PUBLIC_TEAM_NAME.equals(teamName)) {
return jobName;
}
- String qualifiedJobName = teamName + "." + jobName;
+ StringBuilder sb = new StringBuilder(teamName + TEAM_SEPARATOR + jobName);
+ // Make sure the name is unique
+ Hudson h = Hudson.getInstance();
int postfix = 2;
- try {
- while (findTeam(teamName).isJobOwner(qualifiedJobName)){
- qualifiedJobName += ("_" + postfix);
+ int baseLength = sb.length();
+ while (true) {
+ String qualifiedName = sb.toString();
+ if (!team.isJobOwner(qualifiedName) && !publicTeam.isJobOwner(qualifiedName)) {
+ break;
}
- } catch (TeamNotFoundException ex) {
- logger.info("getTeamQualifiedJobName: " + ex.getLocalizedMessage());
- return jobName;
+ sb.setLength(baseLength);
+ sb.append("_" + postfix++);
}
- return qualifiedJobName;
+ return sb.toString();
}
/**
* The Folder where all the jobs of the team to which this jobName belongs to
- * are stored
+ * are stored.
+ *
+ * <p>This method should be called to determine the jobs folder
+ * whether or not team management is enabled, as team manager alone
+ * knows where team jobs are.
*
* @param jobName
* @return String, path to the team jobs folder
@@ -624,7 +688,7 @@ public final class TeamManager implements Saveable {
public String getJobsFolderName(String jobName) {
Team team = findJobOwnerTeam(jobName);
// May be just created job
- if (team == null) {
+ if (team == null && isTeamManagementEnabled()) {
team = findCurrentUserTeam();
}
if ((team != null) && !Team.PUBLIC_TEAM_NAME.equals(team.getName())) {
diff --git a/hudson-core/src/test/java/hudson/model/FreeStyleProjectTest.java b/hudson-core/src/test/java/hudson/model/FreeStyleProjectTest.java
index 6e0a4a8c..7aa20d8d 100644
--- a/hudson-core/src/test/java/hudson/model/FreeStyleProjectTest.java
+++ b/hudson-core/src/test/java/hudson/model/FreeStyleProjectTest.java
@@ -30,9 +30,11 @@ import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;
+import org.apache.commons.io.FileUtils;
import static org.easymock.EasyMock.expect;
import org.eclipse.hudson.security.HudsonSecurityEntitiesHolder;
import org.eclipse.hudson.security.HudsonSecurityManager;
+import org.eclipse.hudson.security.team.TeamManager;
import static org.junit.Assert.assertFalse;
import static org.powermock.api.easymock.PowerMock.createMock;
import static org.powermock.api.easymock.PowerMock.mockStatic;
@@ -102,7 +104,8 @@ public class FreeStyleProjectTest {
@Test
public void testOnCreatedFromScratchAnonymousAuthentication() {
Hudson hudson = createMock(Hudson.class);
- expect(hudson.getTeamManager()).andReturn(null).anyTimes();
+ TeamManager teamManager = new TeamManager(FileUtils.getTempDirectory());
+ expect(hudson.getTeamManager()).andReturn(teamManager).anyTimes();
expect(hudson.isTeamManagementEnabled()).andReturn(false).anyTimes();
mockStatic(Hudson.class);
expect(Hudson.getInstance()).andReturn(hudson).anyTimes();
@@ -172,7 +175,8 @@ public class FreeStyleProjectTest {
@Test
public void testOnCopiedFromAnonymousAuthentication() {
Hudson hudson = createMock(Hudson.class);
- expect(hudson.getTeamManager()).andReturn(null).anyTimes();
+ TeamManager teamManager = new TeamManager(FileUtils.getTempDirectory());
+ expect(hudson.getTeamManager()).andReturn(teamManager).anyTimes();
expect(hudson.isTeamManagementEnabled()).andReturn(false).anyTimes();
mockStatic(Hudson.class);
expect(Hudson.getInstance()).andReturn(hudson).anyTimes();

Back to the top