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:
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user