forked from synopsys-arc-oss/ownership-plugin
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-28881] - Implementation of Ownership property injection
- Loading branch information
1 parent
9d49867
commit 1458f93
Showing
12 changed files
with
681 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/main/java/org/jenkinsci/plugins/ownership/folders/FolderOwnershipAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/org/jenkinsci/plugins/ownership/folders/FolderOwnershipActionFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
121 changes: 121 additions & 0 deletions
121
src/main/java/org/jenkinsci/plugins/ownership/folders/FolderOwnershipHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
src/main/java/org/jenkinsci/plugins/ownership/folders/FolderOwnershipProperty.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.