diff --git a/core/src/main/java/hudson/model/User.java b/core/src/main/java/hudson/model/User.java index 02502e13a0..1452c3d68a 100644 --- a/core/src/main/java/hudson/model/User.java +++ b/core/src/main/java/hudson/model/User.java @@ -132,7 +132,7 @@ public class User extends AbstractModelObject implements AccessControlled, Savea // allocate default instances if needed. // doing so after load makes sure that newly added user properties do get reflected - for (UserPropertyDescriptor d : UserProperties.LIST) { + for (UserPropertyDescriptor d : UserProperty.all()) { if(getProperty(d.clazz)==null) { UserProperty up = d.newInstance(this); if(up!=null) @@ -212,7 +212,7 @@ public class User extends AbstractModelObject implements AccessControlled, Savea } /** - * List of all {@link UserProperties} exposed primarily for the remoting API. + * List of all {@link UserProperty}s exposed primarily for the remoting API. */ @Exported(name="property",inline=true) public List getAllProperties() { @@ -415,7 +415,7 @@ public class User extends AbstractModelObject implements AccessControlled, Savea List props = new ArrayList(); int i=0; - for (Descriptor d : UserProperties.LIST) { + for (UserPropertyDescriptor d : UserProperty.all()) { UserProperty p = d.newInstance(req, json.getJSONObject("userProperty"+(i++))); p.setUser(this); props.add(p); diff --git a/core/src/main/java/hudson/model/UserProperties.java b/core/src/main/java/hudson/model/UserProperties.java index caa0e0174c..5561fe7caa 100644 --- a/core/src/main/java/hudson/model/UserProperties.java +++ b/core/src/main/java/hudson/model/UserProperties.java @@ -23,18 +23,20 @@ */ package hudson.model; -import hudson.tasks.Mailer; -import hudson.security.HudsonPrivateSecurityRealm; +import hudson.Extension; +import hudson.util.DescriptorList; import java.util.List; /** * List of all installed {@link UserProperty} types. * @author Kohsuke Kawaguchi + * @deprecated as of 1.286 */ public class UserProperties { - public static final List LIST = Descriptor.toList( - Mailer.UserProperty.DESCRIPTOR, - HudsonPrivateSecurityRealm.DETAILS_DESCRIPTOR - ); + /** + * @deprecated as of 1.286 + * Use {@link UserProperty#all()} for read access and {@link Extension} for auto-registration. + */ + public static final List LIST = (List)new DescriptorList(UserProperty.class); } diff --git a/core/src/main/java/hudson/model/UserProperty.java b/core/src/main/java/hudson/model/UserProperty.java index db2322985d..96617c20c4 100644 --- a/core/src/main/java/hudson/model/UserProperty.java +++ b/core/src/main/java/hudson/model/UserProperty.java @@ -25,6 +25,7 @@ package hudson.model; import hudson.ExtensionPoint; import hudson.Plugin; +import hudson.DescriptorExtensionList; import hudson.tasks.Mailer; import org.kohsuke.stapler.export.ExportedBean; @@ -44,7 +45,6 @@ import org.kohsuke.stapler.export.ExportedBean; * * * @author Kohsuke Kawaguchi - * @see UserProperties#LIST */ @ExportedBean public abstract class UserProperty implements Describable, ExtensionPoint { @@ -60,5 +60,14 @@ public abstract class UserProperty implements Describable, Extensi } // descriptor must be of the UserPropertyDescriptor type - public abstract UserPropertyDescriptor getDescriptor(); + public UserPropertyDescriptor getDescriptor() { + return (UserPropertyDescriptor)Hudson.getInstance().getDescriptor(getClass()); + } + + /** + * Returns all the registered {@link UserPropertyDescriptor}s. + */ + public static DescriptorExtensionList all() { + return Hudson.getInstance().getDescriptorList(UserProperty.class); + } } diff --git a/core/src/main/java/hudson/model/UserPropertyDescriptor.java b/core/src/main/java/hudson/model/UserPropertyDescriptor.java index 1b88707c51..5367c7c014 100644 --- a/core/src/main/java/hudson/model/UserPropertyDescriptor.java +++ b/core/src/main/java/hudson/model/UserPropertyDescriptor.java @@ -63,7 +63,7 @@ public abstract class UserPropertyDescriptor extends Descriptor { *

