Add endpoint for repository roles (tests pending)

This commit is contained in:
René Pfeuffer
2019-05-03 17:44:27 +02:00
parent 7a2166a365
commit 76b9100e7e
17 changed files with 849 additions and 3 deletions

View File

@@ -28,6 +28,10 @@ public class MapperModule extends AbstractModule {
bind(RepositoryPermissionDtoToRepositoryPermissionMapper.class).to(Mappers.getMapper(RepositoryPermissionDtoToRepositoryPermissionMapper.class).getClass());
bind(RepositoryPermissionToRepositoryPermissionDtoMapper.class).to(Mappers.getMapper(RepositoryPermissionToRepositoryPermissionDtoMapper.class).getClass());
bind(RepositoryRoleToRepositoryRoleDtoMapper.class).to(Mappers.getMapper(RepositoryRoleToRepositoryRoleDtoMapper.class).getClass());
bind(RepositoryRoleDtoToRepositoryRoleMapper.class).to(Mappers.getMapper(RepositoryRoleDtoToRepositoryRoleMapper.class).getClass());
bind(RepositoryRoleCollectionToDtoMapper.class);
bind(ChangesetToChangesetDtoMapper.class).to(Mappers.getMapper(DefaultChangesetToChangesetDtoMapper.class).getClass());
bind(ChangesetToParentDtoMapper.class).to(Mappers.getMapper(ChangesetToParentDtoMapper.class).getClass());

View File

@@ -0,0 +1,95 @@
package sonia.scm.api.v2.resources;
import com.webcohesion.enunciate.metadata.rs.ResponseCode;
import com.webcohesion.enunciate.metadata.rs.ResponseHeader;
import com.webcohesion.enunciate.metadata.rs.ResponseHeaders;
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
import com.webcohesion.enunciate.metadata.rs.TypeHint;
import org.apache.shiro.authc.credential.PasswordService;
import sonia.scm.repository.RepositoryRole;
import sonia.scm.repository.RepositoryRoleManager;
import sonia.scm.web.VndMediaType;
import javax.inject.Inject;
import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
public class RepositoryRoleCollectionResource {
private static final int DEFAULT_PAGE_SIZE = 10;
private final RepositoryRoleDtoToRepositoryRoleMapper dtoToRepositoryRoleMapper;
private final RepositoryRoleCollectionToDtoMapper repositoryRoleCollectionToDtoMapper;
private final ResourceLinks resourceLinks;
private final IdResourceManagerAdapter<RepositoryRole, RepositoryRoleDto> adapter;
@Inject
public RepositoryRoleCollectionResource(RepositoryRoleManager manager, RepositoryRoleDtoToRepositoryRoleMapper dtoToRepositoryRoleMapper,
RepositoryRoleCollectionToDtoMapper repositoryRoleCollectionToDtoMapper, ResourceLinks resourceLinks) {
this.dtoToRepositoryRoleMapper = dtoToRepositoryRoleMapper;
this.repositoryRoleCollectionToDtoMapper = repositoryRoleCollectionToDtoMapper;
this.adapter = new IdResourceManagerAdapter<>(manager, RepositoryRole.class);
this.resourceLinks = resourceLinks;
}
/**
* Returns all repository roles for a given page number with a given page size (default page size is {@value DEFAULT_PAGE_SIZE}).
*
* <strong>Note:</strong> This method requires "repositoryRole" privilege.
*
* @param page the number of the requested page
* @param pageSize the page size (default page size is {@value DEFAULT_PAGE_SIZE})
* @param sortBy sort parameter (if empty - undefined sorting)
* @param desc sort direction desc or asc
*/
@GET
@Path("")
@Produces(VndMediaType.REPOSITORY_ROLE_COLLECTION)
@TypeHint(CollectionDto.class)
@StatusCodes({
@ResponseCode(code = 200, condition = "success"),
@ResponseCode(code = 400, condition = "\"sortBy\" field unknown"),
@ResponseCode(code = 401, condition = "not authenticated / invalid credentials"),
@ResponseCode(code = 403, condition = "not authorized, the current repositoryRole does not have the \"repositoryRole\" privilege"),
@ResponseCode(code = 500, condition = "internal server error")
})
public Response getAll(@DefaultValue("0") @QueryParam("page") int page,
@DefaultValue("" + DEFAULT_PAGE_SIZE) @QueryParam("pageSize") int pageSize,
@QueryParam("sortBy") String sortBy,
@DefaultValue("false") @QueryParam("desc") boolean desc
) {
return adapter.getAll(page, pageSize, x -> true, sortBy, desc,
pageResult -> repositoryRoleCollectionToDtoMapper.map(page, pageSize, pageResult));
}
/**
* Creates a new repository role.
*
* <strong>Note:</strong> This method requires "repositoryRole" privilege.
*
* @param repositoryRole The repositoryRole to be created.
* @return A response with the link to the new repository role (if created successfully).
*/
@POST
@Path("")
@Consumes(VndMediaType.REPOSITORY_ROLE)
@StatusCodes({
@ResponseCode(code = 201, condition = "create success"),
@ResponseCode(code = 401, condition = "not authenticated / invalid credentials"),
@ResponseCode(code = 403, condition = "not authorized, the current user does not have the \"repositoryRole\" privilege"),
@ResponseCode(code = 409, condition = "conflict, a repository role with this name already exists"),
@ResponseCode(code = 500, condition = "internal server error")
})
@TypeHint(TypeHint.NO_CONTENT.class)
@ResponseHeaders(@ResponseHeader(name = "Location", description = "uri to the created repositoryRole"))
public Response create(@Valid RepositoryRoleDto repositoryRole) {
return adapter.create(repositoryRole, () -> dtoToRepositoryRoleMapper.map(repositoryRole), u -> resourceLinks.repositoryRole().self(u.getName()));
}
}

View File

@@ -0,0 +1,34 @@
package sonia.scm.api.v2.resources;
import sonia.scm.PageResult;
import sonia.scm.repository.RepositoryRole;
import sonia.scm.repository.RepositoryRolePermissions;
import javax.inject.Inject;
import java.util.Optional;
import static java.util.Optional.empty;
import static java.util.Optional.of;
public class RepositoryRoleCollectionToDtoMapper extends BasicCollectionToDtoMapper<RepositoryRole, RepositoryRoleDto, RepositoryRoleToRepositoryRoleDtoMapper> {
private final ResourceLinks resourceLinks;
@Inject
public RepositoryRoleCollectionToDtoMapper(RepositoryRoleToRepositoryRoleDtoMapper repositoryRoleToDtoMapper, ResourceLinks resourceLinks) {
super("repositoryRoles", repositoryRoleToDtoMapper);
this.resourceLinks = resourceLinks;
}
public CollectionDto map(int pageNumber, int pageSize, PageResult<RepositoryRole> pageResult) {
return map(pageNumber, pageSize, pageResult, this.createSelfLink(), this.createCreateLink());
}
Optional<String> createCreateLink() {
return RepositoryRolePermissions.modify().isPermitted() ? of(resourceLinks.repositoryRoleCollection().create()): empty();
}
String createSelfLink() {
return resourceLinks.repositoryRoleCollection().self();
}
}

View File

@@ -0,0 +1,22 @@
package sonia.scm.api.v2.resources;
import de.otto.edison.hal.Embedded;
import de.otto.edison.hal.HalRepresentation;
import de.otto.edison.hal.Links;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import java.util.Collection;
@Getter
@Setter
@NoArgsConstructor
public class RepositoryRoleDto extends HalRepresentation {
private String name;
private Collection<String> verbs;
RepositoryRoleDto(Links links, Embedded embedded) {
super(links, embedded);
}
}

View File

@@ -0,0 +1,14 @@
package sonia.scm.api.v2.resources;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import sonia.scm.repository.RepositoryRole;
// Mapstruct does not support parameterized (i.e. non-default) constructors. Thus, we need to use field injection.
@SuppressWarnings("squid:S3306")
@Mapper
public abstract class RepositoryRoleDtoToRepositoryRoleMapper extends BaseDtoMapper {
@Mapping(target = "creationDate", ignore = true)
public abstract RepositoryRole map(RepositoryRoleDto repositoryRoleDto);
}

View File

@@ -0,0 +1,103 @@
package sonia.scm.api.v2.resources;
import com.webcohesion.enunciate.metadata.rs.ResponseCode;
import com.webcohesion.enunciate.metadata.rs.StatusCodes;
import com.webcohesion.enunciate.metadata.rs.TypeHint;
import sonia.scm.repository.RepositoryRole;
import sonia.scm.repository.RepositoryRoleManager;
import sonia.scm.web.VndMediaType;
import javax.inject.Inject;
import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
public class RepositoryRoleResource {
private final RepositoryRoleDtoToRepositoryRoleMapper dtoToRepositoryRoleMapper;
private final RepositoryRoleToRepositoryRoleDtoMapper repositoryRoleToDtoMapper;
private final IdResourceManagerAdapter<RepositoryRole, RepositoryRoleDto> adapter;
@Inject
public RepositoryRoleResource(
RepositoryRoleDtoToRepositoryRoleMapper dtoToRepositoryRoleMapper,
RepositoryRoleToRepositoryRoleDtoMapper repositoryRoleToDtoMapper,
RepositoryRoleManager manager) {
this.dtoToRepositoryRoleMapper = dtoToRepositoryRoleMapper;
this.repositoryRoleToDtoMapper = repositoryRoleToDtoMapper;
this.adapter = new IdResourceManagerAdapter<>(manager, RepositoryRole.class);
}
/**
* Returns a repository role.
*
* <strong>Note:</strong> This method requires "repositoryRole" privilege.
*
* @param name the id/name of the repository role
*/
@GET
@Path("")
@Produces(VndMediaType.REPOSITORY_ROLE)
@TypeHint(RepositoryRoleDto.class)
@StatusCodes({
@ResponseCode(code = 200, condition = "success"),
@ResponseCode(code = 401, condition = "not authenticated / invalid credentials"),
@ResponseCode(code = 403, condition = "not authorized, the current user has no privileges to read the repository role"),
@ResponseCode(code = 404, condition = "not found, no repository role with the specified name available"),
@ResponseCode(code = 500, condition = "internal server error")
})
public Response get(@PathParam("name") String name) {
return adapter.get(name, repositoryRoleToDtoMapper::map);
}
/**
* Deletes a repository role.
*
* <strong>Note:</strong> This method requires "repositoryRole" privilege.
*
* @param name the name of the repository role to delete.
*/
@DELETE
@Path("")
@StatusCodes({
@ResponseCode(code = 204, condition = "delete success or nothing to delete"),
@ResponseCode(code = 401, condition = "not authenticated / invalid credentials"),
@ResponseCode(code = 403, condition = "not authorized, the current user does not have the \"repositoryRole\" privilege"),
@ResponseCode(code = 500, condition = "internal server error")
})
@TypeHint(TypeHint.NO_CONTENT.class)
public Response delete(@PathParam("name") String name) {
return adapter.delete(name);
}
/**
* Modifies the given repository role.
*
* <strong>Note:</strong> This method requires "repositoryRole" privilege.
*
* @param name name of the repository role to be modified
* @param repositoryRole repository role object to modify
*/
@PUT
@Path("")
@Consumes(VndMediaType.REPOSITORY_ROLE)
@StatusCodes({
@ResponseCode(code = 204, condition = "update success"),
@ResponseCode(code = 400, condition = "Invalid body, e.g. illegal change of repository role name"),
@ResponseCode(code = 401, condition = "not authenticated / invalid credentials"),
@ResponseCode(code = 403, condition = "not authorized, the current user does not have the \"repositoryRole\" privilege"),
@ResponseCode(code = 404, condition = "not found, no repository role with the specified name available"),
@ResponseCode(code = 500, condition = "internal server error")
})
@TypeHint(TypeHint.NO_CONTENT.class)
public Response update(@PathParam("name") String name, @Valid RepositoryRoleDto repositoryRole) {
return adapter.update(name, existing -> dtoToRepositoryRoleMapper.map(repositoryRole));
}
}

View File

@@ -0,0 +1,34 @@
package sonia.scm.api.v2.resources;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.ws.rs.Path;
/**
* RESTful web service resource to manage repository roles.
*/
@Path(RepositoryRoleRootResource.REPOSITORY_ROLES_PATH_V2)
public class RepositoryRoleRootResource {
static final String REPOSITORY_ROLES_PATH_V2 = "v2/repository-roles/";
private final Provider<RepositoryRoleCollectionResource> repositoryRoleCollectionResource;
private final Provider<RepositoryRoleResource> repositoryRoleResource;
@Inject
public RepositoryRoleRootResource(Provider<RepositoryRoleCollectionResource> repositoryRoleCollectionResource,
Provider<RepositoryRoleResource> repositoryRoleResource) {
this.repositoryRoleCollectionResource = repositoryRoleCollectionResource;
this.repositoryRoleResource = repositoryRoleResource;
}
@Path("")
public RepositoryRoleCollectionResource getRepositoryRoleCollectionResource() {
return repositoryRoleCollectionResource.get();
}
@Path("{name}")
public RepositoryRoleResource getRepositoryRoleResource() {
return repositoryRoleResource.get();
}
}

View File

@@ -0,0 +1,42 @@
package sonia.scm.api.v2.resources;
import de.otto.edison.hal.Embedded;
import de.otto.edison.hal.Links;
import org.mapstruct.Mapper;
import org.mapstruct.ObjectFactory;
import sonia.scm.repository.RepositoryRole;
import sonia.scm.repository.RepositoryRoleManager;
import sonia.scm.repository.RepositoryRolePermissions;
import javax.inject.Inject;
import static de.otto.edison.hal.Embedded.embeddedBuilder;
import static de.otto.edison.hal.Link.link;
import static de.otto.edison.hal.Links.linkingTo;
// Mapstruct does not support parameterized (i.e. non-default) constructors. Thus, we need to use field injection.
@SuppressWarnings("squid:S3306")
@Mapper
public abstract class RepositoryRoleToRepositoryRoleDtoMapper extends BaseMapper<RepositoryRole, RepositoryRoleDto> {
@Inject
private RepositoryRoleManager repositoryRoleManager;
@Inject
private ResourceLinks resourceLinks;
@ObjectFactory
RepositoryRoleDto createDto(RepositoryRole repositoryRole) {
Links.Builder linksBuilder = linkingTo().self(resourceLinks.repositoryRole().self(repositoryRole.getName()));
if (RepositoryRolePermissions.modify().isPermitted()) {
linksBuilder.single(link("delete", resourceLinks.repositoryRole().delete(repositoryRole.getName())));
linksBuilder.single(link("update", resourceLinks.repositoryRole().update(repositoryRole.getName())));
}
Embedded.Builder embeddedBuilder = embeddedBuilder();
applyEnrichers(new EdisonHalAppender(linksBuilder, embeddedBuilder), repositoryRole);
return new RepositoryRoleDto(linksBuilder.build(), embeddedBuilder.build());
}
}

View File

@@ -172,7 +172,6 @@ class ResourceLinks {
}
}
UserCollectionLinks userCollection() {
return new UserCollectionLinks(scmPathInfoStore.get());
}
@@ -522,8 +521,50 @@ class ResourceLinks {
public String content(String namespace, String name, String revision, String path) {
return addPath(sourceLinkBuilder.method("getRepositoryResource").parameters(namespace, name).method("content").parameters().method("get").parameters(revision, "").href(), path);
}
}
RepositoryRoleLinks repositoryRole() {
return new RepositoryRoleLinks(scmPathInfoStore.get());
}
static class RepositoryRoleLinks {
private final LinkBuilder repositoryRoleLinkBuilder;
RepositoryRoleLinks(ScmPathInfo pathInfo) {
repositoryRoleLinkBuilder = new LinkBuilder(pathInfo, RepositoryRoleRootResource.class, RepositoryRoleResource.class);
}
String self(String name) {
return repositoryRoleLinkBuilder.method("getRepositoryRoleResource").parameters(name).method("get").parameters().href();
}
String delete(String name) {
return repositoryRoleLinkBuilder.method("getRepositoryRoleResource").parameters(name).method("delete").parameters().href();
}
String update(String name) {
return repositoryRoleLinkBuilder.method("getRepositoryRoleResource").parameters(name).method("update").parameters().href();
}
}
RepositoryRoleCollectionLinks repositoryRoleCollection() {
return new RepositoryRoleCollectionLinks(scmPathInfoStore.get());
}
static class RepositoryRoleCollectionLinks {
private final LinkBuilder collectionLinkBuilder;
RepositoryRoleCollectionLinks(ScmPathInfo pathInfo) {
collectionLinkBuilder = new LinkBuilder(pathInfo, RepositoryRoleRootResource.class, RepositoryRoleCollectionResource.class);
}
String self() {
return collectionLinkBuilder.method("getRepositoryRoleCollectionResource").parameters().method("getAll").parameters().href();
}
String create() {
return collectionLinkBuilder.method("getRepositoryRoleCollectionResource").parameters().method("create").parameters().href();
}
}
public RepositoryPermissionLinks repositoryPermission() {