mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-05-13 00:46:29 +02:00
Merged in bugfix/forbid_empty_groups_users_at_global_permission (pull request #167)
disable add admin group/user button when entry is empty
This commit is contained in:
@@ -48,7 +48,7 @@ class AddEntryToTableField extends React.Component<Props, State> {
|
||||
<AddButton
|
||||
label={buttonLabel}
|
||||
action={this.addButtonClicked}
|
||||
disabled={disabled}
|
||||
disabled={disabled || this.state.entryToAdd ===""}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -23,7 +23,9 @@ public class ConfigDto extends HalRepresentation {
|
||||
private boolean disableGroupingGrid;
|
||||
private String dateFormat;
|
||||
private boolean anonymousAccessEnabled;
|
||||
@NoBlankStrings
|
||||
private Set<String> adminGroups;
|
||||
@NoBlankStrings
|
||||
private Set<String> adminUsers;
|
||||
private String baseUrl;
|
||||
private boolean forceBaseUrl;
|
||||
|
||||
@@ -9,6 +9,7 @@ import sonia.scm.util.ScmConfigurationUtil;
|
||||
import sonia.scm.web.VndMediaType;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.validation.Valid;
|
||||
import javax.ws.rs.Consumes;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.PUT;
|
||||
@@ -71,7 +72,7 @@ public class ConfigResource {
|
||||
@ResponseCode(code = 500, condition = "internal server error")
|
||||
})
|
||||
@TypeHint(TypeHint.NO_CONTENT.class)
|
||||
public Response update(ConfigDto configDto) {
|
||||
public Response update(@Valid ConfigDto configDto) {
|
||||
|
||||
// This *could* be moved to ScmConfiguration or ScmConfigurationUtil classes.
|
||||
// But to where to check? load() or store()? Leave it for now, SCMv1 legacy that can be cleaned up later.
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.Payload;
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
|
||||
import static java.lang.annotation.ElementType.FIELD;
|
||||
import static java.lang.annotation.ElementType.METHOD;
|
||||
import static java.lang.annotation.ElementType.PARAMETER;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
@Target({FIELD, METHOD, PARAMETER, ANNOTATION_TYPE})
|
||||
@Retention(RUNTIME)
|
||||
@Constraint(validatedBy = NoBlankStringsValidator.class)
|
||||
@Documented
|
||||
public @interface NoBlankStrings {
|
||||
|
||||
String message() default "collection must not contain empty strings";
|
||||
|
||||
Class<?>[] groups() default {};
|
||||
|
||||
Class<? extends Payload>[] payload() default {};
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
import java.util.Collection;
|
||||
|
||||
public class NoBlankStringsValidator implements ConstraintValidator<NoBlankStrings, Collection> {
|
||||
|
||||
@Override
|
||||
public void initialize(NoBlankStrings constraintAnnotation) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid(Collection object, ConstraintValidatorContext constraintContext) {
|
||||
if ( object == null || object.isEmpty()) {
|
||||
return true;
|
||||
}
|
||||
return object.stream()
|
||||
.map(x -> x.toString())
|
||||
.map(s -> ((String) s).trim())
|
||||
.noneMatch(s -> ((String) s).isEmpty());
|
||||
}
|
||||
}
|
||||
@@ -92,11 +92,7 @@ public class ConfigResourceTest {
|
||||
@Test
|
||||
@SubjectAware(username = "readWrite")
|
||||
public void shouldUpdateConfig() throws URISyntaxException, IOException {
|
||||
URL url = Resources.getResource("sonia/scm/api/v2/config-test-update.json");
|
||||
byte[] configJson = Resources.toByteArray(url);
|
||||
MockHttpRequest request = MockHttpRequest.put("/" + ConfigResource.CONFIG_PATH_V2)
|
||||
.contentType(VndMediaType.CONFIG)
|
||||
.content(configJson);
|
||||
MockHttpRequest request = post("sonia/scm/api/v2/config-test-update.json");
|
||||
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
dispatcher.invoke(request, response);
|
||||
@@ -113,11 +109,7 @@ public class ConfigResourceTest {
|
||||
@Test
|
||||
@SubjectAware(username = "readOnly")
|
||||
public void shouldNotUpdateConfigWhenNotAuthorized() throws URISyntaxException, IOException {
|
||||
URL url = Resources.getResource("sonia/scm/api/v2/config-test-update.json");
|
||||
byte[] configJson = Resources.toByteArray(url);
|
||||
MockHttpRequest request = MockHttpRequest.put("/" + ConfigResource.CONFIG_PATH_V2)
|
||||
.contentType(VndMediaType.CONFIG)
|
||||
.content(configJson);
|
||||
MockHttpRequest request = post("sonia/scm/api/v2/config-test-update.json");
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
|
||||
thrown.expectMessage("Subject does not have permission [configuration:write:global]");
|
||||
@@ -125,6 +117,36 @@ public class ConfigResourceTest {
|
||||
dispatcher.invoke(request, response);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SubjectAware(username = "readWrite")
|
||||
public void shouldFailForEmptyAdminUsers() throws URISyntaxException, IOException {
|
||||
MockHttpRequest request = post("sonia/scm/api/v2/config-test-empty-admin-user.json");
|
||||
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(HttpServletResponse.SC_BAD_REQUEST, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
@SubjectAware(username = "readWrite")
|
||||
public void shouldFailForEmptyAdminGroups() throws URISyntaxException, IOException {
|
||||
MockHttpRequest request = post("sonia/scm/api/v2/config-test-empty-admin-group.json");
|
||||
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
dispatcher.invoke(request, response);
|
||||
|
||||
assertEquals(HttpServletResponse.SC_BAD_REQUEST, response.getStatus());
|
||||
}
|
||||
|
||||
private MockHttpRequest post(String resourceName) throws IOException, URISyntaxException {
|
||||
URL url = Resources.getResource(resourceName);
|
||||
byte[] configJson = Resources.toByteArray(url);
|
||||
return MockHttpRequest.put("/" + ConfigResource.CONFIG_PATH_V2)
|
||||
.contentType(VndMediaType.CONFIG)
|
||||
.content(configJson);
|
||||
}
|
||||
|
||||
private static ScmConfiguration createConfiguration() {
|
||||
ScmConfiguration scmConfiguration = new ScmConfiguration();
|
||||
scmConfiguration.setProxyPassword("heartOfGold");
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package sonia.scm.api.v2.resources;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import static java.util.Collections.emptySet;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class NoBlankStringsValidatorTest {
|
||||
|
||||
@Test
|
||||
void shouldAcceptNonEmptyElements() {
|
||||
assertTrue(new NoBlankStringsValidator().isValid(Arrays.asList("not", "empty"), null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFailForEmptyElements() {
|
||||
assertFalse(new NoBlankStringsValidator().isValid(Arrays.asList("one", "", "three"), null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAcceptEmptyList() {
|
||||
assertTrue(new NoBlankStringsValidator().isValid(emptySet(), null));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"adminGroups": [""]
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"adminUsers": [""]
|
||||
}
|
||||
Reference in New Issue
Block a user