Skip to content

Commit

Permalink
Merge pull request #3460 from oleg-nenashev/feature/JENKINS-51598-Per…
Browse files Browse the repository at this point in the history
…issionAPI

[JENKINS-51598] - Expose PermissionGroup IDs to API
  • Loading branch information
oleg-nenashev committed Jun 8, 2018
2 parents a846c36 + 7de5f7d commit 8d438b7
Showing 1 changed file with 29 additions and 6 deletions.
35 changes: 29 additions & 6 deletions core/src/main/java/hudson/security/PermissionGroup.java
Expand Up @@ -27,6 +27,7 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.SortedSet;
import java.util.TreeSet;
import javax.annotation.CheckForNull;
Expand All @@ -51,19 +52,41 @@ public final class PermissionGroup implements Iterable<Permission>, Comparable<P
*/
public final Localizable title;

private final String id;

/**
* Both creates a registers a new permission group.
* @param owner sets {@link #owner}
* @param title sets {@link #title}
* @throws IllegalStateException if this group was already registered
*/
public PermissionGroup(@Nonnull Class owner, Localizable title) throws IllegalStateException {
this(title.toString(Locale.ENGLISH), owner, title);
}

/**
* Both creates a registers a new permission group.
* @param owner sets {@link #owner}
* @param title sets {@link #title}
* @throws IllegalStateException if this group was already registered
* @since TODO
*/
public PermissionGroup(String id, @Nonnull Class owner, Localizable title) throws IllegalStateException {
this.owner = owner;
this.title = title;
this.id = id;
register(this);
}

private String id() {
/**
* Gets ID of the permission group.
* @return Non-localizable ID of the permission group.
*/
public String getId() {
return id;
}

public String getOwnerClassName() {
return owner.getName();
}

Expand Down Expand Up @@ -110,7 +133,7 @@ public int compareTo(PermissionGroup that) {

// among the permissions of the same group, just sort by their names
// so that the sort order is consistent regardless of classloading order.
return id().compareTo(that.id());
return getOwnerClassName().compareTo(that.getOwnerClassName());
}

private int compareOrder() {
Expand All @@ -119,24 +142,24 @@ private int compareOrder() {
}

@Override public boolean equals(Object o) {
return o instanceof PermissionGroup && id().equals(((PermissionGroup) o).id());
return o instanceof PermissionGroup && getOwnerClassName().equals(((PermissionGroup) o).getOwnerClassName());
}

@Override public int hashCode() {
return id().hashCode();
return getOwnerClassName().hashCode();
}

public synchronized int size() {
return permissions.size();
}

@Override public String toString() {
return "PermissionGroup[" + id() + "]";
return "PermissionGroup[" + getOwnerClassName() + "]";
}

private static synchronized void register(PermissionGroup g) {
if (!PERMISSIONS.add(g)) {
throw new IllegalStateException("attempt to register a second PermissionGroup for " + g.id());
throw new IllegalStateException("attempt to register a second PermissionGroup for " + g.getOwnerClassName());
}
}

Expand Down

0 comments on commit 8d438b7

Please sign in to comment.