Search in sources :

Example 31 with AuthManager

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

the class AuthTest method testUpdateBelong.

@Test
public void testUpdateBelong() throws InterruptedException {
    HugeGraph graph = graph();
    AuthManager authManager = graph.authManager();
    Id user = authManager.createUser(makeUser("tom", "pass1"));
    Id group = authManager.createGroup(makeGroup("group1"));
    HugeBelong belong = makeBelong(user, group);
    belong.description("description1");
    Id id = authManager.createBelong(belong);
    belong = authManager.getBelong(id);
    Assert.assertEquals(user, belong.source());
    Assert.assertEquals(group, belong.target());
    Assert.assertEquals("description1", belong.description());
    Assert.assertEquals(belong.create(), belong.update());
    Date oldUpdateTime = belong.update();
    Thread.sleep(1L);
    belong.description("description2");
    authManager.updateBelong(belong);
    HugeBelong belong2 = authManager.getBelong(id);
    Assert.assertEquals(user, belong.source());
    Assert.assertEquals(group, belong.target());
    Assert.assertEquals("description2", belong.description());
    Assert.assertEquals(oldUpdateTime, belong2.create());
    Assert.assertNotEquals(oldUpdateTime, belong2.update());
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        Id group2 = authManager.createGroup(makeGroup("group2"));
        HugeBelong belong3 = makeBelong(user, group2);
        authManager.updateBelong(belong3);
    }, e -> {
        Assert.assertContains("Can't save belong", e.getMessage());
        Assert.assertContains("that not exists", e.getMessage());
    });
}
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) Date(java.util.Date) Test(org.junit.Test)

Example 32 with AuthManager

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

the class AuthTest method testListAllAccess.

@Test
public void testListAllAccess() {
    HugeGraph graph = graph();
    AuthManager authManager = graph.authManager();
    Id group = authManager.createGroup(makeGroup("group1"));
    Id target1 = authManager.createTarget(makeTarget("graph1", "url1"));
    Id target2 = authManager.createTarget(makeTarget("graph2", "url2"));
    authManager.createAccess(makeAccess(group, target1, HugePermission.READ));
    authManager.createAccess(makeAccess(group, target2, HugePermission.READ));
    List<HugeAccess> access = authManager.listAllAccess(-1);
    Assert.assertEquals(2, access.size());
    Assert.assertEquals(ImmutableSet.of(target1, target2), ImmutableSet.of(access.get(0).target(), access.get(1).target()));
    Assert.assertEquals(0, authManager.listAllAccess(0).size());
    Assert.assertEquals(1, authManager.listAllAccess(1).size());
    Assert.assertEquals(2, authManager.listAllAccess(2).size());
    Assert.assertEquals(2, authManager.listAllAccess(3).size());
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) AuthManager(com.baidu.hugegraph.auth.AuthManager) HugeAccess(com.baidu.hugegraph.auth.HugeAccess) Id(com.baidu.hugegraph.backend.id.Id) Test(org.junit.Test)

Example 33 with AuthManager

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

the class AuthTest method testDeleteGroup.

@Test
public void testDeleteGroup() {
    HugeGraph graph = graph();
    AuthManager authManager = graph.authManager();
    Id id1 = authManager.createGroup(makeGroup("group1"));
    Id id2 = authManager.createGroup(makeGroup("group2"));
    Assert.assertEquals(2, authManager.listAllGroups(-1).size());
    HugeGroup group = authManager.deleteGroup(id1);
    Assert.assertEquals("group1", group.name());
    Assert.assertEquals(1, authManager.listAllGroups(-1).size());
    group = authManager.deleteGroup(id2);
    Assert.assertEquals("group2", group.name());
    Assert.assertEquals(0, authManager.listAllGroups(-1).size());
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        Id user = authManager.createUser(makeUser("tom", "pass1"));
        authManager.deleteGroup(user);
    });
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) AuthManager(com.baidu.hugegraph.auth.AuthManager) HugeGroup(com.baidu.hugegraph.auth.HugeGroup) Id(com.baidu.hugegraph.backend.id.Id) Test(org.junit.Test)

Example 34 with AuthManager

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

the class AuthTest method testDeleteAccess.

@Test
public void testDeleteAccess() {
    HugeGraph graph = graph();
    AuthManager authManager = graph.authManager();
    Id group = authManager.createGroup(makeGroup("group1"));
    Id target1 = authManager.createTarget(makeTarget("graph1", "url1"));
    Id target2 = authManager.createTarget(makeTarget("graph2", "url2"));
    Id id1 = authManager.createAccess(makeAccess(group, target1, HugePermission.READ));
    Id id2 = authManager.createAccess(makeAccess(group, target2, HugePermission.READ));
    Assert.assertEquals(2, authManager.listAllAccess(-1).size());
    HugeAccess access = authManager.deleteAccess(id1);
    Assert.assertEquals(target1, access.target());
    Assert.assertEquals(1, authManager.listAllAccess(-1).size());
    Assert.assertEquals(1, authManager.listAllAccess(2).size());
    access = authManager.deleteAccess(id2);
    Assert.assertEquals(target2, access.target());
    Assert.assertEquals(0, authManager.listAllAccess(-1).size());
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        Id user = authManager.createUser(makeUser("tom", "pass1"));
        Id belong = authManager.createBelong(makeBelong(user, group));
        authManager.deleteAccess(belong);
    });
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) AuthManager(com.baidu.hugegraph.auth.AuthManager) HugeAccess(com.baidu.hugegraph.auth.HugeAccess) Id(com.baidu.hugegraph.backend.id.Id) Test(org.junit.Test)

Example 35 with AuthManager

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

the class AuthTest method testValidateUserByNameAndPassword.

@Test
public void testValidateUserByNameAndPassword() {
    AuthManager authManager = graph().authManager();
    HugeUser user = makeUser("test", StringEncoding.hashPassword("pass"));
    Id userId = authManager.createUser(user);
    UserWithRole userWithRole;
    userWithRole = authManager.validateUser("test", "pass");
    Assert.assertEquals(userId, userWithRole.userId());
    Assert.assertEquals("test", userWithRole.username());
    Assert.assertEquals("{\"roles\":{}}", userWithRole.role().toJson());
    // Error case
    userWithRole = authManager.validateUser("huge", "graph");
    Assert.assertNull(userWithRole.userId());
    Assert.assertEquals("huge", userWithRole.username());
    Assert.assertNull(userWithRole.role());
}
Also used : AuthManager(com.baidu.hugegraph.auth.AuthManager) UserWithRole(com.baidu.hugegraph.auth.UserWithRole) Id(com.baidu.hugegraph.backend.id.Id) HugeUser(com.baidu.hugegraph.auth.HugeUser) 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