Search in sources :

Example 11 with AuthManager

use of com.baidu.hugegraph.auth.AuthManager in project incubator-hugegraph by apache.

the class AuthTest method testListTargets.

@Test
public void testListTargets() {
    HugeGraph graph = graph();
    AuthManager authManager = graph.authManager();
    Id id1 = authManager.createTarget(makeTarget("target1", "url1"));
    Id id2 = authManager.createTarget(makeTarget("target2", "url2"));
    List<HugeTarget> targets = authManager.listTargets(ImmutableList.of(id1, id2));
    Assert.assertEquals(2, targets.size());
    Assert.assertEquals("target1", targets.get(0).name());
    Assert.assertEquals("target2", targets.get(1).name());
    targets = authManager.listTargets(ImmutableList.of(id1, id2, id2));
    Assert.assertEquals(3, targets.size());
    Assert.assertEquals("target1", targets.get(0).name());
    Assert.assertEquals("target2", targets.get(1).name());
    Assert.assertEquals("target2", targets.get(2).name());
    targets = authManager.listTargets(ImmutableList.of(id1, id2, IdGenerator.of("fake")));
    Assert.assertEquals(2, targets.size());
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) AuthManager(com.baidu.hugegraph.auth.AuthManager) Id(com.baidu.hugegraph.backend.id.Id) HugeTarget(com.baidu.hugegraph.auth.HugeTarget) Test(org.junit.Test)

Example 12 with AuthManager

use of com.baidu.hugegraph.auth.AuthManager in project incubator-hugegraph by apache.

the class AuthTest method testCreateAccess.

