use of org.eclipse.openvsx.entities.NamespaceMembership in project openvsx by eclipse.
the class LocalRegistryService method createNamespace.
@Transactional(rollbackOn = ErrorResultException.class)
public ResultJson createNamespace(NamespaceJson json, String tokenValue) {
var namespaceIssue = validator.validateNamespace(json.name);
if (namespaceIssue.isPresent()) {
throw new ErrorResultException(namespaceIssue.get().toString());
}
var token = users.useAccessToken(tokenValue);
if (token == null) {
throw new ErrorResultException("Invalid access token.");
}
eclipse.checkPublisherAgreement(token.getUser());
var namespace = repositories.findNamespace(json.name);
if (namespace != null) {
throw new ErrorResultException("Namespace already exists: " + namespace.getName());
}
// Create the requested namespace
namespace = new Namespace();
namespace.setName(json.name);
entityManager.persist(namespace);
// Assign the requesting user as contributor
var membership = new NamespaceMembership();
membership.setNamespace(namespace);
membership.setUser(token.getUser());
membership.setRole(NamespaceMembership.ROLE_CONTRIBUTOR);
entityManager.persist(membership);
return ResultJson.success("Created namespace " + namespace.getName());
}
use of org.eclipse.openvsx.entities.NamespaceMembership in project openvsx by eclipse.
the class UserService method addNamespaceMember.
@Transactional(rollbackOn = ErrorResultException.class)
public ResultJson addNamespaceMember(Namespace namespace, UserData user, String role) {
if (!(role.equals(NamespaceMembership.ROLE_OWNER) || role.equals(NamespaceMembership.ROLE_CONTRIBUTOR))) {
throw new ErrorResultException("Invalid role: " + role);
}
var membership = repositories.findMembership(user, namespace);
if (membership != null) {
if (role.equals(membership.getRole())) {
throw new ErrorResultException("User " + user.getLoginName() + " already has the role " + role + ".");
}
membership.setRole(role);
return ResultJson.success("Changed role of " + user.getLoginName() + " in " + namespace.getName() + " to " + role + ".");
}
membership = new NamespaceMembership();
membership.setNamespace(namespace);
membership.setUser(user);
membership.setRole(role);
entityManager.persist(membership);
return ResultJson.success("Added " + user.getLoginName() + " as " + role + " of " + namespace.getName() + ".");
}
use of org.eclipse.openvsx.entities.NamespaceMembership in project openvsx by eclipse.
the class RegistryAPITest method mockForPublish.
private void mockForPublish(String mode) {
var token = mockAccessToken();
if (mode.equals("invalid")) {
token.setActive(false);
}
var namespace = new Namespace();
namespace.setName("foo");
Mockito.when(repositories.findNamespace("foo")).thenReturn(namespace);
if (mode.equals("existing")) {
var extension = new Extension();
extension.setName("bar");
var extVersion = new ExtensionVersion();
extVersion.setExtension(extension);
extVersion.setVersion("1");
extVersion.setActive(true);
Mockito.when(repositories.findExtension("bar", namespace)).thenReturn(extension);
Mockito.when(repositories.findVersion("1", extension)).thenReturn(extVersion);
}
Mockito.when(repositories.countActiveReviews(any(Extension.class))).thenReturn(0l);
Mockito.when(repositories.findVersions(any(Extension.class))).thenReturn(Streamable.empty());
Mockito.when(repositories.findActiveVersions(any(Extension.class))).thenReturn(Streamable.empty());
Mockito.when(repositories.getVersionStrings(any(Extension.class))).thenReturn(Streamable.empty());
Mockito.when(repositories.getActiveVersionStrings(any(Extension.class))).thenReturn(Streamable.empty());
Mockito.when(repositories.findFilesByType(any(ExtensionVersion.class), anyCollection())).thenReturn(Streamable.empty());
if (mode.equals("owner")) {
var ownerMem = new NamespaceMembership();
ownerMem.setUser(token.getUser());
ownerMem.setNamespace(namespace);
ownerMem.setRole(NamespaceMembership.ROLE_OWNER);
Mockito.when(repositories.findMemberships(namespace, NamespaceMembership.ROLE_OWNER)).thenReturn(Streamable.of(ownerMem));
Mockito.when(repositories.countMemberships(namespace, NamespaceMembership.ROLE_OWNER)).thenReturn(1l);
Mockito.when(repositories.findMembership(token.getUser(), namespace)).thenReturn(ownerMem);
Mockito.when(repositories.countMemberships(token.getUser(), namespace)).thenReturn(1l);
} else if (mode.equals("contributor") || mode.equals("sole-contributor") || mode.equals("existing")) {
var contribMem = new NamespaceMembership();
contribMem.setUser(token.getUser());
contribMem.setNamespace(namespace);
contribMem.setRole(NamespaceMembership.ROLE_CONTRIBUTOR);
Mockito.when(repositories.findMembership(token.getUser(), namespace)).thenReturn(contribMem);
Mockito.when(repositories.countMemberships(token.getUser(), namespace)).thenReturn(1l);
if (mode.equals("contributor")) {
var otherUser = new UserData();
otherUser.setLoginName("other_user");
var ownerMem = new NamespaceMembership();
ownerMem.setUser(otherUser);
ownerMem.setNamespace(namespace);
ownerMem.setRole(NamespaceMembership.ROLE_OWNER);
Mockito.when(repositories.findMemberships(namespace, NamespaceMembership.ROLE_OWNER)).thenReturn(Streamable.of(ownerMem));
Mockito.when(repositories.countMemberships(namespace, NamespaceMembership.ROLE_OWNER)).thenReturn(1l);
} else {
Mockito.when(repositories.findMemberships(namespace, NamespaceMembership.ROLE_OWNER)).thenReturn(Streamable.empty());
Mockito.when(repositories.countMemberships(namespace, NamespaceMembership.ROLE_OWNER)).thenReturn(0l);
}
} else if (mode.equals("privileged") || mode.equals("unrelated")) {
var otherUser = new UserData();
otherUser.setLoginName("other_user");
var ownerMem = new NamespaceMembership();
ownerMem.setUser(otherUser);
ownerMem.setNamespace(namespace);
ownerMem.setRole(NamespaceMembership.ROLE_OWNER);
Mockito.when(repositories.findMemberships(namespace, NamespaceMembership.ROLE_OWNER)).thenReturn(Streamable.of(ownerMem));
Mockito.when(repositories.countMemberships(namespace, NamespaceMembership.ROLE_OWNER)).thenReturn(1l);
if (mode.equals("privileged")) {
token.getUser().setRole(UserData.ROLE_PRIVILEGED);
}
} else {
Mockito.when(repositories.findMemberships(namespace, NamespaceMembership.ROLE_OWNER)).thenReturn(Streamable.empty());
Mockito.when(repositories.countMemberships(namespace, NamespaceMembership.ROLE_OWNER)).thenReturn(0l);
}
}
Aggregations