Skip to content
Permalink
Browse files
[JENKINS-28881] - Implementation of Ownership property injection
  • Loading branch information
oleg-nenashev committed Jan 2, 2016
1 parent 9d49867 commit 1458f9358784c43f1369fa53e4c66722873f3b16
Showing 12 changed files with 681 additions and 2 deletions.
20 pom.xml
@@ -3,7 +3,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.509.3</version>
<version>1.609.3</version>
</parent>

<groupId>com.synopsys.jenkinsci</groupId>
@@ -81,13 +81,31 @@
<version>0.1</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>cloudbees-folder</artifactId>
<version>5.1</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>authorize-project</artifactId>
<version>1.0.2</version>
<optional>true</optional>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>matrix-project</artifactId>
<version>1.6</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>matrix-auth</artifactId>
<version>1.2</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>mailer</artifactId>
@@ -39,7 +39,7 @@
* @author Oleg Nenashev, Synopsys Inc.
*/
@Extension
public class OwnershipActionFactory extends TransientProjectActionFactory {
public class OwnershipActionFactory extends TransientProjectActionFactory {
/**Empty actions collection for invalid project type*/
private static final Collection<? extends Action> EMPTY_ACTIONS
= new LinkedList<Action>();
@@ -0,0 +1,85 @@
/*
* The MIT License
*
* Copyright (c) 2015 Oleg Nenashev.
*
* 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 org.jenkinsci.plugins.ownership.folders;

import com.cloudbees.hudson.plugins.folder.AbstractFolder;
import com.cloudbees.hudson.plugins.folder.Folder;
import com.synopsys.arc.jenkins.plugins.ownership.IOwnershipHelper;
import com.synopsys.arc.jenkins.plugins.ownership.ItemOwnershipAction;
import com.synopsys.arc.jenkins.plugins.ownership.OwnershipDescription;
import com.synopsys.arc.jenkins.plugins.ownership.OwnershipPlugin;
import com.synopsys.arc.jenkins.plugins.ownership.jobs.JobOwnerHelper;
import hudson.model.Descriptor;
import hudson.security.Permission;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.annotation.Nonnull;
import javax.servlet.ServletException;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.HttpResponses;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

/**
* Allows managing actions for {@link Folder}s.
* @author Oleg Nenashev
* @since TODO
*/
public class FolderOwnershipAction extends ItemOwnershipAction<AbstractFolder<?>> {

public FolderOwnershipAction(@Nonnull Folder folder) {
super(folder);
}

@Override
public Permission getOwnerPermission() {
return OwnershipPlugin.MANAGE_ITEMS_OWNERSHIP;
}

@Override
public Permission getProjectSpecificPermission() {
return OwnershipPlugin.MANAGE_ITEMS_OWNERSHIP;
}

@Override
public boolean actionIsAvailable() {
return getDescribedItem().hasPermission(OwnershipPlugin.MANAGE_ITEMS_OWNERSHIP);
}

@Override
public IOwnershipHelper<AbstractFolder<?>> helper() {
return FolderOwnershipHelper.getInstance();
}

public HttpResponse doOwnersSubmit(StaplerRequest req, StaplerResponse rsp) throws IOException, UnsupportedEncodingException, ServletException, Descriptor.FormException {
getDescribedItem().checkPermission(OwnershipPlugin.MANAGE_ITEMS_OWNERSHIP);

JSONObject jsonOwnership = req.getSubmittedForm().getJSONObject("owners");
OwnershipDescription descr = OwnershipDescription.parseJSON(jsonOwnership);
FolderOwnershipHelper.setOwnership(getDescribedItem(), descr);

return HttpResponses.redirectViaContextPath(getDescribedItem().getUrl());
}
}
@@ -0,0 +1,44 @@
/*
* The MIT License
*
* Copyright (c) 2015 Oleg Nenashev.
*
* 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 org.jenkinsci.plugins.ownership.folders;

import com.cloudbees.hudson.plugins.folder.Folder;
import com.cloudbees.hudson.plugins.folder.TransientFolderActionFactory;
import hudson.Extension;
import hudson.model.Action;
import java.util.Collection;
import java.util.Collections;

/**
* Injects {@link FolderOwnershipAction}s.
* @author Oleg Nenashev
*/
@Extension(optional = true)
public class FolderOwnershipActionFactory extends TransientFolderActionFactory {

@Override
public Collection<? extends Action> createFor(Folder target) {
return Collections.singleton(new FolderOwnershipAction(target));
}
}
@@ -0,0 +1,121 @@
/*
* The MIT License
*
* Copyright (c) 2015 Oleg Nenashev.
*
* 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 org.jenkinsci.plugins.ownership.folders;

import com.cloudbees.hudson.plugins.folder.AbstractFolder;
import com.cloudbees.hudson.plugins.folder.AbstractFolderProperty;
import com.cloudbees.hudson.plugins.folder.Folder;
import com.synopsys.arc.jenkins.plugins.ownership.OwnershipDescription;
import com.synopsys.arc.jenkins.plugins.ownership.OwnershipPlugin;
import com.synopsys.arc.jenkins.plugins.ownership.jobs.JobOwnerHelper;
import com.synopsys.arc.jenkins.plugins.ownership.jobs.JobOwnerJobProperty;
import com.synopsys.arc.jenkins.plugins.ownership.util.AbstractOwnershipHelper;
import com.synopsys.arc.jenkins.plugins.ownership.util.UserCollectionFilter;
import com.synopsys.arc.jenkins.plugins.ownership.util.userFilters.AccessRightsFilter;
import com.synopsys.arc.jenkins.plugins.ownership.util.userFilters.IUserFilter;
import hudson.model.Computer;
import hudson.model.Job;
import hudson.model.User;
import java.io.IOException;
import java.util.Collection;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

/**
* Integration with Folders plugin.
* @author Oleg Nenashev
* @since TODO
*/
public class FolderOwnershipHelper extends AbstractOwnershipHelper<AbstractFolder<?>> {

static final FolderOwnershipHelper INSTANCE = new FolderOwnershipHelper();

@Nonnull
public static FolderOwnershipHelper getInstance() {
return INSTANCE;
}

/**
* Gets OwnerNodeProperty from job if possible.
* @param folder Folder
* @return OwnerNodeProperty or null
*/
@CheckForNull
public static FolderOwnershipProperty getOwnerProperty(@Nonnull AbstractFolder<?> folder) {
FolderOwnershipProperty prop = folder.getProperties().get(FolderOwnershipProperty.class);
return prop != null ? prop : null;
}

@Override
public String getItemTypeName(AbstractFolder<?> item) {
return "folder";
}

@Override
public String getItemDisplayName(AbstractFolder<?> item) {
return item.getDisplayName();
}

@Override
public String getItemURL(AbstractFolder<?> item) {
return item.getUrl();
}

@Override
public OwnershipDescription getOwnershipDescription(AbstractFolder<?> item) {
if (item == null) { // Handle renames, etc.
return OwnershipDescription.DISABLED_DESCR;
}

FolderOwnershipProperty prop = getOwnerProperty(item);
return prop != null ? prop.getOwnership() : OwnershipDescription.DISABLED_DESCR;
}

@Override
public Collection<User> getPossibleOwners(AbstractFolder<?> item) {
if (OwnershipPlugin.getInstance().isRequiresConfigureRights()) {
IUserFilter filter = new AccessRightsFilter(item, Folder.CONFIGURE);
return UserCollectionFilter.filterUsers(User.getAll(), true, filter);
} else {
return User.getAll();
}
}

/**
* Sets the ownership information.
* @param folder Folder to be modified
* @param descr A description to be set. Use null to drop settings.
* @throws IOException
*/
public static void setOwnership(@Nonnull AbstractFolder<?> folder,
@CheckForNull OwnershipDescription descr) throws IOException {
FolderOwnershipProperty prop = getOwnerProperty(folder);
if (prop == null) {
prop = new FolderOwnershipProperty(descr);
folder.addProperty(prop);
} else {
prop.setOwnershipDescription(descr);
}
}
}
@@ -0,0 +1,89 @@
/*
* The MIT License
*
* Copyright (c) 2015 Oleg Nenashev.
*
* 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 org.jenkinsci.plugins.ownership.folders;

import com.cloudbees.hudson.plugins.folder.AbstractFolder;
import com.cloudbees.hudson.plugins.folder.AbstractFolderProperty;
import com.cloudbees.hudson.plugins.folder.AbstractFolderPropertyDescriptor;
import com.cloudbees.hudson.plugins.folder.Folder;
import com.cloudbees.hudson.plugins.folder.FolderProperty;
import com.synopsys.arc.jenkins.plugins.ownership.IOwnershipHelper;
import com.synopsys.arc.jenkins.plugins.ownership.IOwnershipItem;
import com.synopsys.arc.jenkins.plugins.ownership.OwnershipDescription;
import hudson.Extension;
import java.io.IOException;
import javax.annotation.CheckForNull;

/**
* Ownership property for {@link Folder}s.
* @author Oleg Nenashev
* @since TODO
*/
public class FolderOwnershipProperty
extends AbstractFolderProperty<AbstractFolder<?>>
implements IOwnershipItem<AbstractFolder<?>>{

@CheckForNull
OwnershipDescription ownership;

public FolderOwnershipProperty(@CheckForNull OwnershipDescription ownership) {
this.ownership = ownership;
}

@Override
public IOwnershipHelper<AbstractFolder<?>> helper() {
return FolderOwnershipHelper.getInstance();
}

@Override
public AbstractFolder<?> getDescribedItem() {
return owner;
}

@Override
public OwnershipDescription getOwnership() {
return ownership;
}

/**
* Sets the new ownership description.
* @param description Description to be set. Use {@code null} to drop settings.
* @throws IOException Property cannot be saved.
*/
public void setOwnershipDescription(@CheckForNull OwnershipDescription description) throws IOException {
ownership = description;
owner.save();
}

@Extension(optional = true)
public static class DescriptorImpl extends AbstractFolderPropertyDescriptor {

@Override
public String getDisplayName() {
// It prevents the property from displaying
return null;
}

}
}

0 comments on commit 1458f93

Please sign in to comment.