@Test
public void testCreateAccess() {
    HugeGraph graph = graph();
    AuthManager authManager = graph.authManager();
    Id group1 = authManager.createGroup(makeGroup("group1"));
    Id group2 = authManager.createGroup(makeGroup("group2"));
    Id target1 = authManager.createTarget(makeTarget("graph1", "url1"));
    Id target2 = authManager.createTarget(makeTarget("graph2", "url2"));
    Id id1 = authManager.createAccess(makeAccess(group1, target1, HugePermission.READ));
    Id id2 = authManager.createAccess(makeAccess(group1, target1, HugePermission.WRITE));
    Id id3 = authManager.createAccess(makeAccess(group1, target2, HugePermission.READ));
    Id id4 = authManager.createAccess(makeAccess(group2, target2, HugePermission.READ));
    HugeAccess access = authManager.getAccess(id1);
    Assert.assertEquals(group1, access.source());
    Assert.assertEquals(target1, access.target());
    Assert.assertEquals(HugePermission.READ, access.permission());
    Assert.assertEquals(access.create(), access.update());
    Map<String, Object> expected = new HashMap<>();
    expected.putAll(ImmutableMap.of("id", access.id(), "group", group1, "target", target1, "access_permission", HugePermission.READ, "access_creator", "admin"));
    expected.putAll(ImmutableMap.of("access_create", access.create(), "access_update", access.update()));
    Assert.assertEquals(expected, access.asMap());
    access = authManager.getAccess(id2);
    Assert.assertEquals(group1, access.source());
    Assert.assertEquals(target1, access.target());
    Assert.assertEquals(HugePermission.WRITE, access.permission());
    Assert.assertEquals(access.create(), access.update());
    expected = new HashMap<>();
    expected.putAll(ImmutableMap.of("id", access.id(), "group", group1, "target", target1, "access_permission", HugePermission.WRITE, "access_creator", "admin"));
    expected.putAll(ImmutableMap.of("access_create", access.create(), "access_update", access.update()));
    Assert.assertEquals(expected, access.asMap());
    access = authManager.getAccess(id3);
    Assert.assertEquals(group1, access.source());
    Assert.assertEquals(target2, access.target());
    Assert.assertEquals(HugePermission.READ, access.permission());
    Assert.assertEquals(access.create(), access.update());
    expected = new HashMap<>();
    expected.putAll(ImmutableMap.of("id", access.id(), "group", group1, "target", target2, "access_permission", HugePermission.READ, "access_creator", "admin"));
    expected.putAll(ImmutableMap.of("access_create", access.create(), "access_update", access.update()));
    Assert.assertEquals(expected, access.asMap());
    access = authManager.getAccess(id4);
    Assert.assertEquals(group2, access.source());
    Assert.assertEquals(target2, access.target());
    Assert.assertEquals(HugePermission.READ, access.permission());
    Assert.assertEquals(access.create(), access.update());
    expected = new HashMap<>();
    expected.putAll(ImmutableMap.of("id", access.id(), "group", group2, "target", target2, "access_permission", HugePermission.READ, "access_creator", "admin"));
    expected.putAll(ImmutableMap.of("access_create", access.create(), "access_update", access.update()));
    Assert.assertEquals(expected, access.asMap());
    List<HugeAccess> accesses = authManager.listAccessByGroup(group1, -1);
    Assert.assertEquals(3, accesses.size());
    accesses = authManager.listAccessByGroup(group2, -1);
    Assert.assertEquals(1, accesses.size());
    accesses = authManager.listAccessByTarget(target1, -1);
    Assert.assertEquals(2, accesses.size());
    accesses = authManager.listAccessByTarget(target2, -1);
    Assert.assertEquals(2, accesses.size());
    // Create access with description
    access = makeAccess(group2, target2, HugePermission.WRITE);
    access.description("something3");
    Id id5 = authManager.createAccess(access);
    access = authManager.getAccess(id5);
    Assert.assertEquals(group2, access.source());
    Assert.assertEquals(target2, access.target());
    Assert.assertEquals(HugePermission.WRITE, access.permission());
    Assert.assertEquals("something3", access.description());
    Assert.assertEquals(access.create(), access.update());
    expected = new HashMap<>();
    expected.putAll(ImmutableMap.of("id", access.id(), "group", group2, "target", target2, "access_permission", HugePermission.WRITE, "access_creator", "admin"));
    expected.putAll(ImmutableMap.of("access_description", "something3", "access_create", access.create(), "access_update", access.update()));
    Assert.assertEquals(expected, access.asMap());
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        authManager.createAccess(makeAccess(group1, target1, HugePermission.READ));
    }, e -> {
        Assert.assertContains("Can't save access", e.getMessage());
        Assert.assertContains("that already exists", e.getMessage());
    });
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) AuthManager(com.baidu.hugegraph.auth.AuthManager) HashMap(java.util.HashMap) HugeAccess(com.baidu.hugegraph.auth.HugeAccess) Id(com.baidu.hugegraph.backend.id.Id) Test(org.junit.Test)

Example 13 with AuthManager

use of com.baidu.hugegraph.auth.AuthManager in project incubator-hugegraph by apache.

the class AuthTest method testCreateTarget.

@Test
public void testCreateTarget() {
    HugeGraph graph = graph();
    AuthManager authManager = graph.authManager();
    HugeTarget target = makeTarget("graph1", "127.0.0.1:8080");
    target.creator("admin");
    Id id = authManager.createTarget(target);
    target = authManager.getTarget(id);
    Assert.assertEquals("graph1", target.name());
    Assert.assertEquals("127.0.0.1:8080", target.url());
    Assert.assertEquals(target.create(), target.update());
    HashMap<String, Object> expected = new HashMap<>();
    expected.putAll(ImmutableMap.of("target_name", "graph1", "target_graph", "graph1", "target_url", "127.0.0.1:8080", "target_creator", "admin"));
    expected.putAll(ImmutableMap.of("target_create", target.create(), "target_update", target.update(), "id", target.id()));
    Assert.assertEquals(expected, target.asMap());
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) AuthManager(com.baidu.hugegraph.auth.AuthManager) HashMap(java.util.HashMap) Id(com.baidu.hugegraph.backend.id.Id) HugeTarget(com.baidu.hugegraph.auth.HugeTarget) Test(org.junit.Test)

Example 14 with AuthManager

