use of com.baidu.hugegraph.auth.HugeBelong in project incubator-hugegraph by apache.
the class BelongAPI method create.
@POST
@Timed
@Status(Status.CREATED)
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
public String create(@Context GraphManager manager, @PathParam("graph") String graph, JsonBelong jsonBelong) {
LOG.debug("Graph [{}] create belong: {}", graph, jsonBelong);
checkCreatingBody(jsonBelong);
HugeGraph g = graph(manager, graph);
HugeBelong belong = jsonBelong.build();
belong.id(manager.authManager().createBelong(belong));
return manager.serializer(g).writeAuthElement(belong);
}
use of com.baidu.hugegraph.auth.HugeBelong in project incubator-hugegraph by apache.
the class BelongAPI method update.
@PUT
@Timed
@Path("{id}")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
public String update(@Context GraphManager manager, @PathParam("graph") String graph, @PathParam("id") String id, JsonBelong jsonBelong) {
LOG.debug("Graph [{}] update belong: {}", graph, jsonBelong);
checkUpdatingBody(jsonBelong);
HugeGraph g = graph(manager, graph);
HugeBelong belong;
try {
belong = manager.authManager().getBelong(UserAPI.parseId(id));
} catch (NotFoundException e) {
throw new IllegalArgumentException("Invalid belong id: " + id);
}
belong = jsonBelong.build(belong);
manager.authManager().updateBelong(belong);
return manager.serializer(g).writeAuthElement(belong);
}
use of com.baidu.hugegraph.auth.HugeBelong in project incubator-hugegraph by apache.
the class BelongAPI method list.
@GET
@Timed
@Produces(APPLICATION_JSON_WITH_CHARSET)
public String list(@Context GraphManager manager, @PathParam("graph") String graph, @QueryParam("user") String user, @QueryParam("group") String group, @QueryParam("limit") @DefaultValue("100") long limit) {
LOG.debug("Graph [{}] list belongs by user {} or group {}", graph, user, group);
E.checkArgument(user == null || group == null, "Can't pass both user and group at the same time");
HugeGraph g = graph(manager, graph);
List<HugeBelong> belongs;
if (user != null) {
Id id = UserAPI.parseId(user);
belongs = manager.authManager().listBelongByUser(id, limit);
} else if (group != null) {
Id id = UserAPI.parseId(group);
belongs = manager.authManager().listBelongByGroup(id, limit);
} else {
belongs = manager.authManager().listAllBelong(limit);
}
return manager.serializer(g).writeAuthElements("belongs", belongs);
}
use of com.baidu.hugegraph.auth.HugeBelong in project incubator-hugegraph by apache.
the class AuthTest method makeBelong.
private static HugeBelong makeBelong(Id user, Id group) {
HugeBelong belong = new HugeBelong(user, group);
belong.creator("admin");
return belong;
}
use of com.baidu.hugegraph.auth.HugeBelong in project incubator-hugegraph by apache.
the class AuthTest method testListBelong.
@Test
public void testListBelong() {
HugeGraph graph = graph();
AuthManager authManager = graph.authManager();
Id user = authManager.createUser(makeUser("tom", "pass1"));
Id group1 = authManager.createGroup(makeGroup("group1"));
Id group2 = authManager.createGroup(makeGroup("group2"));
Id id1 = authManager.createBelong(makeBelong(user, group1));
Id id2 = authManager.createBelong(makeBelong(user, group2));
List<HugeBelong> belongs = authManager.listBelong(ImmutableList.of(id1, id2));
Assert.assertEquals(2, belongs.size());
Assert.assertEquals(user, belongs.get(0).source());
Assert.assertEquals(user, belongs.get(1).source());
Assert.assertEquals(group1, belongs.get(0).target());
Assert.assertEquals(group2, belongs.get(1).target());
belongs = authManager.listBelong(ImmutableList.of(id1, id2, id2));
Assert.assertEquals(3, belongs.size());
belongs = authManager.listBelong(ImmutableList.of(id1, id2, IdGenerator.of("fake")));
Assert.assertEquals(2, belongs.size());
belongs = authManager.listBelongByUser(user, -1);
Assert.assertEquals(2, belongs.size());
Assert.assertEquals(user, belongs.get(0).source());
Assert.assertEquals(user, belongs.get(1).source());
belongs = authManager.listBelongByGroup(group1, -1);
Assert.assertEquals(1, belongs.size());
Assert.assertEquals(user, belongs.get(0).source());
Assert.assertEquals(group1, belongs.get(0).target());
belongs = authManager.listBelongByGroup(group2, -1);
Assert.assertEquals(1, belongs.size());
Assert.assertEquals(user, belongs.get(0).source());
Assert.assertEquals(group2, belongs.get(0).target());
}
Aggregations