提交 363cc0e3 编写于 作者: N Nicolas De Loof

Merge branch 'Vlatombe/user-transient-actions'

...@@ -99,6 +99,8 @@ Upcoming changes</a> ...@@ -99,6 +99,8 @@ Upcoming changes</a>
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-14327">issue 14327</a>) (<a href="https://issues.jenkins-ci.org/browse/JENKINS-14327">issue 14327</a>)
<li class=rfe> <li class=rfe>
Option to set java executable path for managed windows slaves Option to set java executable path for managed windows slaves
<li class=rfe>
Added new extension point for transient user actions, and displays user properties if they are also Actions.
</ul> </ul>
</div><!--=TRUNK-END=--> </div><!--=TRUNK-END=-->
......
/*
* The MIT License
*
* Copyright (c) 2012, Vincent Latombe.
*
* 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 hudson.Extension;
import hudson.ExtensionList;
import hudson.ExtensionPoint;
import jenkins.model.Jenkins;
import java.util.Collection;
import java.util.Collections;
/**
* Extension point for inserting transient {@link Action}s into {@link User}s.
*
* To register your implementation, put {@link Extension} on your subtype.
*
* @author Vincent Latombe
* @since 1.477
* @see Action
*/
public abstract class TransientUserActionFactory implements ExtensionPoint {
/**
* Creates actions for the given user.
*
* @param target for which the action objects are requested. Never null.
* @return Can be empty but must not be null.
*/
public Collection<? extends Action> createFor(User target) {
return Collections.emptyList();
}
/**
* Returns all the registered {@link TransientUserActionFactory}s.
*/
public static ExtensionList<TransientUserActionFactory> all() {
return Jenkins.getInstance().getExtensionList(TransientUserActionFactory.class);
}
}
\ No newline at end of file
/* /*
* The MIT License * The MIT License
* *
* Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, Erik Ramfelt, Tom Huybrechts * Copyright (c) 2004-2012, Sun Microsystems, Inc., Kohsuke Kawaguchi, Erik Ramfelt,
* Tom Huybrechts, Vincent Latombe
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
...@@ -101,7 +102,7 @@ import java.util.logging.Logger; ...@@ -101,7 +102,7 @@ import java.util.logging.Logger;
*/ */
@ExportedBean @ExportedBean
public class User extends AbstractModelObject implements AccessControlled, DescriptorByNameOwner, Saveable, Comparable<User> { public class User extends AbstractModelObject implements AccessControlled, DescriptorByNameOwner, Saveable, Comparable<User> {
private transient final String id; private transient final String id;
private volatile String fullName; private volatile String fullName;
...@@ -591,15 +592,45 @@ public class User extends AbstractModelObject implements AccessControlled, Descr ...@@ -591,15 +592,45 @@ public class User extends AbstractModelObject implements AccessControlled, Descr
public Descriptor getDescriptorByName(String className) { public Descriptor getDescriptorByName(String className) {
return Jenkins.getInstance().getDescriptorByName(className); return Jenkins.getInstance().getDescriptorByName(className);
} }
public Object getDynamic(String token) { public Object getDynamic(String token) {
for (UserProperty property: getProperties().values()) { for(Action action: getTransientActions()){
if (property instanceof Action) { if(action.getUrlName().equals(token))
Action a= (Action) property; return action;
if(a.getUrlName().equals(token) || a.getUrlName().equals('/'+token)) }
return a; for(Action action: getPropertyActions()){
} if(action.getUrlName().equals(token))
return action;
} }
return null; return null;
} }
/**
* Return all properties that are also actions.
*
* @return the list can be empty but never null. read only.
*/
public List<Action> getPropertyActions() {
List<Action> actions = new ArrayList<Action>();
for (UserProperty userProp : getProperties().values()) {
if (userProp instanceof Action) {
actions.add((Action) userProp);
}
}
return Collections.unmodifiableList(actions);
}
/**
* Return all transient actions associated with this user.
*
* @return the list can be empty but never null. read only.
*/
public List<Action> getTransientActions() {
List<Action> actions = new ArrayList<Action>();
for (TransientUserActionFactory factory: TransientUserActionFactory.all()) {
actions.addAll(factory.createFor(this));
}
return Collections.unmodifiableList(actions);
}
} }
<!-- <!--
The MIT License The MIT License
Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Tom Huybrechts, id:cactusman Copyright (c) 2004-2012, Sun Microsystems, Inc., Kohsuke Kawaguchi, Tom Huybrechts,
id:cactusman, Vincent Latombe
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
...@@ -35,10 +36,11 @@ THE SOFTWARE. ...@@ -35,10 +36,11 @@ THE SOFTWARE.
<l:task icon="images/24x24/notepad.png" href="${rootURL}/${it.url}/builds" title="${%Builds}" /> <l:task icon="images/24x24/notepad.png" href="${rootURL}/${it.url}/builds" title="${%Builds}" />
<l:task icon="images/24x24/notepad.png" href="${rootURL}/${it.url}/my-views/" title="${%My Views}" /> <l:task icon="images/24x24/notepad.png" href="${rootURL}/${it.url}/my-views/" title="${%My Views}" />
<l:task icon="images/24x24/setting.png" href="${rootURL}/${it.url}/configure" title="${%Configure}" permission="${app.ADMINISTER}" /> <l:task icon="images/24x24/setting.png" href="${rootURL}/${it.url}/configure" title="${%Configure}" permission="${app.ADMINISTER}" />
<t:actions actions="${it.propertyActions}"/>
<t:actions actions="${it.transientActions}"/>
<j:if test="${it.canDelete()}"> <j:if test="${it.canDelete()}">
<l:task icon="images/24x24/edit-delete.png" href="${rootURL}/${it.url}/delete" title="${%Delete}" /> <l:task icon="images/24x24/edit-delete.png" href="${rootURL}/${it.url}/delete" title="${%Delete}" />
</j:if> </j:if>
<!-- TODO add all UserProperties that are also actions -->
</l:tasks> </l:tasks>
</l:side-panel> </l:side-panel>
</j:jelly> </j:jelly>
/* /*
* The MIT License * The MIT License
* *
* Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Erik Ramfelt * Copyright (c) 2004-2012, Sun Microsystems, Inc., Kohsuke Kawaguchi, Erik Ramfelt,
* Vincent Latombe
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
...@@ -29,7 +30,7 @@ import org.jvnet.hudson.test.HudsonTestCase; ...@@ -29,7 +30,7 @@ import org.jvnet.hudson.test.HudsonTestCase;
public class UserTestCase extends HudsonTestCase { public class UserTestCase extends HudsonTestCase {
public static class UserPropertyImpl extends UserProperty { public static class UserPropertyImpl extends UserProperty implements Action {
private final String testString; private final String testString;
private UserPropertyDescriptor descriptorImpl = new UserPropertyDescriptorImpl(); private UserPropertyDescriptor descriptorImpl = new UserPropertyDescriptorImpl();
...@@ -47,23 +48,35 @@ public class UserTestCase extends HudsonTestCase { ...@@ -47,23 +48,35 @@ public class UserTestCase extends HudsonTestCase {
return descriptorImpl; return descriptorImpl;
} }
public static class UserPropertyDescriptorImpl extends UserPropertyDescriptor { public String getIconFileName() {
@Override return "/images/24x24/gear.png";
public UserProperty newInstance(User user) { }
return null;
}
@Override public String getDisplayName() {
public String getDisplayName() { return "UserPropertyImpl";
return "Property";
}
} }
public String getUrlName() {
return "userpropertyimpl";
}
public static class UserPropertyDescriptorImpl extends UserPropertyDescriptor {
@Override
public UserProperty newInstance(User user) {
return null;
}
@Override
public String getDisplayName() {
return "Property";
}
}
} }
/** /**
* Asserts that bug# is fixed. * Asserts that bug# is fixed.
*/ */
public void testUserPropertySummaryIsShownInUserPage() throws Exception { public void testUserPropertySummaryAndActionAreShownInUserPage() throws Exception {
UserProperty property = new UserPropertyImpl("NeedleInPage"); UserProperty property = new UserPropertyImpl("NeedleInPage");
UserProperty.all().add(property.getDescriptor()); UserProperty.all().add(property.getDescriptor());
...@@ -72,9 +85,12 @@ public class UserTestCase extends HudsonTestCase { ...@@ -72,9 +85,12 @@ public class UserTestCase extends HudsonTestCase {
user.addProperty(property); user.addProperty(property);
HtmlPage page = new WebClient().goTo("user/user-test-case"); HtmlPage page = new WebClient().goTo("user/user-test-case");
WebAssert.assertTextPresent(page, "NeedleInPage");
WebAssert.assertTextPresentInElement(page, "NeedleInPage", "main-panel");
WebAssert.assertTextPresentInElement(page, ((Action) property).getDisplayName(), "side-panel");
} }
/** /**
* Asserts that the default user avatar can be fetched (ie no 404) * Asserts that the default user avatar can be fetched (ie no 404)
*/ */
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册