use of com.baidu.hugegraph.auth.AuthManager in project incubator-hugegraph by apache.

the class AuthTest method testUpdateTarget.

@Test
public void testUpdateTarget() throws InterruptedException {
    HugeGraph graph = graph();
    AuthManager authManager = graph.authManager();
    HugeTarget target = makeTarget("target1", "url1");
    Id id = authManager.createTarget(target);
    target = authManager.getTarget(id);
    Assert.assertEquals("target1", target.name());
    Assert.assertEquals("url1", target.url());
    Assert.assertEquals(target.create(), target.update());
    Date oldUpdateTime = target.update();
    Thread.sleep(1L);
    target.url("url2");
    authManager.updateTarget(target);
    HugeTarget target2 = authManager.getTarget(id);
    Assert.assertEquals("target1", target2.name());
    Assert.assertEquals("url2", target2.url());
    Assert.assertEquals(oldUpdateTime, target2.create());
    Assert.assertNotEquals(oldUpdateTime, target2.update());
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) AuthManager(com.baidu.hugegraph.auth.AuthManager) Id(com.baidu.hugegraph.backend.id.Id) HugeTarget(com.baidu.hugegraph.auth.HugeTarget) Date(java.util.Date) Test(org.junit.Test)

Example 15 with AuthManager

use of com.baidu.hugegraph.auth.AuthManager in project incubator-hugegraph by apache.

the class AuthTest method testCreateTargetWithRess.

@Test
public void testCreateTargetWithRess() {
    HugeGraph graph = graph();
    AuthManager authManager = graph.authManager();
    String ress = "[{\"type\": \"VERTEX\", \"label\": \"person\", " + "\"properties\":{\"city\": \"Beijing\"}}, " + "{\"type\": \"EDGE\", \"label\": \"transfer\"}]";
    HugeTarget target = makeTarget("graph1", "127.0.0.1:8080");
    target.resources(ress);
    Id id = authManager.createTarget(target);
    target = authManager.getTarget(id);
    Assert.assertEquals("graph1", target.name());
    Assert.assertEquals("127.0.0.1:8080", target.url());
    Assert.assertEquals(target.create(), target.update());
    String expect = "[{\"type\":\"VERTEX\",\"label\":\"person\"," + "\"properties\":{\"city\":\"Beijing\"}}," + "{\"type\":\"EDGE\",\"label\":\"transfer\"," + "\"properties\":null}]";
    Assert.assertEquals(expect, JsonUtil.toJson(target.asMap().get("target_resources")));
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) AuthManager(com.baidu.hugegraph.auth.AuthManager) Id(com.baidu.hugegraph.backend.id.Id) HugeTarget(com.baidu.hugegraph.auth.HugeTarget) Test(org.junit.Test)

Aggregations

AuthManager (com.baidu.hugegraph.auth.AuthManager)46 Test (org.junit.Test)43 Id (com.baidu.hugegraph.backend.id.Id)39 HugeGraph (com.baidu.hugegraph.HugeGraph)36 HugeUser (com.baidu.hugegraph.auth.HugeUser)12 HugeProject (com.baidu.hugegraph.auth.HugeProject)8 HugeTarget (com.baidu.hugegraph.auth.HugeTarget)8 HugeGroup (com.baidu.hugegraph.auth.HugeGroup)7 HugeAccess (com.baidu.hugegraph.auth.HugeAccess)6 HugeBelong (com.baidu.hugegraph.auth.HugeBelong)6 HashMap (java.util.HashMap)6 Date (java.util.Date)5 UserWithRole (com.baidu.hugegraph.auth.UserWithRole)2 HugeResource (com.baidu.hugegraph.auth.HugeResource)1 RolePermission (com.baidu.hugegraph.auth.RolePermission)1 NotFoundException (com.baidu.hugegraph.exception.NotFoundException)1 Timed (com.codahale.metrics.annotation.Timed)1 Consumes (jakarta.ws.rs.Consumes)1 PUT (jakarta.ws.rs.PUT)1 Path (jakarta.ws.rs.Path)1