Search in sources :

Example 1 with HugeBelong

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);
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) HugeBelong(com.baidu.hugegraph.auth.HugeBelong) Status(com.baidu.hugegraph.api.filter.StatusFilter.Status) POST(jakarta.ws.rs.POST) Consumes(jakarta.ws.rs.Consumes) Produces(jakarta.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed)

Example 2 with HugeBelong

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);
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) HugeBelong(com.baidu.hugegraph.auth.HugeBelong) NotFoundException(com.baidu.hugegraph.exception.NotFoundException) Path(jakarta.ws.rs.Path) Consumes(jakarta.ws.rs.Consumes) Produces(jakarta.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) PUT(jakarta.ws.rs.PUT)

Example 3 with HugeBelong

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);
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) HugeBelong(com.baidu.hugegraph.auth.HugeBelong) Id(com.baidu.hugegraph.backend.id.Id) Produces(jakarta.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(jakarta.ws.rs.GET)

Example 4 with HugeBelong

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;
}
Also used : HugeBelong(com.baidu.hugegraph.auth.HugeBelong)

Example 5 with HugeBelong

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());
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) HugeBelong(com.baidu.hugegraph.auth.HugeBelong) AuthManager(com.baidu.hugegraph.auth.AuthManager) Id(com.baidu.hugegraph.backend.id.Id) Test(org.junit.Test)

Aggregations

HugeBelong (com.baidu.hugegraph.auth.HugeBelong)11 HugeGraph (com.baidu.hugegraph.HugeGraph)10 Id (com.baidu.hugegraph.backend.id.Id)7 AuthManager (com.baidu.hugegraph.auth.AuthManager)6 Test (org.junit.Test)6 Timed (com.codahale.metrics.annotation.Timed)4 Produces (jakarta.ws.rs.Produces)4 Consumes (jakarta.ws.rs.Consumes)2 GET (jakarta.ws.rs.GET)2 Path (jakarta.ws.rs.Path)2 Status (com.baidu.hugegraph.api.filter.StatusFilter.Status)1 NotFoundException (com.baidu.hugegraph.exception.NotFoundException)1 POST (jakarta.ws.rs.POST)1 PUT (jakarta.ws.rs.PUT)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1