mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-08 14:35:45 +01:00
adds namespace strategies for current year, repository type, username and custom
This commit is contained in:
@@ -185,7 +185,7 @@ public class ScmConfiguration implements Configuration {
|
|||||||
private boolean enabledXsrfProtection = true;
|
private boolean enabledXsrfProtection = true;
|
||||||
|
|
||||||
@XmlElement(name = "default-namespace-strategy")
|
@XmlElement(name = "default-namespace-strategy")
|
||||||
private String defaultNamespaceStrategy = "sonia.scm.repository.DefaultNamespaceStrategy";
|
private String defaultNamespaceStrategy = "sonia.scm.repository.UsernameNamespaceStrategy";
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package sonia.scm.repository;
|
||||||
|
|
||||||
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
|
import sonia.scm.plugin.Extension;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import java.time.Clock;
|
||||||
|
import java.time.Year;
|
||||||
|
|
||||||
|
@Extension
|
||||||
|
public class CurrentYearNamespaceStrategy implements NamespaceStrategy {
|
||||||
|
|
||||||
|
private final Clock clock;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public CurrentYearNamespaceStrategy() {
|
||||||
|
this(Clock.systemDefaultZone());
|
||||||
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
CurrentYearNamespaceStrategy(Clock clock) {
|
||||||
|
this.clock = clock;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String createNamespace(Repository repository) {
|
||||||
|
return String.valueOf(Year.now(clock).getValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package sonia.scm.repository;
|
||||||
|
|
||||||
|
import sonia.scm.plugin.Extension;
|
||||||
|
import sonia.scm.util.ValidationUtil;
|
||||||
|
|
||||||
|
import static sonia.scm.ScmConstraintViolationException.Builder.doThrow;
|
||||||
|
|
||||||
|
@Extension
|
||||||
|
public class CustomNamespaceStrategy implements NamespaceStrategy {
|
||||||
|
@Override
|
||||||
|
public String createNamespace(Repository repository) {
|
||||||
|
String namespace = repository.getNamespace();
|
||||||
|
|
||||||
|
doThrow()
|
||||||
|
.violation("invalid namespace", "namespace")
|
||||||
|
.when(!ValidationUtil.isRepositoryNameValid(namespace));
|
||||||
|
|
||||||
|
return namespace;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
package sonia.scm.repository;
|
|
||||||
|
|
||||||
import com.google.common.base.Strings;
|
|
||||||
import org.apache.shiro.SecurityUtils;
|
|
||||||
import sonia.scm.plugin.Extension;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The DefaultNamespaceStrategy returns the predefined namespace of the given repository, if the namespace was not set
|
|
||||||
* the username of the currently loggedin user is used.
|
|
||||||
*
|
|
||||||
* @since 2.0.0
|
|
||||||
*/
|
|
||||||
@Extension
|
|
||||||
public class DefaultNamespaceStrategy implements NamespaceStrategy {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String createNamespace(Repository repository) {
|
|
||||||
String namespace = repository.getNamespace();
|
|
||||||
if (Strings.isNullOrEmpty(namespace)) {
|
|
||||||
namespace = SecurityUtils.getSubject().getPrincipal().toString();
|
|
||||||
}
|
|
||||||
return namespace;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package sonia.scm.repository;
|
||||||
|
|
||||||
|
import sonia.scm.plugin.Extension;
|
||||||
|
|
||||||
|
@Extension
|
||||||
|
public class RepositoryTypeNamespaceStrategy implements NamespaceStrategy {
|
||||||
|
@Override
|
||||||
|
public String createNamespace(Repository repository) {
|
||||||
|
return repository.getType();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package sonia.scm.repository;
|
||||||
|
|
||||||
|
import org.apache.shiro.SecurityUtils;
|
||||||
|
import sonia.scm.plugin.Extension;
|
||||||
|
|
||||||
|
@Extension
|
||||||
|
public class UsernameNamespaceStrategy implements NamespaceStrategy {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String createNamespace(Repository repository) {
|
||||||
|
return SecurityUtils.getSubject().getPrincipal().toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package sonia.scm.repository;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
|
||||||
|
import java.time.Clock;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.ZoneId;
|
||||||
|
import java.time.ZoneOffset;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
class CurrentYearNamespaceStrategyTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private Clock clock;
|
||||||
|
private NamespaceStrategy namespaceStrategy;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setupObjectUnderTest() {
|
||||||
|
namespaceStrategy = new CurrentYearNamespaceStrategy(clock);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldReturn1985() {
|
||||||
|
LocalDateTime dateTime = LocalDateTime.of(1985, 4, 9, 21, 42);
|
||||||
|
when(clock.instant()).thenReturn(dateTime.toInstant(ZoneOffset.UTC));
|
||||||
|
when(clock.getZone()).thenReturn(ZoneId.systemDefault());
|
||||||
|
|
||||||
|
String namespace = namespaceStrategy.createNamespace(RepositoryTestData.createHeartOfGold());
|
||||||
|
assertThat(namespace).isEqualTo("1985");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package sonia.scm.repository;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import sonia.scm.ScmConstraintViolationException;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
|
class CustomNamespaceStrategyTest {
|
||||||
|
|
||||||
|
private final NamespaceStrategy namespaceStrategy = new CustomNamespaceStrategy();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldReturnNamespaceFromRepository() {
|
||||||
|
Repository heartOfGold = RepositoryTestData.createHeartOfGold();
|
||||||
|
assertThat(namespaceStrategy.createNamespace(heartOfGold)).isEqualTo(RepositoryTestData.NAMESPACE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldThrowAnValidationExceptionForAnInvalidNamespace() {
|
||||||
|
Repository repository = new Repository();
|
||||||
|
repository.setNamespace("..");
|
||||||
|
repository.setName(".");
|
||||||
|
|
||||||
|
assertThrows(ScmConstraintViolationException.class, () -> namespaceStrategy.createNamespace(repository));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
package sonia.scm.repository;
|
|
||||||
|
|
||||||
import com.github.sdorra.shiro.ShiroRule;
|
|
||||||
import com.github.sdorra.shiro.SubjectAware;
|
|
||||||
import org.junit.Rule;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
|
||||||
|
|
||||||
@SubjectAware(configuration = "classpath:sonia/scm/shiro-001.ini")
|
|
||||||
public class DefaultNamespaceStrategyTest {
|
|
||||||
|
|
||||||
@Rule
|
|
||||||
public ShiroRule shiroRule = new ShiroRule();
|
|
||||||
|
|
||||||
private DefaultNamespaceStrategy namespaceStrategy = new DefaultNamespaceStrategy();
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@SubjectAware(username = "trillian", password = "secret")
|
|
||||||
public void testNamespaceStrategyWithoutPreset() {
|
|
||||||
assertEquals("trillian", namespaceStrategy.createNamespace(new Repository()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@SubjectAware(username = "trillian", password = "secret")
|
|
||||||
public void testNamespaceStrategyWithPreset() {
|
|
||||||
Repository repository = new Repository();
|
|
||||||
repository.setNamespace("awesome");
|
|
||||||
assertEquals("awesome", namespaceStrategy.createNamespace(repository));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package sonia.scm.repository;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
class RepositoryTypeNamespaceStrategyTest {
|
||||||
|
|
||||||
|
private final RepositoryTypeNamespaceStrategy namespaceStrategy = new RepositoryTypeNamespaceStrategy();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldReturnTypeOfRepository() {
|
||||||
|
Repository git = RepositoryTestData.create42Puzzle("git");
|
||||||
|
assertThat(namespaceStrategy.createNamespace(git)).isEqualTo("git");
|
||||||
|
|
||||||
|
Repository hg = RepositoryTestData.create42Puzzle("hg");
|
||||||
|
assertThat(namespaceStrategy.createNamespace(hg)).isEqualTo("hg");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package sonia.scm.repository;
|
||||||
|
|
||||||
|
|
||||||
|
import org.apache.shiro.subject.Subject;
|
||||||
|
import org.apache.shiro.util.ThreadContext;
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
class UsernameNamespaceStrategyTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private Subject subject;
|
||||||
|
|
||||||
|
private final NamespaceStrategy usernameNamespaceStrategy = new UsernameNamespaceStrategy();
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setupSubject() {
|
||||||
|
ThreadContext.bind(subject);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void clearThreadContext() {
|
||||||
|
ThreadContext.unbindSubject();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldReturnPrimaryPrincipal() {
|
||||||
|
when(subject.getPrincipal()).thenReturn("trillian");
|
||||||
|
|
||||||
|
String namespace = usernameNamespaceStrategy.createNamespace(RepositoryTestData.createHeartOfGold());
|
||||||
|
assertThat(namespace).isEqualTo("trillian");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user