Add ui plugin and self links

This commit is contained in:
René Pfeuffer
2018-10-01 12:59:45 +02:00
parent aed4c60296
commit 2c5fdd6b41
5 changed files with 61 additions and 10 deletions

View File

@@ -1,6 +1,5 @@
package sonia.scm.api.v2.resources;
import de.otto.edison.hal.Link;
import de.otto.edison.hal.Links;
import org.apache.shiro.SecurityUtils;
import sonia.scm.config.ConfigurationPermissions;
@@ -9,6 +8,8 @@ import sonia.scm.user.UserPermissions;
import javax.inject.Inject;
import static de.otto.edison.hal.Link.link;
public class IndexDtoGenerator {
private final ResourceLinks resourceLinks;
@@ -20,25 +21,27 @@ public class IndexDtoGenerator {
public IndexDto generate() {
Links.Builder builder = Links.linkingTo();
builder.self(resourceLinks.index().self());
builder.single(link("uiPlugins", resourceLinks.uiPluginCollection().self()));
if (SecurityUtils.getSubject().isAuthenticated()) {
builder.single(
Link.link("me", resourceLinks.me().self()),
Link.link("logout", resourceLinks.authentication().logout())
link("me", resourceLinks.me().self()),
link("logout", resourceLinks.authentication().logout())
);
if (UserPermissions.list().isPermitted()) {
builder.single(Link.link("users", resourceLinks.userCollection().self()));
builder.single(link("users", resourceLinks.userCollection().self()));
}
if (GroupPermissions.list().isPermitted()) {
builder.single(Link.link("groups", resourceLinks.groupCollection().self()));
builder.single(link("groups", resourceLinks.groupCollection().self()));
}
if (ConfigurationPermissions.list().isPermitted()) {
builder.single(Link.link("config", resourceLinks.config().self()));
builder.single(link("config", resourceLinks.config().self()));
}
builder.single(Link.link("repositories", resourceLinks.repositoryCollection().self()));
builder.single(link("repositories", resourceLinks.repositoryCollection().self()));
} else {
builder.single(
Link.link("formLogin", resourceLinks.authentication().formLogin()),
Link.link("jsonLogin", resourceLinks.authentication().jsonLogin())
link("formLogin", resourceLinks.authentication().formLogin()),
link("jsonLogin", resourceLinks.authentication().jsonLogin())
);
}

View File

@@ -20,6 +20,7 @@ public class IndexResource {
}
@GET
@Path("")
@Produces(VndMediaType.INDEX)
@TypeHint(IndexDto.class)
public IndexDto getIndex() {

View File

@@ -440,7 +440,6 @@ class ResourceLinks {
}
}
public UIPluginLinks uiPlugin() {
return new UIPluginLinks(scmPathInfoStore.get());
}
@@ -496,4 +495,21 @@ class ResourceLinks {
return loginLinkBuilder.method("logout").parameters().href();
}
}
public IndexLinks index() {
return new IndexLinks(scmPathInfoStore.get());
}
static class IndexLinks {
private final LinkBuilder indexLinkBuilder;
IndexLinks(ScmPathInfo pathInfo) {
indexLinkBuilder = new LinkBuilder(pathInfo, IndexResource.class);
}
String self() {
return indexLinkBuilder.method("getIndex").parameters().href();
}
}
}