-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Nicer JCasC syntax for defining permissions (#145)
* Nicer JCasC syntax for defining permissions * Oops, wrong Converter! Also, clean up test and note PR dependency * Sort entries in the JCasC YAML, add proper assertions * Implement nested type to prevent weird sort order * Adapt link in readme to future preferred syntax * Improve error message * Add test for v3 format --------- Co-authored-by: Daniel Beck <[email protected]>
- Loading branch information
1 parent
5f2fae6
commit 47a3280
Showing
17 changed files
with
588 additions
and
151 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
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
110 changes: 110 additions & 0 deletions
110
src/main/java/org/jenkinsci/plugins/matrixauth/integrations/casc/DefinitionEntry.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,110 @@ | ||
package org.jenkinsci.plugins.matrixauth.integrations.casc; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import org.jenkinsci.plugins.matrixauth.AuthorizationType; | ||
import org.jenkinsci.plugins.matrixauth.PermissionEntry; | ||
import org.kohsuke.accmod.Restricted; | ||
import org.kohsuke.accmod.restrictions.NoExternalUse; | ||
import org.kohsuke.stapler.DataBoundConstructor; | ||
import org.kohsuke.stapler.DataBoundSetter; | ||
|
||
/** | ||
* Entry for type-safe permission definitions in JCasC YAML. | ||
*/ | ||
@Restricted(NoExternalUse.class) | ||
public class DefinitionEntry { | ||
private AuthorizationType type; | ||
private Child child; | ||
|
||
@DataBoundConstructor | ||
public DefinitionEntry() {} | ||
|
||
public DefinitionEntry(AuthorizationType type, Child child) { | ||
this.child = child; | ||
this.type = type; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setUserOrGroup(Child child) { | ||
setNew(AuthorizationType.EITHER, child); | ||
} | ||
|
||
public Child getUserOrGroup() { | ||
return type == AuthorizationType.EITHER ? child : null; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setUser(Child child) { | ||
setNew(AuthorizationType.USER, child); | ||
} | ||
|
||
public Child getUser() { | ||
return type == AuthorizationType.USER ? child : null; | ||
} | ||
|
||
@DataBoundSetter | ||
public void setGroup(Child child) { | ||
setNew(AuthorizationType.GROUP, child); | ||
} | ||
|
||
private void setNew(AuthorizationType type, Child child) { | ||
if (this.type != null) { | ||
throw new IllegalStateException( | ||
"Can only configure one of: 'user', 'group', 'userOrGroup', but attempted to redefine to '" | ||
+ authorizationTypeToKey(type) + "' with name '" + child.name + "' after '" | ||
+ authorizationTypeToKey(this.type) + "' was already set to '" | ||
+ this.child.name + "'"); | ||
} | ||
this.type = type; | ||
this.child = child; | ||
} | ||
|
||
public Child getGroup() { | ||
return type == AuthorizationType.GROUP ? child : null; | ||
} | ||
|
||
public Child child() { | ||
return child; | ||
} | ||
|
||
public PermissionEntry permissionEntry() { | ||
return new PermissionEntry(type, child.name); | ||
} | ||
|
||
private static String authorizationTypeToKey(AuthorizationType type) { | ||
if (type == null) { | ||
throw new NullPointerException("Received null 'type'"); | ||
} | ||
if (type == AuthorizationType.USER) { | ||
return "user"; | ||
} | ||
if (type == AuthorizationType.GROUP) { | ||
return "group"; | ||
} | ||
if (type == AuthorizationType.EITHER) { | ||
return "userOrGroup"; | ||
} | ||
throw new IllegalStateException("Unexpected 'type': " + type); | ||
} | ||
|
||
@Restricted(NoExternalUse.class) | ||
public static class Child { | ||
final List<PermissionDefinition> permissions; | ||
final String name; | ||
|
||
@DataBoundConstructor | ||
public Child(String name, List<PermissionDefinition> permissions) { | ||
this.name = name; | ||
this.permissions = permissions; | ||
} | ||
|
||
public List<PermissionDefinition> getPermissions() { | ||
return permissions.stream().sorted().collect(Collectors.toList()); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.