Skip to content
Permalink
Browse files
[JENKINS-25726] - Fixed the previous implementation, added support of…
… anonymous users

Signed-off-by: Oleg Nenashev <o.v.nenashev@gmail.com>
  • Loading branch information
oleg-nenashev committed Nov 23, 2014
1 parent 36e0003 commit 796315d76ea3df8a0860150867bffece72a00b80
Showing 4 changed files with 36 additions and 18 deletions.
@@ -35,6 +35,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.kohsuke.stapler.DataBoundConstructor;

@@ -46,29 +47,36 @@
public class StartedByUserRestriction extends JobRestriction {

private final List<UserSelector> usersList;
private final boolean checkUsersFromUpstremProjects;
private final boolean acceptAutomaticRuns;
private final boolean checkUsersFromUpstreamProjects;
private final @Deprecated boolean acceptAutomaticRuns;
private final boolean acceptAnonymousUsers;
private transient Set<String> acceptedUsers = null;

@DataBoundConstructor
public StartedByUserRestriction(List<UserSelector> usersList, boolean checkUsersFromUpstremProjects, boolean acceptAutomaticRuns) {
public StartedByUserRestriction(List<UserSelector> usersList, boolean checkUsersFromUpstreamProjects,
boolean acceptAutomaticRuns, boolean acceptAnonymousUsers) {
this.usersList = usersList;
this.checkUsersFromUpstremProjects = checkUsersFromUpstremProjects;
this.checkUsersFromUpstreamProjects = checkUsersFromUpstreamProjects;
this.acceptAutomaticRuns = acceptAutomaticRuns;
this.acceptAnonymousUsers = acceptAnonymousUsers;
}

public List<UserSelector> getUsersList() {
return usersList;
}

public boolean isAcceptAutomaticRuns() {
public @Deprecated boolean isAcceptAutomaticRuns() {
return acceptAutomaticRuns;
}

public boolean isCheckUsersFromUpstremProjects() {
return checkUsersFromUpstremProjects;
public boolean isCheckUsersFromUpstreamProjects() {
return checkUsersFromUpstreamProjects;
}


public boolean isAcceptAnonymousUsers() {
return acceptAnonymousUsers;
}

private synchronized @Nonnull Set<String> getAcceptedUsers() {
if (acceptedUsers == null) {
final List<UserSelector> selectors = getUsersList();
@@ -80,19 +88,21 @@ public boolean isCheckUsersFromUpstremProjects() {
return acceptedUsers;
}

private boolean acceptsUser(String userId) {
return getAcceptedUsers().contains(userId);
private boolean acceptsUser(@CheckForNull String userId) {
return userId == null
? acceptAnonymousUsers
: getAcceptedUsers().contains(userId);
}

/**package*/ boolean canTake(@Nonnull List<Cause> causes) {
for (Cause cause : causes) {
if (cause instanceof Cause.UserIdCause) {
String startedBy = ((Cause.UserIdCause)cause).getUserId();
final @CheckForNull String startedBy = ((Cause.UserIdCause)cause).getUserId();
if (acceptsUser(startedBy)) {
return true;
}
}
if (checkUsersFromUpstremProjects && cause instanceof Cause.UpstreamCause) {
if (checkUsersFromUpstreamProjects && cause instanceof Cause.UpstreamCause) {
final List<Cause> upstreamCauses = ((Cause.UpstreamCause)cause).getUpstreamCauses();
if (canTake(upstreamCauses)) { // Recursive call to iterate through all causes
return true;
@@ -118,7 +128,7 @@ public static class DescriptorImpl extends JobRestrictionDescriptor {

@Override
public String getDisplayName() {
return Messages.restrictions_Job_RegexName();
return Messages.restrictions_Job_StartedByUserRestriction_displayName();
}
}
}
@@ -11,5 +11,6 @@ restrictions.Logic.Not=Not
restrictions.Job.RegexName=Regular Expression (Job Name)
restrictions.Job.RegexName.OkMessage=Pattern is valid
restrictions.Job.RegexName.FailMessage=Pattern is invalid and will be ignored.
restrictions.Job.StartedByUserRestriction.displayName=Started By User
restirctions.Stuff.MultipleSuffix=(multiple entries)

@@ -26,13 +26,17 @@
xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form"
xmlns:i="jelly:fmt" xmlns:p="/lib/hudson/project">

<f:entry title="${%Users list}">
<f:entry title="${%usersList}">
<f:repeatableProperty field="usersList" add="${%Add user}"/>
</f:entry>
<f:entry field="checkUsersFromUpstremProjects">
<f:checkbox title="${%Accept upstream runs started by users}" checked="${it.checkUsersFromUpstremProjects}"/>
<f:entry field="checkUsersFromUpstreamProjects">
<f:checkbox title="${%checkUsersFromUpstreamProjects}" checked="${it.checkUsersFromUpstreamProjects}"/>
</f:entry>
<f:entry field="acceptAutomaticRuns">
<f:checkbox title="${%Accept runs started by other causes}" checked="${it.acceptAutomaticRuns}"/>
<f:entry field="acceptAnonymousUsers">
<f:checkbox title="${%acceptAnonymousUsers}" checked="${it.acceptAnonymousUsers}"/>
</f:entry>

<!-- <f:entry field="acceptAutomaticRuns">
<f:checkbox title="${%Accept runs started by other causes}" checked="${it.acceptAutomaticRuns}"/>
</f:entry> -->
</j:jelly>
@@ -0,0 +1,3 @@
usersList=Users list
checkUsersFromUpstreamProjects=Accept upstream runs started by users above
acceptAnonymousUsers=Accept runs started by anonymous users

0 comments on commit 796315d

Please sign in to comment.