updated UserProperty to support auto registration

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@15724 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
kohsuke committed 2009-02-25 04:17:10 +00:00
1 parent a0dc496276
commit eb5a64d10b
7 files changed
+79 -76

No files matched your search

+3 -3
View File
@@ -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<UserProperty> getAllProperties() {
@@ -415,7 +415,7 @@ public class User extends AbstractModelObject implements AccessControlled, Savea
List<UserProperty> props = new ArrayList<UserProperty>();
int i=0;
for (Descriptor<UserProperty> d : UserProperties.LIST) {
for (UserPropertyDescriptor d : UserProperty.all()) {
UserProperty p = d.newInstance(req, json.getJSONObject("userProperty"+(i++)));
p.setUser(this);
props.add(p);
@@ -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<UserPropertyDescriptor> 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<UserPropertyDescriptor> LIST = (List)new DescriptorList<UserProperty>(UserProperty.class);
}
@@ -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<UserProperty>, ExtensionPoint {
@@ -60,5 +60,14 @@ public abstract class UserProperty implements Describable<UserProperty>, 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<UserProperty,UserPropertyDescriptor> all() {
return Hudson.getInstance().getDescriptorList(UserProperty.class);
}
}
@@ -63,7 +63,7 @@ public abstract class UserPropertyDescriptor extends Descriptor<UserProperty> {
* <p>
* 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()}.
*
* <p>
* This mechanism is useful if the availability of the property is
@@ -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.
+1 -6
View File
@@ -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();