From 0763ffd0e928eeb7b2411701f03a165139902f54 Mon Sep 17 00:00:00 2001 From: Stephen Connolly Date: Thu, 27 Oct 2016 11:03:45 +0100 Subject: [PATCH 01/15] [FIXED JENKINS-39300] Use View.getDisplayName() when rendering the view tabs - Also fix the stapler documentation for the tag --- .../DefaultMyViewsTabBar/myViewTabs.jelly | 2 +- .../views/DefaultViewsTabBar/viewTabs.jelly | 2 +- core/src/main/resources/lib/layout/tab.jelly | 28 ++++++++++++------- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/core/src/main/resources/hudson/views/DefaultMyViewsTabBar/myViewTabs.jelly b/core/src/main/resources/hudson/views/DefaultMyViewsTabBar/myViewTabs.jelly index 10b55865f6..67a2ad0af0 100644 --- a/core/src/main/resources/hudson/views/DefaultMyViewsTabBar/myViewTabs.jelly +++ b/core/src/main/resources/hudson/views/DefaultMyViewsTabBar/myViewTabs.jelly @@ -27,7 +27,7 @@ THE SOFTWARE. - + - + - - -
+ + + + The name of the tab + + + The url of the tab + + + Whether the tab is active or not + + + The title of the tab + + + From 5b1ace0e0a2f9e9576444e07dd8d26daa44876a5 Mon Sep 17 00:00:00 2001 From: Stephen Connolly Date: Thu, 27 Oct 2016 11:07:36 +0100 Subject: [PATCH 02/15] [JENKINS-39300] Add displayName validation API to ViewDescriptor --- .../java/hudson/model/ViewDescriptor.java | 29 +++++++++++++++++++ .../hudson/model/Messages.properties | 2 ++ 2 files changed, 31 insertions(+) diff --git a/core/src/main/java/hudson/model/ViewDescriptor.java b/core/src/main/java/hudson/model/ViewDescriptor.java index dde3c0b9ed..ddecc6d2f3 100644 --- a/core/src/main/java/hudson/model/ViewDescriptor.java +++ b/core/src/main/java/hudson/model/ViewDescriptor.java @@ -23,13 +23,17 @@ */ package hudson.model; +import hudson.util.FormValidation; import hudson.views.ListViewColumn; import hudson.views.ListViewColumnDescriptor; import hudson.views.ViewJobFilter; import java.util.Iterator; import java.util.List; +import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; import jenkins.model.DirectlyModifiableTopLevelItemGroup; import jenkins.model.Jenkins; +import org.apache.commons.lang.StringUtils; import org.kohsuke.accmod.Restricted; import org.kohsuke.accmod.restrictions.DoNotUse; import org.kohsuke.stapler.AncestorInPath; @@ -110,4 +114,29 @@ public abstract class ViewDescriptor extends Descriptor { public List> getJobFiltersDescriptors() { return ViewJobFilter.all(); } + + /** + * Validation of the display name field. + * + * @param view the view to check the new display name of. + * @param value the proposed new display name. + * @return the validation result. + * @since FIXME + */ + @SuppressWarnings("unused") // expose utility check method to subclasses + protected FormValidation checkDisplayName(@Nonnull View view, @CheckForNull String value) { + if (StringUtils.isBlank(value)) { + // no custom name, no need to check + return FormValidation.ok(); + } + for (View v: view.owner.getViews()) { + if (v.getViewName().equals(view.getViewName())) { + continue; + } + if (StringUtils.equals(v.getDisplayName(), value)) { + return FormValidation.warning(Messages.View_DisplayNameNotUniqueWarning(value)); + } + } + return FormValidation.ok(); + } } diff --git a/core/src/main/resources/hudson/model/Messages.properties b/core/src/main/resources/hudson/model/Messages.properties index 454e2d98f2..61b7886f3e 100644 --- a/core/src/main/resources/hudson/model/Messages.properties +++ b/core/src/main/resources/hudson/model/Messages.properties @@ -261,6 +261,8 @@ View.ConfigurePermission.Description=\ View.ReadPermission.Description=\ This permission allows users to see views (implied by generic read access). View.MissingMode=No view type is specified +View.DisplayNameNotUniqueWarning=The display name, "{0}", is already in use by another view and \ + could cause confusion and delay. UpdateCenter.Status.CheckingInternet=Checking internet connectivity UpdateCenter.Status.CheckingJavaNet=Checking update center connectivity From 7c43986b388865c93b133c4f87eeb6f864d97d42 Mon Sep 17 00:00:00 2001 From: Stephen Connolly Date: Thu, 27 Oct 2016 11:32:55 +0100 Subject: [PATCH 03/15] [JENKINS-39300 Follow-up] Fix the AllView tab title to reflect l10n - Also while we are at it, modernise ViewDescriptor instantiation to allow for descriptor visibility filtering --- core/src/main/java/hudson/model/AllView.java | 13 +++++++--- core/src/main/java/hudson/model/View.java | 12 ++++++++-- .../java/hudson/model/ViewDescriptor.java | 24 +++++++++++++++++++ core/src/main/java/jenkins/model/Jenkins.java | 2 +- .../model/MyViewsProperty/newView.jelly | 2 +- .../jenkins/model/Jenkins/newView.jelly | 2 +- 6 files changed, 47 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/hudson/model/AllView.java b/core/src/main/java/hudson/model/AllView.java index 9398c307f0..d70a3c9232 100644 --- a/core/src/main/java/hudson/model/AllView.java +++ b/core/src/main/java/hudson/model/AllView.java @@ -64,6 +64,11 @@ public class AllView extends View { return true; } + @Override + public String getDisplayName() { + return Messages.Hudson_ViewName(); + } + @RequirePOST @Override public Item doCreateItem(StaplerRequest req, StaplerResponse rsp) @@ -92,10 +97,12 @@ public class AllView extends View { @Extension @Symbol("all") public static final class DescriptorImpl extends ViewDescriptor { @Override - public boolean isInstantiable() { - for (View v : Stapler.getCurrentRequest().findAncestorObject(ViewGroup.class).getViews()) - if(v instanceof AllView) + public boolean isApplicableIn(ViewGroup owner) { + for (View v : owner.getViews()) { + if (v instanceof AllView) { return false; + } + } return true; } diff --git a/core/src/main/java/hudson/model/View.java b/core/src/main/java/hudson/model/View.java index 9683370a73..e259c94dde 100644 --- a/core/src/main/java/hudson/model/View.java +++ b/core/src/main/java/hudson/model/View.java @@ -1219,11 +1219,19 @@ public abstract class View extends AbstractModelObject implements AccessControll return Jenkins.getInstance().getDescriptorList(View.class); } + /** + * Returns the {@link ViewDescriptor} instances that can be instantiated for the {@link ViewGroup} in the current + * {@link StaplerRequest}. NOTE: This method must be called from a {@link StaplerRequest} + * @return the list of instantiable {@link ViewDescriptor} instances for the current {@link StaplerRequest} + */ public static List allInstantiable() { List r = new ArrayList(); - for (ViewDescriptor d : all()) - if(d.isInstantiable()) + ViewGroup owner = Stapler.getCurrentRequest().findAncestorObject(ViewGroup.class); + for (ViewDescriptor d : DescriptorVisibilityFilter.apply(owner, all())) { + if (d.isApplicableIn(owner) && d.isInstantiable()) { r.add(d); + } + } return r; } diff --git a/core/src/main/java/hudson/model/ViewDescriptor.java b/core/src/main/java/hudson/model/ViewDescriptor.java index ddecc6d2f3..7bee01a108 100644 --- a/core/src/main/java/hudson/model/ViewDescriptor.java +++ b/core/src/main/java/hudson/model/ViewDescriptor.java @@ -27,6 +27,8 @@ import hudson.util.FormValidation; import hudson.views.ListViewColumn; import hudson.views.ListViewColumnDescriptor; import hudson.views.ViewJobFilter; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; import java.util.Iterator; import java.util.List; import javax.annotation.CheckForNull; @@ -34,6 +36,7 @@ import javax.annotation.Nonnull; import jenkins.model.DirectlyModifiableTopLevelItemGroup; import jenkins.model.Jenkins; import org.apache.commons.lang.StringUtils; +import org.jvnet.tiger_types.Types; import org.kohsuke.accmod.Restricted; import org.kohsuke.accmod.restrictions.DoNotUse; import org.kohsuke.stapler.AncestorInPath; @@ -139,4 +142,25 @@ public abstract class ViewDescriptor extends Descriptor { } return FormValidation.ok(); } + + /** + * Returns true if this {@link View} type is applicable to the given {@link ViewGroup} type. + * + * @return true to indicate applicable, in which case the view will be instantiable within the type of owner. + * @since FIXME + */ + public boolean isApplicable(Class ownerType) { + return true; + } + + /** + * Returns true if this {@link View} type is applicable in the specific {@link ViewGroup}. + * + * @return true to indicate applicable, in which case the view will be instantiable within the given owner. + * @since FIXME + */ + public boolean isApplicableIn(ViewGroup owner) { + return isApplicable(owner.getClass()); + } + } diff --git a/core/src/main/java/jenkins/model/Jenkins.java b/core/src/main/java/jenkins/model/Jenkins.java index 25527183f5..b7b3bdff11 100644 --- a/core/src/main/java/jenkins/model/Jenkins.java +++ b/core/src/main/java/jenkins/model/Jenkins.java @@ -3052,7 +3052,7 @@ public class Jenkins extends AbstractCIBase implements DirectlyModifiableTopLeve // initialize views by inserting the default view if necessary // this is both for clean Jenkins and for backward compatibility. if(views.size()==0 || primaryView==null) { - View v = new AllView(Messages.Hudson_ViewName()); + View v = new AllView("all"); setViewOwner(v); views.add(0,v); primaryView = v.getViewName(); diff --git a/core/src/main/resources/hudson/model/MyViewsProperty/newView.jelly b/core/src/main/resources/hudson/model/MyViewsProperty/newView.jelly index 8c2f1cf13e..fd469596f0 100644 --- a/core/src/main/resources/hudson/model/MyViewsProperty/newView.jelly +++ b/core/src/main/resources/hudson/model/MyViewsProperty/newView.jelly @@ -32,7 +32,7 @@ THE SOFTWARE. - + diff --git a/core/src/main/resources/jenkins/model/Jenkins/newView.jelly b/core/src/main/resources/jenkins/model/Jenkins/newView.jelly index 823ed4a97b..3f7548721a 100644 --- a/core/src/main/resources/jenkins/model/Jenkins/newView.jelly +++ b/core/src/main/resources/jenkins/model/Jenkins/newView.jelly @@ -31,7 +31,7 @@ THE SOFTWARE. - + From 056b3b052cebad2eb04f2159a66066ca25e0c9de Mon Sep 17 00:00:00 2001 From: Stephen Connolly Date: Thu, 27 Oct 2016 11:43:52 +0100 Subject: [PATCH 04/15] [JENKINS-39300 Follow-up] Allow create permission restriction for types of views --- core/src/main/java/hudson/model/View.java | 9 ++++++++- core/src/main/java/hudson/security/ACL.java | 19 +++++++++++++++++++ .../hudson/model/Messages.properties | 1 + 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/hudson/model/View.java b/core/src/main/java/hudson/model/View.java index e259c94dde..139c38e3e3 100644 --- a/core/src/main/java/hudson/model/View.java +++ b/core/src/main/java/hudson/model/View.java @@ -1228,7 +1228,8 @@ public abstract class View extends AbstractModelObject implements AccessControll List r = new ArrayList(); ViewGroup owner = Stapler.getCurrentRequest().findAncestorObject(ViewGroup.class); for (ViewDescriptor d : DescriptorVisibilityFilter.apply(owner, all())) { - if (d.isApplicableIn(owner) && d.isInstantiable()) { + if (d.isApplicableIn(owner) && d.isInstantiable() + && owner.getACL().hasCreatePermission(Jenkins.getAuthentication(), owner, d)) { r.add(d); } } @@ -1276,6 +1277,9 @@ public abstract class View extends AbstractModelObject implements AccessControll if (mode==null || mode.length()==0) { if(isXmlSubmission) { View v = createViewFromXML(name, req.getInputStream()); + if (!owner.getACL().hasCreatePermission(Jenkins.getAuthentication(), owner, v.getDescriptor())) { + throw new Failure(Messages.View_CreatePermissionMissing(v.getDescriptor().getDisplayName())); + } v.owner = owner; rsp.setStatus(HttpServletResponse.SC_OK); return v; @@ -1295,6 +1299,9 @@ public abstract class View extends AbstractModelObject implements AccessControll // create a view v = descriptor.newInstance(req,req.getSubmittedForm()); } + if (!owner.getACL().hasCreatePermission(Jenkins.getAuthentication(), owner, v.getDescriptor())) { + throw new Failure(Messages.View_CreatePermissionMissing(v.getDescriptor().getDisplayName())); + } v.owner = owner; // redirect to the config screen diff --git a/core/src/main/java/hudson/security/ACL.java b/core/src/main/java/hudson/security/ACL.java index 3f892b7289..e2cea2cb03 100644 --- a/core/src/main/java/hudson/security/ACL.java +++ b/core/src/main/java/hudson/security/ACL.java @@ -24,6 +24,8 @@ package hudson.security; import hudson.model.User; +import hudson.model.ViewDescriptor; +import hudson.model.ViewGroup; import javax.annotation.CheckForNull; import javax.annotation.Nonnull; @@ -120,6 +122,23 @@ public abstract class ACL { return true; } + /** + * Checks if the given principal has the permission to create views within the specified view group. + *

+ * Note that {@link #SYSTEM} can be passed in as the authentication parameter, + * in which case you should probably just assume it can create anything anywhere. + * @param a the principal. + * @param c the container of the view. + * @param d the descriptor of the view to be created. + * @return false + * if the user doesn't have the permission. + * @since FIXME + */ + public boolean hasCreatePermission(@Nonnull Authentication a, @Nonnull ViewGroup c, + @Nonnull ViewDescriptor d) { + return true; + } + // // Sid constants // diff --git a/core/src/main/resources/hudson/model/Messages.properties b/core/src/main/resources/hudson/model/Messages.properties index 61b7886f3e..6571451ff6 100644 --- a/core/src/main/resources/hudson/model/Messages.properties +++ b/core/src/main/resources/hudson/model/Messages.properties @@ -263,6 +263,7 @@ View.ReadPermission.Description=\ View.MissingMode=No view type is specified View.DisplayNameNotUniqueWarning=The display name, "{0}", is already in use by another view and \ could cause confusion and delay. +View.CreatePermissionMissing=You are not permitted to create a {0} view UpdateCenter.Status.CheckingInternet=Checking internet connectivity UpdateCenter.Status.CheckingJavaNet=Checking update center connectivity From e0a80dc92642b74e3407e6f21b146949e8a5f479 Mon Sep 17 00:00:00 2001 From: Stephen Connolly Date: Thu, 27 Oct 2016 11:44:41 +0100 Subject: [PATCH 05/15] Redundant null check removed --- core/src/main/java/hudson/model/View.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/hudson/model/View.java b/core/src/main/java/hudson/model/View.java index 139c38e3e3..27b502ba59 100644 --- a/core/src/main/java/hudson/model/View.java +++ b/core/src/main/java/hudson/model/View.java @@ -1288,7 +1288,7 @@ public abstract class View extends AbstractModelObject implements AccessControll } View v; - if (mode!=null && mode.equals("copy")) { + if ("copy".equals(mode)) { v = copy(req, owner, name); } else { ViewDescriptor descriptor = all().findByName(mode); From 417e0d8784b05fa901038c08c14af7aa381fe521 Mon Sep 17 00:00:00 2001 From: Stephen Connolly Date: Thu, 27 Oct 2016 12:18:32 +0100 Subject: [PATCH 06/15] [JENKINS-39300 Follow-up] JobFilters and Columns should work with DescriptorVisiblilityFilters too --- core/src/main/java/hudson/model/ViewDescriptor.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/hudson/model/ViewDescriptor.java b/core/src/main/java/hudson/model/ViewDescriptor.java index 7bee01a108..f6ef0c7ba9 100644 --- a/core/src/main/java/hudson/model/ViewDescriptor.java +++ b/core/src/main/java/hudson/model/ViewDescriptor.java @@ -41,6 +41,8 @@ import org.kohsuke.accmod.Restricted; import org.kohsuke.accmod.restrictions.DoNotUse; import org.kohsuke.stapler.AncestorInPath; import org.kohsuke.stapler.QueryParameter; +import org.kohsuke.stapler.Stapler; +import org.kohsuke.stapler.StaplerRequest; /** * {@link Descriptor} for {@link View}. @@ -108,14 +110,20 @@ public abstract class ViewDescriptor extends Descriptor { * Possible {@link ListViewColumnDescriptor}s that can be used with this view. */ public List> getColumnsDescriptors() { - return ListViewColumn.all(); + StaplerRequest request = Stapler.getCurrentRequest(); + View view = request.findAncestorObject(clazz); + return view == null ? DescriptorVisibilityFilter.applyType(clazz, ListViewColumn.all()) + : DescriptorVisibilityFilter.apply(view, ListViewColumn.all()); } /** * Possible {@link ViewJobFilter} types that can be used with this view. */ public List> getJobFiltersDescriptors() { - return ViewJobFilter.all(); + StaplerRequest request = Stapler.getCurrentRequest(); + View view = request.findAncestorObject(clazz); + return view == null ? DescriptorVisibilityFilter.applyType(clazz, ViewJobFilter.all()) + : DescriptorVisibilityFilter.apply(view, ViewJobFilter.all()); } /** From f3d13af44d0084de51dd6364656340c6e54989e4 Mon Sep 17 00:00:00 2001 From: Stephen Connolly Date: Thu, 27 Oct 2016 12:34:22 +0100 Subject: [PATCH 07/15] [JENKINS-39300 Follow-up] Default columns should respect DescriptorVisiblilityFilters --- core/src/main/java/hudson/model/ListView.java | 6 ++- core/src/main/java/hudson/model/View.java | 2 +- .../java/hudson/views/ListViewColumn.java | 37 +++++++++++++++++-- .../test/java/hudson/model/ListViewTest.java | 4 +- 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/hudson/model/ListView.java b/core/src/main/java/hudson/model/ListView.java index 2aa955b1a3..68ded29c6f 100644 --- a/core/src/main/java/hudson/model/ListView.java +++ b/core/src/main/java/hudson/model/ListView.java @@ -141,7 +141,9 @@ public class ListView extends View implements DirectlyModifiableView { protected void initColumns() { if (columns == null) - columns = new DescribableList>(this,ListViewColumn.createDefaultInitialColumnList()); + columns = new DescribableList>(this, + ListViewColumn.createDefaultInitialColumnList(getClass()) + ); } protected void initJobFilters() { @@ -459,7 +461,7 @@ public class ListView extends View implements DirectlyModifiableView { */ @Deprecated public static List getDefaultColumns() { - return ListViewColumn.createDefaultInitialColumnList(); + return ListViewColumn.createDefaultInitialColumnList(ListView.class); } @Restricted(NoExternalUse.class) diff --git a/core/src/main/java/hudson/model/View.java b/core/src/main/java/hudson/model/View.java index 27b502ba59..bb653c1189 100644 --- a/core/src/main/java/hudson/model/View.java +++ b/core/src/main/java/hudson/model/View.java @@ -415,7 +415,7 @@ public abstract class View extends AbstractModelObject implements AccessControll * If this view uses <t:projectView> for rendering, this method returns columns to be displayed. */ public Iterable getColumns() { - return ListViewColumn.createDefaultInitialColumnList(); + return ListViewColumn.createDefaultInitialColumnList(this); } /** diff --git a/core/src/main/java/hudson/views/ListViewColumn.java b/core/src/main/java/hudson/views/ListViewColumn.java index b5b4b62f9f..094d0f0dae 100644 --- a/core/src/main/java/hudson/views/ListViewColumn.java +++ b/core/src/main/java/hudson/views/ListViewColumn.java @@ -29,6 +29,7 @@ import hudson.ExtensionPoint; import hudson.model.Describable; import hudson.model.Descriptor; import hudson.model.Descriptor.FormException; +import hudson.model.DescriptorVisibilityFilter; import jenkins.model.Jenkins; import hudson.model.Item; import hudson.model.ItemGroup; @@ -121,20 +122,50 @@ public abstract class ListViewColumn implements ExtensionPoint, Describable createDefaultInitialColumnList() { + return createDefaultInitialColumnList(ListViewColumn.all()); + } + + /** + * Creates the list of {@link ListViewColumn}s to be used for newly created {@link ListView}s and their likes. + * + * @see ListView#initColumns() + * @since FIXME + */ + public static List createDefaultInitialColumnList(Class context) { + return createDefaultInitialColumnList(DescriptorVisibilityFilter.applyType(context, ListViewColumn.all())); + } + + /** + * Creates the list of {@link ListViewColumn}s to be used for a {@link ListView}s and their likes. + * + * @see View#getColumns() + * @since FIXME + */ + public static List createDefaultInitialColumnList(View view) { + return createDefaultInitialColumnList(DescriptorVisibilityFilter.apply(view, ListViewColumn.all())); + } + + private static List createDefaultInitialColumnList(List> descriptors) { // OK, set up default list of columns: // create all instances ArrayList r = new ArrayList(); final JSONObject emptyJSON = new JSONObject(); - for (Descriptor d : ListViewColumn.all()) + for (Descriptor d : descriptors) try { if (d instanceof ListViewColumnDescriptor) { ListViewColumnDescriptor ld = (ListViewColumnDescriptor) d; - if (!ld.shownByDefault()) continue; // skip this + if (!ld.shownByDefault()) { + continue; // skip this + } } ListViewColumn lvc = d.newInstance(null, emptyJSON); - if (!lvc.shownByDefault()) continue; // skip this + if (!lvc.shownByDefault()) { + continue; // skip this + } r.add(lvc); } catch (FormException e) { diff --git a/core/src/test/java/hudson/model/ListViewTest.java b/core/src/test/java/hudson/model/ListViewTest.java index 7de805913b..f184aac9a2 100644 --- a/core/src/test/java/hudson/model/ListViewTest.java +++ b/core/src/test/java/hudson/model/ListViewTest.java @@ -30,7 +30,7 @@ public class ListViewTest { mockStatic(Items.class); mockStatic(ListViewColumn.class); List columns = Collections.emptyList(); - when(ListViewColumn.createDefaultInitialColumnList()).thenReturn(columns); + when(ListViewColumn.createDefaultInitialColumnList(ListView.class)).thenReturn(columns); ViewGroup owner = mock(ViewGroup.class); ItemGroup itemGroupOwner = mock(ItemGroup.class); when(owner.getItemGroup()).thenReturn(itemGroupOwner); @@ -49,7 +49,7 @@ public class ListViewTest { mockStatic(Items.class); mockStatic(ListViewColumn.class); List columns = Collections.emptyList(); - when(ListViewColumn.createDefaultInitialColumnList()).thenReturn(columns); + when(ListViewColumn.createDefaultInitialColumnList(ListView.class)).thenReturn(columns); ViewGroup owner = mock(ViewGroup.class); ItemGroup ig = mock(ItemGroup.class); when(owner.getItemGroup()).thenReturn(ig); From 8cb107066b22d9edd45b369c592e1de625d6fce7 Mon Sep 17 00:00:00 2001 From: Stephen Connolly Date: Fri, 28 Oct 2016 09:41:28 +0100 Subject: [PATCH 08/15] [FIXED JENKINS-38606] Detect and fix localized AllView name if and only if safe to fix - Don't fix if there is already a view with the new default name - Don't fix if the AllView is not the primary view --- core/src/main/java/hudson/model/AllView.java | 77 ++++++++++++++++++- .../java/hudson/model/MyViewsProperty.java | 6 +- core/src/main/java/jenkins/model/Jenkins.java | 3 +- 3 files changed, 81 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/hudson/model/AllView.java b/core/src/main/java/hudson/model/AllView.java index d70a3c9232..3ceba50c9f 100644 --- a/core/src/main/java/hudson/model/AllView.java +++ b/core/src/main/java/hudson/model/AllView.java @@ -23,9 +23,15 @@ */ package hudson.model; +import java.util.List; +import java.util.Locale; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; +import org.apache.commons.lang.StringUtils; import org.jenkinsci.Symbol; import org.kohsuke.stapler.DataBoundConstructor; -import org.kohsuke.stapler.Stapler; import org.kohsuke.stapler.StaplerRequest; import org.kohsuke.stapler.StaplerResponse; @@ -44,6 +50,21 @@ import org.kohsuke.stapler.interceptor.RequirePOST; * @since 1.269 */ public class AllView extends View { + + /** + * The name of the default {@link AllView}. An {@link AllView} with this name will get a localized display name. + * Other {@link AllView} instances will be assumed to have been created by the user and thus will use the + * name the user created them with. + * + * @since FIXME + */ + public static final String DEFAULT_VIEW_NAME = "all"; + + /** + * Our logger. + */ + private static final Logger LOGGER = Logger.getLogger(AllView.class.getName()); + @DataBoundConstructor public AllView(String name) { super(name); @@ -66,7 +87,7 @@ public class AllView extends View { @Override public String getDisplayName() { - return Messages.Hudson_ViewName(); + return DEFAULT_VIEW_NAME.equals(name) ? Messages.Hudson_ViewName() : name; } @RequirePOST @@ -94,6 +115,58 @@ public class AllView extends View { // noop } + /** + * Corrects the name of the {@link AllView} if and only if the {@link AllView} is the primary view and + * its name is one of the localized forms of {@link Messages#_Hudson_ViewName()}. + * Use this method to round-trip the primary view name, e.g. + * {@code primaryView = applyJenkins38606Fixup(views, primaryView)} + * NOTE: we can only fix the localized name of an {@link AllView} if it is the primary view as otherwise urls + * would change, whereas the primary view is special and does not normall get accessed by the + * {@code /view/_name_} url. + * + * @param views the list of views. + * @param primaryView the current primary view name. + * @return the primary view name - this will be the same as the provided primary view name unless a JENKINS-38606 + * matching name is detected, in which case this will be the new name of the primary view. + * @since FIXME + */ + @Nonnull + public static String applyJenkins38606Fixup(@Nonnull List views, @Nonnull String primaryView) { + if (DEFAULT_VIEW_NAME.equals(primaryView)) { + // modern name, we are safe + return primaryView; + } + AllView allView = null; + for (View v: views) { + if (DEFAULT_VIEW_NAME.equals(v.getViewName())) { + // name conflict, we cannot rename the all view anyway + return primaryView; + } + if (StringUtils.equals(v.getViewName(), primaryView)) { + if (v instanceof AllView) { + allView = (AllView) v; + } else { + // none of our business fixing as we can only safely fix the primary view + return primaryView; + } + } + } + if (allView != null) { + // the primary view is an AllView but using a non-default name + for (Locale l : Locale.getAvailableLocales()) { + if (primaryView.equals(Messages._Hudson_ViewName().toString(l))) { + // bingo JENKINS-38606 detected + LOGGER.log(Level.INFO, + "JENKINS-38606 detected for AllView in {0}; renaming view from {1} to {2}", + new Object[]{allView.owner.getUrl(), DEFAULT_VIEW_NAME}); + allView.name = DEFAULT_VIEW_NAME; + return DEFAULT_VIEW_NAME; + } + } + } + return primaryView; + } + @Extension @Symbol("all") public static final class DescriptorImpl extends ViewDescriptor { @Override diff --git a/core/src/main/java/hudson/model/MyViewsProperty.java b/core/src/main/java/hudson/model/MyViewsProperty.java index d075a73e81..363bab4478 100644 --- a/core/src/main/java/hudson/model/MyViewsProperty.java +++ b/core/src/main/java/hudson/model/MyViewsProperty.java @@ -83,9 +83,11 @@ public class MyViewsProperty extends UserProperty implements ModifiableViewGroup // this shouldn't happen, but an error in 1.319 meant the last view could be deleted views = new CopyOnWriteArrayList(); - if (views.isEmpty()) + if (views.isEmpty()) { // preserve the non-empty invariant - views.add(new AllView(Messages.Hudson_ViewName(), this)); + views.add(new AllView(AllView.DEFAULT_VIEW_NAME, this)); + } + primaryViewName = AllView.applyJenkins38606Fixup(views, primaryViewName); viewGroupMixIn = new ViewGroupMixIn(this) { protected List views() { return views; } diff --git a/core/src/main/java/jenkins/model/Jenkins.java b/core/src/main/java/jenkins/model/Jenkins.java index b7b3bdff11..d5d4566ec8 100644 --- a/core/src/main/java/jenkins/model/Jenkins.java +++ b/core/src/main/java/jenkins/model/Jenkins.java @@ -3052,11 +3052,12 @@ public class Jenkins extends AbstractCIBase implements DirectlyModifiableTopLeve // initialize views by inserting the default view if necessary // this is both for clean Jenkins and for backward compatibility. if(views.size()==0 || primaryView==null) { - View v = new AllView("all"); + View v = new AllView(AllView.DEFAULT_VIEW_NAME); setViewOwner(v); views.add(0,v); primaryView = v.getViewName(); } + primaryView =AllView.applyJenkins38606Fixup(views, primaryView); if (useSecurity!=null && !useSecurity) { // forced reset to the unsecure mode. From 19e8be4c1ee1ab84a0fae887e29a607b890e1ee0 Mon Sep 17 00:00:00 2001 From: Stephen Connolly Date: Fri, 28 Oct 2016 09:52:38 +0100 Subject: [PATCH 09/15] [JENKINS-38606] Typos --- core/src/main/java/hudson/model/AllView.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/hudson/model/AllView.java b/core/src/main/java/hudson/model/AllView.java index 3ceba50c9f..d1e0daaba0 100644 --- a/core/src/main/java/hudson/model/AllView.java +++ b/core/src/main/java/hudson/model/AllView.java @@ -121,7 +121,7 @@ public class AllView extends View { * Use this method to round-trip the primary view name, e.g. * {@code primaryView = applyJenkins38606Fixup(views, primaryView)} * NOTE: we can only fix the localized name of an {@link AllView} if it is the primary view as otherwise urls - * would change, whereas the primary view is special and does not normall get accessed by the + * would change, whereas the primary view is special and does not normally get accessed by the * {@code /view/_name_} url. * * @param views the list of views. From 559a5afc0f180eb6eec94e2c338675428415dcc9 Mon Sep 17 00:00:00 2001 From: Stephen Connolly Date: Fri, 28 Oct 2016 09:58:06 +0100 Subject: [PATCH 10/15] [JENKINS-38606] Require the user to Opt-in to the fix --- core/src/main/java/hudson/model/AllView.java | 50 +++++++++++--------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/core/src/main/java/hudson/model/AllView.java b/core/src/main/java/hudson/model/AllView.java index d1e0daaba0..b60a42d1cb 100644 --- a/core/src/main/java/hudson/model/AllView.java +++ b/core/src/main/java/hudson/model/AllView.java @@ -29,6 +29,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import javax.annotation.CheckForNull; import javax.annotation.Nonnull; +import jenkins.util.SystemProperties; import org.apache.commons.lang.StringUtils; import org.jenkinsci.Symbol; import org.kohsuke.stapler.DataBoundConstructor; @@ -117,7 +118,8 @@ public class AllView extends View { /** * Corrects the name of the {@link AllView} if and only if the {@link AllView} is the primary view and - * its name is one of the localized forms of {@link Messages#_Hudson_ViewName()}. + * its name is one of the localized forms of {@link Messages#_Hudson_ViewName()} and the user has opted in to + * fixing the view name by setting the system property {@code hudson.mode.AllView.JENKINS-38606} to {@code true}. * Use this method to round-trip the primary view name, e.g. * {@code primaryView = applyJenkins38606Fixup(views, primaryView)} * NOTE: we can only fix the localized name of an {@link AllView} if it is the primary view as otherwise urls @@ -136,31 +138,33 @@ public class AllView extends View { // modern name, we are safe return primaryView; } - AllView allView = null; - for (View v: views) { - if (DEFAULT_VIEW_NAME.equals(v.getViewName())) { - // name conflict, we cannot rename the all view anyway - return primaryView; - } - if (StringUtils.equals(v.getViewName(), primaryView)) { - if (v instanceof AllView) { - allView = (AllView) v; - } else { - // none of our business fixing as we can only safely fix the primary view + if (SystemProperties.getBoolean(AllView.class.getName()+".JENKINS-38606", false)) { + AllView allView = null; + for (View v : views) { + if (DEFAULT_VIEW_NAME.equals(v.getViewName())) { + // name conflict, we cannot rename the all view anyway return primaryView; } + if (StringUtils.equals(v.getViewName(), primaryView)) { + if (v instanceof AllView) { + allView = (AllView) v; + } else { + // none of our business fixing as we can only safely fix the primary view + return primaryView; + } + } } - } - if (allView != null) { - // the primary view is an AllView but using a non-default name - for (Locale l : Locale.getAvailableLocales()) { - if (primaryView.equals(Messages._Hudson_ViewName().toString(l))) { - // bingo JENKINS-38606 detected - LOGGER.log(Level.INFO, - "JENKINS-38606 detected for AllView in {0}; renaming view from {1} to {2}", - new Object[]{allView.owner.getUrl(), DEFAULT_VIEW_NAME}); - allView.name = DEFAULT_VIEW_NAME; - return DEFAULT_VIEW_NAME; + if (allView != null) { + // the primary view is an AllView but using a non-default name + for (Locale l : Locale.getAvailableLocales()) { + if (primaryView.equals(Messages._Hudson_ViewName().toString(l))) { + // bingo JENKINS-38606 detected + LOGGER.log(Level.INFO, + "JENKINS-38606 detected for AllView in {0}; renaming view from {1} to {2}", + new Object[]{allView.owner.getUrl(), DEFAULT_VIEW_NAME}); + allView.name = DEFAULT_VIEW_NAME; + return DEFAULT_VIEW_NAME; + } } } } From beaabb0901ef161bc6aa0aa59a99b99ce87fb456 Mon Sep 17 00:00:00 2001 From: Stephen Connolly Date: Tue, 1 Nov 2016 12:57:16 +0000 Subject: [PATCH 11/15] [JENKINS-39300] Sort the views by displayName --- .../main/java/hudson/views/MyViewsTabBar.java | 29 +++++++++++++++++++ .../main/java/hudson/views/ViewsTabBar.java | 29 +++++++++++++++++++ .../DefaultMyViewsTabBar/myViewTabs.jelly | 2 +- .../views/DefaultViewsTabBar/viewTabs.jelly | 2 +- 4 files changed, 60 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/hudson/views/MyViewsTabBar.java b/core/src/main/java/hudson/views/MyViewsTabBar.java index c166c71e7d..45cba48b49 100644 --- a/core/src/main/java/hudson/views/MyViewsTabBar.java +++ b/core/src/main/java/hudson/views/MyViewsTabBar.java @@ -28,11 +28,19 @@ import hudson.Extension; import hudson.ExtensionPoint; import hudson.model.AbstractDescribableImpl; import hudson.model.Descriptor; +import hudson.model.View; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import javax.annotation.Nonnull; import jenkins.model.GlobalConfiguration; import jenkins.model.Jenkins; import hudson.model.MyViewsProperty; import net.sf.json.JSONObject; import org.jenkinsci.Symbol; +import org.kohsuke.accmod.Restricted; +import org.kohsuke.accmod.restrictions.NoExternalUse; import org.kohsuke.stapler.StaplerRequest; /** @@ -63,6 +71,27 @@ public abstract class MyViewsTabBar extends AbstractDescribableImpl sort(@Nonnull List views) { + List result = new ArrayList<>(views); + Collections.sort(result, new Comparator() { + @Override + public int compare(View o1, View o2) { + return o1.getDisplayName().compareTo(o2.getDisplayName()); + } + }); + return result; + } + /** * Configures {@link ViewsTabBar} in the system configuration. * diff --git a/core/src/main/java/hudson/views/ViewsTabBar.java b/core/src/main/java/hudson/views/ViewsTabBar.java index 579f110ce5..f529232672 100644 --- a/core/src/main/java/hudson/views/ViewsTabBar.java +++ b/core/src/main/java/hudson/views/ViewsTabBar.java @@ -29,11 +29,19 @@ import hudson.ExtensionPoint; import hudson.model.AbstractDescribableImpl; import hudson.model.Descriptor; import hudson.model.Descriptor.FormException; +import hudson.model.View; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import javax.annotation.Nonnull; import jenkins.model.GlobalConfiguration; import jenkins.model.Jenkins; import hudson.model.ListView; import net.sf.json.JSONObject; import org.jenkinsci.Symbol; +import org.kohsuke.accmod.Restricted; +import org.kohsuke.accmod.restrictions.NoExternalUse; import org.kohsuke.stapler.StaplerRequest; /** @@ -65,6 +73,27 @@ public abstract class ViewsTabBar extends AbstractDescribableImpl i return (ViewsTabBarDescriptor)super.getDescriptor(); } + /** + * Sorts the views by {@link View#getDisplayName()}. + * + * @param views the views. + * @return the sorted views + * @since FIXME + */ + @Nonnull + @Restricted(NoExternalUse.class) + @SuppressWarnings("unused") // invoked from stapler view + public List sort(@Nonnull List views) { + List result = new ArrayList<>(views); + Collections.sort(result, new Comparator() { + @Override + public int compare(View o1, View o2) { + return o1.getDisplayName().compareTo(o2.getDisplayName()); + } + }); + return result; + } + /** * Configures {@link ViewsTabBar} in the system configuration. * diff --git a/core/src/main/resources/hudson/views/DefaultMyViewsTabBar/myViewTabs.jelly b/core/src/main/resources/hudson/views/DefaultMyViewsTabBar/myViewTabs.jelly index 67a2ad0af0..e49686bf35 100644 --- a/core/src/main/resources/hudson/views/DefaultMyViewsTabBar/myViewTabs.jelly +++ b/core/src/main/resources/hudson/views/DefaultMyViewsTabBar/myViewTabs.jelly @@ -26,7 +26,7 @@ THE SOFTWARE. - + diff --git a/core/src/main/resources/hudson/views/DefaultViewsTabBar/viewTabs.jelly b/core/src/main/resources/hudson/views/DefaultViewsTabBar/viewTabs.jelly index 67a2ad0af0..e49686bf35 100644 --- a/core/src/main/resources/hudson/views/DefaultViewsTabBar/viewTabs.jelly +++ b/core/src/main/resources/hudson/views/DefaultViewsTabBar/viewTabs.jelly @@ -26,7 +26,7 @@ THE SOFTWARE. - + From 51066757760b86aca823fd21a667afcc1b4091e2 Mon Sep 17 00:00:00 2001 From: Stephen Connolly Date: Thu, 3 Nov 2016 21:55:37 +0000 Subject: [PATCH 12/15] [JENKINS-39300] Address review comments --- core/src/main/java/hudson/model/AllView.java | 6 +++-- core/src/main/java/hudson/model/View.java | 24 ++++++++++++------- .../java/hudson/model/ViewDescriptor.java | 22 ++++++++++++----- core/src/main/java/hudson/security/ACL.java | 20 ++++++++++++++++ .../hudson/model/Messages.properties | 1 - .../model/MyViewsProperty/newView.jelly | 2 +- .../jenkins/model/Jenkins/newView.jelly | 2 +- 7 files changed, 58 insertions(+), 19 deletions(-) diff --git a/core/src/main/java/hudson/model/AllView.java b/core/src/main/java/hudson/model/AllView.java index b60a42d1cb..a1ffdb9e25 100644 --- a/core/src/main/java/hudson/model/AllView.java +++ b/core/src/main/java/hudson/model/AllView.java @@ -122,9 +122,11 @@ public class AllView extends View { * fixing the view name by setting the system property {@code hudson.mode.AllView.JENKINS-38606} to {@code true}. * Use this method to round-trip the primary view name, e.g. * {@code primaryView = applyJenkins38606Fixup(views, primaryView)} + *

* NOTE: we can only fix the localized name of an {@link AllView} if it is the primary view as otherwise urls * would change, whereas the primary view is special and does not normally get accessed by the - * {@code /view/_name_} url. + * {@code /view/_name_} url. (Also note that there are some cases where the primary view will get accessed by + * its {@code /view/_name_} url which is why users need to opt-in to this fix. * * @param views the list of views. * @param primaryView the current primary view name. @@ -138,7 +140,7 @@ public class AllView extends View { // modern name, we are safe return primaryView; } - if (SystemProperties.getBoolean(AllView.class.getName()+".JENKINS-38606", false)) { + if (SystemProperties.getBoolean(AllView.class.getName()+".JENKINS-38606")) { AllView allView = null; for (View v : views) { if (DEFAULT_VIEW_NAME.equals(v.getViewName())) { diff --git a/core/src/main/java/hudson/model/View.java b/core/src/main/java/hudson/model/View.java index bb653c1189..ea3d911700 100644 --- a/core/src/main/java/hudson/model/View.java +++ b/core/src/main/java/hudson/model/View.java @@ -56,6 +56,7 @@ import hudson.util.RunList; import hudson.util.XStream2; import hudson.views.ListViewColumn; import hudson.widgets.Widget; +import javax.annotation.Nonnull; import jenkins.model.Jenkins; import jenkins.model.ModelObjectWithChildren; import jenkins.model.item_category.Categories; @@ -238,6 +239,7 @@ public abstract class View extends AbstractModelObject implements AccessControll * @see #rename(String) */ @Exported(visibility=2,name="name") + @Nonnull public String getViewName() { return name; } @@ -1221,12 +1223,22 @@ public abstract class View extends AbstractModelObject implements AccessControll /** * Returns the {@link ViewDescriptor} instances that can be instantiated for the {@link ViewGroup} in the current - * {@link StaplerRequest}. NOTE: This method must be called from a {@link StaplerRequest} + * {@link StaplerRequest}. + *

+ * NOTE: Historically this method is only ever called from a {@link StaplerRequest} * @return the list of instantiable {@link ViewDescriptor} instances for the current {@link StaplerRequest} */ + @Nonnull public static List allInstantiable() { List r = new ArrayList(); - ViewGroup owner = Stapler.getCurrentRequest().findAncestorObject(ViewGroup.class); + StaplerRequest request = Stapler.getCurrentRequest(); + if (request == null) { + throw new IllegalStateException("This method can only be invoked from a stapler request"); + } + ViewGroup owner = request.findAncestorObject(ViewGroup.class); + if (owner == null) { + throw new IllegalStateException("This method can only be invoked from a request with a ViewGroup ancestor"); + } for (ViewDescriptor d : DescriptorVisibilityFilter.apply(owner, all())) { if (d.isApplicableIn(owner) && d.isInstantiable() && owner.getACL().hasCreatePermission(Jenkins.getAuthentication(), owner, d)) { @@ -1277,9 +1289,7 @@ public abstract class View extends AbstractModelObject implements AccessControll if (mode==null || mode.length()==0) { if(isXmlSubmission) { View v = createViewFromXML(name, req.getInputStream()); - if (!owner.getACL().hasCreatePermission(Jenkins.getAuthentication(), owner, v.getDescriptor())) { - throw new Failure(Messages.View_CreatePermissionMissing(v.getDescriptor().getDisplayName())); - } + owner.getACL().checkCreatePermission(owner, v.getDescriptor()); v.owner = owner; rsp.setStatus(HttpServletResponse.SC_OK); return v; @@ -1299,9 +1309,7 @@ public abstract class View extends AbstractModelObject implements AccessControll // create a view v = descriptor.newInstance(req,req.getSubmittedForm()); } - if (!owner.getACL().hasCreatePermission(Jenkins.getAuthentication(), owner, v.getDescriptor())) { - throw new Failure(Messages.View_CreatePermissionMissing(v.getDescriptor().getDisplayName())); - } + owner.getACL().checkCreatePermission(owner, v.getDescriptor()); v.owner = owner; // redirect to the config screen diff --git a/core/src/main/java/hudson/model/ViewDescriptor.java b/core/src/main/java/hudson/model/ViewDescriptor.java index f6ef0c7ba9..e4f6dceb56 100644 --- a/core/src/main/java/hudson/model/ViewDescriptor.java +++ b/core/src/main/java/hudson/model/ViewDescriptor.java @@ -111,9 +111,12 @@ public abstract class ViewDescriptor extends Descriptor { */ public List> getColumnsDescriptors() { StaplerRequest request = Stapler.getCurrentRequest(); - View view = request.findAncestorObject(clazz); - return view == null ? DescriptorVisibilityFilter.applyType(clazz, ListViewColumn.all()) - : DescriptorVisibilityFilter.apply(view, ListViewColumn.all()); + if (request != null) { + View view = request.findAncestorObject(clazz); + return view == null ? DescriptorVisibilityFilter.applyType(clazz, ListViewColumn.all()) + : DescriptorVisibilityFilter.apply(view, ListViewColumn.all()); + } + return ListViewColumn.all(); } /** @@ -121,9 +124,12 @@ public abstract class ViewDescriptor extends Descriptor { */ public List> getJobFiltersDescriptors() { StaplerRequest request = Stapler.getCurrentRequest(); - View view = request.findAncestorObject(clazz); - return view == null ? DescriptorVisibilityFilter.applyType(clazz, ViewJobFilter.all()) - : DescriptorVisibilityFilter.apply(view, ViewJobFilter.all()); + if (request != null) { + View view = request.findAncestorObject(clazz); + return view == null ? DescriptorVisibilityFilter.applyType(clazz, ViewJobFilter.all()) + : DescriptorVisibilityFilter.apply(view, ViewJobFilter.all()); + } + return ViewJobFilter.all(); } /** @@ -153,6 +159,8 @@ public abstract class ViewDescriptor extends Descriptor { /** * Returns true if this {@link View} type is applicable to the given {@link ViewGroup} type. + *

+ * Default implementation returns {@code true} always. * * @return true to indicate applicable, in which case the view will be instantiable within the type of owner. * @since FIXME @@ -163,6 +171,8 @@ public abstract class ViewDescriptor extends Descriptor { /** * Returns true if this {@link View} type is applicable in the specific {@link ViewGroup}. + *

+ * Default implementation returns {@link #isApplicable(Class)} for the {@link ViewGroup#getClass()}. * * @return true to indicate applicable, in which case the view will be instantiable within the given owner. * @since FIXME diff --git a/core/src/main/java/hudson/security/ACL.java b/core/src/main/java/hudson/security/ACL.java index e2cea2cb03..fd5b1e8e0b 100644 --- a/core/src/main/java/hudson/security/ACL.java +++ b/core/src/main/java/hudson/security/ACL.java @@ -24,6 +24,7 @@ package hudson.security; import hudson.model.User; +import hudson.model.View; import hudson.model.ViewDescriptor; import hudson.model.ViewGroup; import javax.annotation.CheckForNull; @@ -122,6 +123,25 @@ public abstract class ACL { return true; } + /** + * Checks if the current security principal has the permission to create views within the specified view group. + *

+ * This is just a convenience function. + * + * @param c the container of the item. + * @param d the descriptor of the view to be created. + * @throws AccessDeniedException if the user doesn't have the permission. + * @since 1.607 + */ + public final void checkCreatePermission(@Nonnull ViewGroup c, + @Nonnull ViewDescriptor d) { + Authentication a = Jenkins.getAuthentication(); + if (!hasCreatePermission(a, c, d)) { + throw new AccessDeniedException(Messages.AccessDeniedException2_MissingPermission(a.getName(), + View.CREATE.group.title + "/" + View.CREATE.name + View.CREATE + "/" + d.getDisplayName())); + } + } + /** * Checks if the given principal has the permission to create views within the specified view group. *

diff --git a/core/src/main/resources/hudson/model/Messages.properties b/core/src/main/resources/hudson/model/Messages.properties index 6571451ff6..61b7886f3e 100644 --- a/core/src/main/resources/hudson/model/Messages.properties +++ b/core/src/main/resources/hudson/model/Messages.properties @@ -263,7 +263,6 @@ View.ReadPermission.Description=\ View.MissingMode=No view type is specified View.DisplayNameNotUniqueWarning=The display name, "{0}", is already in use by another view and \ could cause confusion and delay. -View.CreatePermissionMissing=You are not permitted to create a {0} view UpdateCenter.Status.CheckingInternet=Checking internet connectivity UpdateCenter.Status.CheckingJavaNet=Checking update center connectivity diff --git a/core/src/main/resources/hudson/model/MyViewsProperty/newView.jelly b/core/src/main/resources/hudson/model/MyViewsProperty/newView.jelly index fd469596f0..8c2f1cf13e 100644 --- a/core/src/main/resources/hudson/model/MyViewsProperty/newView.jelly +++ b/core/src/main/resources/hudson/model/MyViewsProperty/newView.jelly @@ -32,7 +32,7 @@ THE SOFTWARE. - + diff --git a/core/src/main/resources/jenkins/model/Jenkins/newView.jelly b/core/src/main/resources/jenkins/model/Jenkins/newView.jelly index 3f7548721a..823ed4a97b 100644 --- a/core/src/main/resources/jenkins/model/Jenkins/newView.jelly +++ b/core/src/main/resources/jenkins/model/Jenkins/newView.jelly @@ -31,7 +31,7 @@ THE SOFTWARE. - + From fd2009a9712b8b0a8c310d687df07df60edbc8ad Mon Sep 17 00:00:00 2001 From: Stephen Connolly Date: Wed, 7 Dec 2016 09:54:04 +0000 Subject: [PATCH 13/15] [JENKINS-38606] Allow legacy localized links to continue working after migration - This means we can default the migration to enabled --- core/src/main/java/hudson/model/AllView.java | 11 ++++++----- core/src/main/java/hudson/model/MyViewsProperty.java | 2 +- core/src/main/java/hudson/model/ViewGroupMixIn.java | 11 +++++++++++ core/src/main/java/jenkins/model/Jenkins.java | 2 +- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/hudson/model/AllView.java b/core/src/main/java/hudson/model/AllView.java index a1ffdb9e25..06122f89c6 100644 --- a/core/src/main/java/hudson/model/AllView.java +++ b/core/src/main/java/hudson/model/AllView.java @@ -118,15 +118,15 @@ public class AllView extends View { /** * Corrects the name of the {@link AllView} if and only if the {@link AllView} is the primary view and - * its name is one of the localized forms of {@link Messages#_Hudson_ViewName()} and the user has opted in to - * fixing the view name by setting the system property {@code hudson.mode.AllView.JENKINS-38606} to {@code true}. + * its name is one of the localized forms of {@link Messages#_Hudson_ViewName()} and the user has not opted out of + * fixing the view name by setting the system property {@code hudson.mode.AllView.JENKINS-38606} to {@code false}. * Use this method to round-trip the primary view name, e.g. * {@code primaryView = applyJenkins38606Fixup(views, primaryView)} *

* NOTE: we can only fix the localized name of an {@link AllView} if it is the primary view as otherwise urls * would change, whereas the primary view is special and does not normally get accessed by the * {@code /view/_name_} url. (Also note that there are some cases where the primary view will get accessed by - * its {@code /view/_name_} url which is why users need to opt-in to this fix. + * its {@code /view/_name_} url which will then fall back to the primary view) * * @param views the list of views. * @param primaryView the current primary view name. @@ -135,12 +135,13 @@ public class AllView extends View { * @since FIXME */ @Nonnull - public static String applyJenkins38606Fixup(@Nonnull List views, @Nonnull String primaryView) { + public static String migrateLegacyPrimaryAllViewLocalizedName(@Nonnull List views, + @Nonnull String primaryView) { if (DEFAULT_VIEW_NAME.equals(primaryView)) { // modern name, we are safe return primaryView; } - if (SystemProperties.getBoolean(AllView.class.getName()+".JENKINS-38606")) { + if (SystemProperties.getBoolean(AllView.class.getName()+".JENKINS-38606", true)) { AllView allView = null; for (View v : views) { if (DEFAULT_VIEW_NAME.equals(v.getViewName())) { diff --git a/core/src/main/java/hudson/model/MyViewsProperty.java b/core/src/main/java/hudson/model/MyViewsProperty.java index 363bab4478..6220cf0777 100644 --- a/core/src/main/java/hudson/model/MyViewsProperty.java +++ b/core/src/main/java/hudson/model/MyViewsProperty.java @@ -87,7 +87,7 @@ public class MyViewsProperty extends UserProperty implements ModifiableViewGroup // preserve the non-empty invariant views.add(new AllView(AllView.DEFAULT_VIEW_NAME, this)); } - primaryViewName = AllView.applyJenkins38606Fixup(views, primaryViewName); + primaryViewName = AllView.migrateLegacyPrimaryAllViewLocalizedName(views, primaryViewName); viewGroupMixIn = new ViewGroupMixIn(this) { protected List views() { return views; } diff --git a/core/src/main/java/hudson/model/ViewGroupMixIn.java b/core/src/main/java/hudson/model/ViewGroupMixIn.java index e390005edb..dcdc5a803c 100644 --- a/core/src/main/java/hudson/model/ViewGroupMixIn.java +++ b/core/src/main/java/hudson/model/ViewGroupMixIn.java @@ -26,6 +26,8 @@ package hudson.model; import hudson.model.ItemGroupMixIn; import hudson.model.View; import hudson.model.ViewGroup; +import java.util.Locale; +import java.util.logging.Level; import org.kohsuke.stapler.export.Exported; import java.io.IOException; @@ -99,6 +101,15 @@ public abstract class ViewGroupMixIn { View pv = getPrimaryView(); if (pv instanceof ViewGroup) return ((ViewGroup)pv).getView(name); + if (pv instanceof AllView && AllView.DEFAULT_VIEW_NAME.equals(pv.name)) { + // JENKINS-38606: primary view is the default AllView, is somebody using an old link to localized form? + for (Locale l : Locale.getAvailableLocales()) { + if (name.equals(Messages._Hudson_ViewName().toString(l))) { + // why yes they are, let's keep that link working + return pv; + } + } + } } return null; } diff --git a/core/src/main/java/jenkins/model/Jenkins.java b/core/src/main/java/jenkins/model/Jenkins.java index d5d4566ec8..d0c07bc41e 100644 --- a/core/src/main/java/jenkins/model/Jenkins.java +++ b/core/src/main/java/jenkins/model/Jenkins.java @@ -3057,7 +3057,7 @@ public class Jenkins extends AbstractCIBase implements DirectlyModifiableTopLeve views.add(0,v); primaryView = v.getViewName(); } - primaryView =AllView.applyJenkins38606Fixup(views, primaryView); + primaryView = AllView.migrateLegacyPrimaryAllViewLocalizedName(views, primaryView); if (useSecurity!=null && !useSecurity) { // forced reset to the unsecure mode. From d1c40f4b83b723f37f1f1c5f456eb96d728c8ec4 Mon Sep 17 00:00:00 2001 From: Stephen Connolly Date: Fri, 9 Dec 2016 12:46:24 +0000 Subject: [PATCH 14/15] [JENKINS-39300] Update tests --- .../hudson/cli/DeleteViewCommandTest.java | 11 +++-- test/src/test/java/hudson/model/ApiTest.java | 4 +- .../hudson/model/MyViewsPropertyTest.java | 47 ++++++++++--------- 3 files changed, 32 insertions(+), 30 deletions(-) diff --git a/test/src/test/java/hudson/cli/DeleteViewCommandTest.java b/test/src/test/java/hudson/cli/DeleteViewCommandTest.java index d9ff9f708a..7280a65b29 100644 --- a/test/src/test/java/hudson/cli/DeleteViewCommandTest.java +++ b/test/src/test/java/hudson/cli/DeleteViewCommandTest.java @@ -33,6 +33,7 @@ import static hudson.cli.CLICommandInvoker.Matcher.hasNoStandardOutput; import static hudson.cli.CLICommandInvoker.Matcher.succeededSilently; import static hudson.cli.CLICommandInvoker.Matcher.failedWith; +import hudson.model.AllView; import java.io.IOException; import hudson.model.ListView; @@ -116,12 +117,12 @@ public class DeleteViewCommandTest { final CLICommandInvoker.Result result = command .authorizedTo(View.READ, View.DELETE, Jenkins.READ) - .invokeWithArgs("All") + .invokeWithArgs(AllView.DEFAULT_VIEW_NAME) ; assertThat(result, failedWith(4)); assertThat(result, hasNoStandardOutput()); - assertThat(j.jenkins.getView("All"), notNullValue()); + assertThat(j.jenkins.getView(AllView.DEFAULT_VIEW_NAME), notNullValue()); assertThat(result.stderr(), containsString("ERROR: Jenkins does not allow to delete 'All' view")); } @@ -262,15 +263,15 @@ public class DeleteViewCommandTest { final CLICommandInvoker.Result result = command .authorizedTo(View.READ, View.DELETE, Jenkins.READ) - .invokeWithArgs("aView1", "aView2", "All"); + .invokeWithArgs("aView1", "aView2", AllView.DEFAULT_VIEW_NAME); assertThat(result, failedWith(5)); assertThat(result, hasNoStandardOutput()); - assertThat(result.stderr(), containsString("All: Jenkins does not allow to delete 'All' view")); + assertThat(result.stderr(), containsString(AllView.DEFAULT_VIEW_NAME+": Jenkins does not allow to delete '"+ AllView.DEFAULT_VIEW_NAME+"' view")); assertThat(result.stderr(), containsString("ERROR: " + CLICommand.CLI_LISTPARAM_SUMMARY_ERROR_TEXT)); assertThat(j.jenkins.getView("aView1"), nullValue()); assertThat(j.jenkins.getView("aView2"), nullValue()); - assertThat(j.jenkins.getView("All"), notNullValue()); + assertThat(j.jenkins.getView(AllView.DEFAULT_VIEW_NAME), notNullValue()); } } diff --git a/test/src/test/java/hudson/model/ApiTest.java b/test/src/test/java/hudson/model/ApiTest.java index 44dd587878..cffde63571 100644 --- a/test/src/test/java/hudson/model/ApiTest.java +++ b/test/src/test/java/hudson/model/ApiTest.java @@ -99,7 +99,7 @@ public class ApiTest { @Issue("JENKINS-3267") public void wrappedOneItem() throws Exception { Page page = j.createWebClient().goTo("api/xml?wrapper=root&xpath=/hudson/view/name", "application/xml"); - assertEquals("All", page.getWebResponse().getContentAsString()); + assertEquals(""+ AllView.DEFAULT_VIEW_NAME+"", page.getWebResponse().getContentAsString()); } @Test @@ -118,7 +118,7 @@ public class ApiTest { @Test public void unwrappedOneItem() throws Exception { Page page = j.createWebClient().goTo("api/xml?xpath=/hudson/view/name", "application/xml"); - assertEquals("All", page.getWebResponse().getContentAsString()); + assertEquals(""+ AllView.DEFAULT_VIEW_NAME+"", page.getWebResponse().getContentAsString()); } @Test diff --git a/test/src/test/java/hudson/model/MyViewsPropertyTest.java b/test/src/test/java/hudson/model/MyViewsPropertyTest.java index 666a383d20..1abfcacd35 100644 --- a/test/src/test/java/hudson/model/MyViewsPropertyTest.java +++ b/test/src/test/java/hudson/model/MyViewsPropertyTest.java @@ -47,17 +47,17 @@ public class MyViewsPropertyTest { @Test public void testReadResolve() throws IOException { User user = User.get("User"); - MyViewsProperty property = new MyViewsProperty(Messages.Hudson_ViewName()); + MyViewsProperty property = new MyViewsProperty(AllView.DEFAULT_VIEW_NAME); property.setUser(user); user.addProperty(property); property.readResolve(); - assertNotNull("Property should contains " + Messages.Hudson_ViewName() + " defaultly.", property.getView(Messages.Hudson_ViewName())); + assertNotNull("Property should contains " + AllView.DEFAULT_VIEW_NAME + " defaultly.", property.getView(AllView.DEFAULT_VIEW_NAME)); } @Test public void testSave() throws IOException { User user = User.get("User"); - MyViewsProperty property = new MyViewsProperty(Messages.Hudson_ViewName()); + MyViewsProperty property = new MyViewsProperty(AllView.DEFAULT_VIEW_NAME); property.readResolve(); property.setUser(user); user.addProperty(property); @@ -79,10 +79,10 @@ public class MyViewsPropertyTest { @Test public void testGetViews() throws IOException { User user = User.get("User"); - MyViewsProperty property = new MyViewsProperty(Messages.Hudson_ViewName()); + MyViewsProperty property = new MyViewsProperty(AllView.DEFAULT_VIEW_NAME); property.readResolve(); property.setUser(user); - assertTrue("Property should contains " + Messages.Hudson_ViewName(), property.getViews().contains(property.getView(Messages.Hudson_ViewName()))); + assertTrue("Property should contains " + AllView.DEFAULT_VIEW_NAME, property.getViews().contains(property.getView(AllView.DEFAULT_VIEW_NAME))); View view = new ListView("foo",property); property.addView(view); assertTrue("Property should contains " + view.name, property.getViews().contains(view)); @@ -91,10 +91,11 @@ public class MyViewsPropertyTest { @Test public void testGetView() throws IOException { User user = User.get("User"); - MyViewsProperty property = new MyViewsProperty(Messages.Hudson_ViewName()); + MyViewsProperty property = new MyViewsProperty(AllView.DEFAULT_VIEW_NAME); property.readResolve(); property.setUser(user); - assertNotNull("Property should contains " + Messages.Hudson_ViewName(), property.getView(Messages.Hudson_ViewName())); + assertNotNull("Property should contains " + AllView.DEFAULT_VIEW_NAME, property.getView( + AllView.DEFAULT_VIEW_NAME)); View view = new ListView("foo",property); property.addView(view); assertEquals("Property should contains " + view.name, view, property.getView(view.name)); @@ -103,11 +104,11 @@ public class MyViewsPropertyTest { @Test public void testGetPrimaryView() throws IOException { User user = User.get("User"); - MyViewsProperty property = new MyViewsProperty(Messages.Hudson_ViewName()); + MyViewsProperty property = new MyViewsProperty(AllView.DEFAULT_VIEW_NAME); property.readResolve(); property.setUser(user); user.addProperty(property); - assertEquals("Property should have primary view " + Messages.Hudson_ViewName() + " instead of " + property.getPrimaryView(). name,property.getView(Messages.Hudson_ViewName()), property.getPrimaryView()); + assertEquals("Property should have primary view " + AllView.DEFAULT_VIEW_NAME + " instead of " + property.getPrimaryView(). name,property.getView(AllView.DEFAULT_VIEW_NAME), property.getPrimaryView()); View view = new ListView("foo", property); property.addView(view); property.setPrimaryViewName(view.name); @@ -117,35 +118,35 @@ public class MyViewsPropertyTest { @Test public void testCanDelete() throws IOException { User user = User.get("User"); - MyViewsProperty property = new MyViewsProperty(Messages.Hudson_ViewName()); + MyViewsProperty property = new MyViewsProperty(AllView.DEFAULT_VIEW_NAME); property.readResolve(); property.setUser(user); user.addProperty(property); - assertFalse("Property should not enable to delete view " + Messages.Hudson_ViewName(), property.canDelete(property.getView(Messages.Hudson_ViewName()))); + assertFalse("Property should not enable to delete view " + AllView.DEFAULT_VIEW_NAME, property.canDelete(property.getView(AllView.DEFAULT_VIEW_NAME))); View view = new ListView("foo", property); property.addView(view); assertTrue("Property should enable to delete view " + view.name , property.canDelete(view)); property.setPrimaryViewName(view.name); assertFalse("Property should not enable to delete view " + view.name , property.canDelete(view)); - assertTrue("Property should enable to delete view " + Messages.Hudson_ViewName(), property.canDelete(property.getView(Messages.Hudson_ViewName()))); + assertTrue("Property should enable to delete view " + AllView.DEFAULT_VIEW_NAME, property.canDelete(property.getView(AllView.DEFAULT_VIEW_NAME))); } @Test public void testDeleteView() throws IOException { User user = User.get("User"); - MyViewsProperty property = new MyViewsProperty(Messages.Hudson_ViewName()); + MyViewsProperty property = new MyViewsProperty(AllView.DEFAULT_VIEW_NAME); property.readResolve(); property.setUser(user); user.addProperty(property); boolean ex = false; try{ - property.deleteView(property.getView(Messages.Hudson_ViewName())); + property.deleteView(property.getView(AllView.DEFAULT_VIEW_NAME)); } catch(IllegalStateException e){ ex = true; } assertTrue("Property should throw IllegalStateException.", ex); - assertTrue("Property should contain view " + Messages.Hudson_ViewName(), property.getViews().contains(property.getView(Messages.Hudson_ViewName()))); + assertTrue("Property should contain view " + AllView.DEFAULT_VIEW_NAME, property.getViews().contains(property.getView(AllView.DEFAULT_VIEW_NAME))); View view = new ListView("foo", property); property.addView(view); ex = false; @@ -159,14 +160,14 @@ public class MyViewsPropertyTest { property.addView(view); property.setPrimaryViewName(view.name); assertTrue("Property should not contain view " + view.name , property.getViews().contains(view)); - property.deleteView(property.getView(Messages.Hudson_ViewName())); - assertFalse("Property should not contains view " + Messages.Hudson_ViewName(), property.getViews().contains(property.getView(Messages.Hudson_ViewName()))); + property.deleteView(property.getView(AllView.DEFAULT_VIEW_NAME)); + assertFalse("Property should not contains view " + AllView.DEFAULT_VIEW_NAME, property.getViews().contains(property.getView(AllView.DEFAULT_VIEW_NAME))); } @Test public void testOnViewRenamed() throws IOException, Failure, FormException { User user = User.get("User"); - MyViewsProperty property = new MyViewsProperty(Messages.Hudson_ViewName()); + MyViewsProperty property = new MyViewsProperty(AllView.DEFAULT_VIEW_NAME); property.readResolve(); property.setUser(user); user.addProperty(property); @@ -180,7 +181,7 @@ public class MyViewsPropertyTest { @Test public void testAddView() throws IOException { User user = User.get("User"); - MyViewsProperty property = new MyViewsProperty(Messages.Hudson_ViewName()); + MyViewsProperty property = new MyViewsProperty(AllView.DEFAULT_VIEW_NAME); property.readResolve(); property.setUser(user); user.addProperty(property); @@ -196,7 +197,7 @@ public class MyViewsPropertyTest { @Test public void testDoCreateView() throws Exception { User user = User.get("User"); - MyViewsProperty property = new MyViewsProperty(Messages.Hudson_ViewName()); + MyViewsProperty property = new MyViewsProperty(AllView.DEFAULT_VIEW_NAME); property.readResolve(); property.setUser(user); user.addProperty(property); @@ -213,7 +214,7 @@ public class MyViewsPropertyTest { @Test public void testGetACL() throws IOException { User user = User.get("User"); - MyViewsProperty property = new MyViewsProperty(Messages.Hudson_ViewName()); + MyViewsProperty property = new MyViewsProperty(AllView.DEFAULT_VIEW_NAME); property.readResolve(); property.setUser(user); user.addProperty(property); @@ -227,7 +228,7 @@ public class MyViewsPropertyTest { rule.jenkins.setSecurityRealm(rule.createDummySecurityRealm()); User user = User.get("User"); User user2 = User.get("User2"); - MyViewsProperty property = new MyViewsProperty(Messages.Hudson_ViewName()); + MyViewsProperty property = new MyViewsProperty(AllView.DEFAULT_VIEW_NAME); property.readResolve(); property.setUser(user); GlobalMatrixAuthorizationStrategy auth = new GlobalMatrixAuthorizationStrategy(); @@ -264,7 +265,7 @@ public class MyViewsPropertyTest { rule.jenkins.setSecurityRealm(rule.createDummySecurityRealm()); User user = User.get("User"); User user2 = User.get("User2"); - MyViewsProperty property = new MyViewsProperty(Messages.Hudson_ViewName()); + MyViewsProperty property = new MyViewsProperty(AllView.DEFAULT_VIEW_NAME); property.readResolve(); property.setUser(user); GlobalMatrixAuthorizationStrategy auth = new GlobalMatrixAuthorizationStrategy(); From 3538931d1e2ff736adeb172f4947ee9ea20ac59c Mon Sep 17 00:00:00 2001 From: Stephen Connolly Date: Sun, 11 Dec 2016 17:09:04 +0000 Subject: [PATCH 15/15] [JENKINS-39300] Fix final test case --- test/src/test/java/hudson/cli/DeleteViewCommandTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/src/test/java/hudson/cli/DeleteViewCommandTest.java b/test/src/test/java/hudson/cli/DeleteViewCommandTest.java index 7280a65b29..20b36e87a5 100644 --- a/test/src/test/java/hudson/cli/DeleteViewCommandTest.java +++ b/test/src/test/java/hudson/cli/DeleteViewCommandTest.java @@ -123,7 +123,7 @@ public class DeleteViewCommandTest { assertThat(result, failedWith(4)); assertThat(result, hasNoStandardOutput()); assertThat(j.jenkins.getView(AllView.DEFAULT_VIEW_NAME), notNullValue()); - assertThat(result.stderr(), containsString("ERROR: Jenkins does not allow to delete 'All' view")); + assertThat(result.stderr(), containsString("ERROR: Jenkins does not allow to delete '"+AllView.DEFAULT_VIEW_NAME+"' view")); } @Test public void deleteViewShoudlFailIfViewNameIsEmpty() {