* Returning false from this method essentially has the same effect of * making Hudson behaves as if this {@link UserPropertyDescriptor} is - * not a part of {@link UserProperties#LIST}. + * not a part of {@link UserProperty#all()}. * *

* This mechanism is useful if the availability of the property is diff --git a/core/src/main/java/hudson/security/HudsonPrivateSecurityRealm.java b/core/src/main/java/hudson/security/HudsonPrivateSecurityRealm.java index 876d9e40fa..24a444a4fa 100644 --- a/core/src/main/java/hudson/security/HudsonPrivateSecurityRealm.java +++ b/core/src/main/java/hudson/security/HudsonPrivateSecurityRealm.java @@ -335,51 +335,48 @@ public class HudsonPrivateSecurityRealm extends SecurityRealm implements ModelOb return user==null; } - public UserPropertyDescriptor getDescriptor() { - return DETAILS_DESCRIPTOR; - } - private Object readResolve() { // If we are being read back in from an older version if (password!=null && passwordHash==null) passwordHash = PASSWORD_ENCODER.encodePassword(Scrambler.descramble(password),null); return this; } - } - public static final UserPropertyDescriptor DETAILS_DESCRIPTOR = new UserPropertyDescriptor(Details.class) { - public String getDisplayName() { - // this feature is only when HudsonPrivateSecurityRealm is enabled - if(isEnabled()) - return Messages.HudsonPrivateSecurityRealm_Details_DisplayName(); - else - return null; - } - - public Details newInstance(StaplerRequest req, JSONObject formData) throws FormException { - String pwd = Util.fixEmpty(req.getParameter("user.password")); - String pwd2= Util.fixEmpty(req.getParameter("user.password2")); - - if(!Util.fixNull(pwd).equals(Util.fixNull(pwd2))) - throw new FormException("Please confirm the password by typing it twice","user.password2"); - - String data = Protector.unprotect(pwd); - if(data!=null) { - String prefix = Stapler.getCurrentRequest().getSession().getId() + ':'; - if(data.startsWith(prefix)) - return Details.fromHashedPassword(data.substring(prefix.length())); + @Extension + public static final class DescriptorImpl extends UserPropertyDescriptor { + public String getDisplayName() { + // this feature is only when HudsonPrivateSecurityRealm is enabled + if(isEnabled()) + return Messages.HudsonPrivateSecurityRealm_Details_DisplayName(); + else + return null; } - return Details.fromPlainPassword(Util.fixNull(pwd)); - } - public boolean isEnabled() { - return Hudson.getInstance().getSecurityRealm() instanceof HudsonPrivateSecurityRealm; - } + public Details newInstance(StaplerRequest req, JSONObject formData) throws FormException { + String pwd = Util.fixEmpty(req.getParameter("user.password")); + String pwd2= Util.fixEmpty(req.getParameter("user.password2")); - public UserProperty newInstance(User user) { - return null; + if(!Util.fixNull(pwd).equals(Util.fixNull(pwd2))) + throw new FormException("Please confirm the password by typing it twice","user.password2"); + + String data = Protector.unprotect(pwd); + if(data!=null) { + String prefix = Stapler.getCurrentRequest().getSession().getId() + ':'; + if(data.startsWith(prefix)) + return Details.fromHashedPassword(data.substring(prefix.length())); + } + return Details.fromPlainPassword(Util.fixNull(pwd)); + } + + public boolean isEnabled() { + return Hudson.getInstance().getSecurityRealm() instanceof HudsonPrivateSecurityRealm; + } + + public UserProperty newInstance(User user) { + return null; + } } - }; + } /** * {@link UserDetailsService} that loads user information from {@link User} object. diff --git a/core/src/main/java/hudson/tasks/Mailer.java b/core/src/main/java/hudson/tasks/Mailer.java index 47fbbee1ca..c49e22bbb9 100644 --- a/core/src/main/java/hudson/tasks/Mailer.java +++ b/core/src/main/java/hudson/tasks/Mailer.java @@ -424,8 +424,6 @@ public class Mailer extends Notifier { * Per user property that is e-mail address. */ public static class UserProperty extends hudson.model.UserProperty { - public static final DescriptorImpl DESCRIPTOR = new DescriptorImpl(); - /** * The user's e-mail address. * Null to leave it to default. @@ -445,10 +443,7 @@ public class Mailer extends Notifier { return MailAddressResolver.resolve(user); } - public DescriptorImpl getDescriptor() { - return DESCRIPTOR; - } - + @Extension public static final class DescriptorImpl extends UserPropertyDescriptor { public String getDisplayName() { return Messages.Mailer_UserProperty_DisplayName(); diff --git a/test/src/test/java/hudson/model/UserTestCase.java b/test/src/test/java/hudson/model/UserTestCase.java index 945a87db74..079eb29f27 100644 --- a/test/src/test/java/hudson/model/UserTestCase.java +++ b/test/src/test/java/hudson/model/UserTestCase.java @@ -1,26 +1,26 @@ -/* - * The MIT License - * - * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Erik Ramfelt - * - * 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. - */ +/* + * The MIT License + * + * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Erik Ramfelt + * + * 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. + */ package hudson.model; import com.gargoylesoftware.htmlunit.WebAssert; @@ -66,7 +66,7 @@ public class UserTestCase extends HudsonTestCase { public void testUserPropertySummaryIsShownInUserPage() throws Exception { UserProperty property = new UserPropertyImpl("NeedleInPage"); - UserProperties.LIST.add(property.getDescriptor()); + UserProperty.all().add(property.getDescriptor()); User user = User.get("user-test-case"); user.addProperty(property);