mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-11-16 05:22:22 +00:00
Add a new Extension Point : TransientUserActionFactory
Displays transient user actions in the User sidebar Displays UserProperties that are also Action in the User sidebar
This commit is contained in:
1 parent
88b613e95f
commit
39572fbad7
5 files changed
+137
-26
No files matched your search
@@ -97,6 +97,8 @@ Upcoming changes</a>
|
||||
<li class=rfe>
|
||||
Add a setter for node label string.
|
||||
(<a href="https://issues.jenkins-ci.org/browse/JENKINS-14327">issue 14327</a>)
|
||||
<li class=rfe>
|
||||
Added new extension point for transient user actions, and displays user properties if they are also Actions.
|
||||
</ul>
|
||||
</div><!--=TRUNK-END=-->
|
||||
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
/*
|
||||
* 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
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -101,7 +102,7 @@ import java.util.logging.Logger;
|
||||
*/
|
||||
@ExportedBean
|
||||
public class User extends AbstractModelObject implements AccessControlled, DescriptorByNameOwner, Saveable, Comparable<User> {
|
||||
|
||||
|
||||
private transient final String id;
|
||||
|
||||
private volatile String fullName;
|
||||
@@ -591,15 +592,45 @@ public class User extends AbstractModelObject implements AccessControlled, Descr
|
||||
public Descriptor getDescriptorByName(String className) {
|
||||
return Jenkins.getInstance().getDescriptorByName(className);
|
||||
}
|
||||
|
||||
|
||||
public Object getDynamic(String token) {
|
||||
for (UserProperty property: getProperties().values()) {
|
||||
if (property instanceof Action) {
|
||||
Action a= (Action) property;
|
||||
if(a.getUrlName().equals(token) || a.getUrlName().equals('/'+token))
|
||||
return a;
|
||||
}
|
||||
for(Action action: getTransientActions()){
|
||||
if(action.getUrlName().equals(token))
|
||||
return action;
|
||||
}
|
||||
for(Action action: getPropertyActions()){
|
||||
if(action.getUrlName().equals(token))
|
||||
return action;
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
<!--
|
||||
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
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -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}/my-views/" title="${%My Views}" />
|
||||
<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()}">
|
||||
<l:task icon="images/24x24/edit-delete.png" href="${rootURL}/${it.url}/delete" title="${%Delete}" />
|
||||
</j:if>
|
||||
<!-- TODO add all UserProperties that are also actions -->
|
||||
</l:tasks>
|
||||
</l:side-panel>
|
||||
</j:jelly>
|
||||
@@ -1,7 +1,8 @@
|
||||
/*
|
||||
* 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
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -29,7 +30,7 @@ import org.jvnet.hudson.test.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 UserPropertyDescriptor descriptorImpl = new UserPropertyDescriptorImpl();
|
||||
@@ -47,23 +48,35 @@ public class UserTestCase extends HudsonTestCase {
|
||||
return descriptorImpl;
|
||||
}
|
||||
|
||||
public static class UserPropertyDescriptorImpl extends UserPropertyDescriptor {
|
||||
@Override
|
||||
public UserProperty newInstance(User user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDisplayName() {
|
||||
return "Property";
|
||||
}
|
||||
public String getIconFileName() {
|
||||
return "/images/24x24/gear.png";
|
||||
}
|
||||
|
||||
public String getDisplayName() {
|
||||
return "UserPropertyImpl";
|
||||
}
|
||||
|
||||
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.
|
||||
*/
|
||||
public void testUserPropertySummaryIsShownInUserPage() throws Exception {
|
||||
public void testUserPropertySummaryAndActionAreShownInUserPage() throws Exception {
|
||||
|
||||
UserProperty property = new UserPropertyImpl("NeedleInPage");
|
||||
UserProperty.all().add(property.getDescriptor());
|
||||
@@ -72,9 +85,12 @@ public class UserTestCase extends HudsonTestCase {
|
||||
user.addProperty(property);
|
||||
|
||||
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)
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user