Search in sources :

Example 1 with Belong

use of com.baidu.hugegraph.structure.auth.Belong in project incubator-hugegraph-toolchain by apache.

the class BelongApiTest method testCreate.

@Test
public void testCreate() {
    Belong belong1 = new Belong();
    belong1.user(user1);
    belong1.group(group1);
    belong1.description("user 1 => group 1");
    Belong belong2 = new Belong();
    belong2.user(user1);
    belong2.group(group2);
    belong2.description("user 1 => group 2");
    Belong belong3 = new Belong();
    belong3.user(user2);
    belong3.group(group2);
    belong3.description("user 2 => group 2");
    Belong result1 = api.create(belong1);
    Belong result2 = api.create(belong2);
    Belong result3 = api.create(belong3);
    Assert.assertEquals(user1.id(), result1.user());
    Assert.assertEquals(group1.id(), result1.group());
    Assert.assertEquals("user 1 => group 1", result1.description());
    Assert.assertEquals(user1.id(), result2.user());
    Assert.assertEquals(group2.id(), result2.group());
    Assert.assertEquals("user 1 => group 2", result2.description());
    Assert.assertEquals(user2.id(), result3.user());
    Assert.assertEquals(group2.id(), result3.group());
    Assert.assertEquals("user 2 => group 2", result3.description());
    Assert.assertThrows(ServerException.class, () -> {
        api.create(belong1);
    }, e -> {
        Assert.assertContains("Can't save belong", e.getMessage());
        Assert.assertContains("that already exists", e.getMessage());
    });
    Assert.assertThrows(ServerException.class, () -> {
        Belong belong4 = new Belong();
        belong4.user(user1);
        belong4.group(group1);
        api.create(belong3);
    }, e -> {
        Assert.assertContains("Can't save belong", e.getMessage());
        Assert.assertContains("that already exists", e.getMessage());
    });
}
Also used : Belong(com.baidu.hugegraph.structure.auth.Belong) Test(org.junit.Test)

Example 2 with Belong

use of com.baidu.hugegraph.structure.auth.Belong in project incubator-hugegraph-toolchain by apache.

the class BelongApiTest method createBelong.

private Belong createBelong(User user, Group group, String description) {
    Belong belong = new Belong();
    belong.user(user);
    belong.group(group);
    belong.description(description);
    return api.create(belong);
}
Also used : Belong(com.baidu.hugegraph.structure.auth.Belong)

Example 3 with Belong

use of com.baidu.hugegraph.structure.auth.Belong in project incubator-hugegraph-toolchain by apache.

the class AuthRestoreTest method testAuthRestoreForAllType.

@Test
public void testAuthRestoreForAllType() {
    this.loadData(HugeType.USER, "auth_users.txt");
    this.loadData(HugeType.TARGET, "auth_targets.txt");
    this.loadData(HugeType.GROUP, "auth_groups.txt");
    this.loadData(HugeType.BELONG, "auth_belongs.txt");
    this.loadData(HugeType.ACCESS, "auth_accesses.txt");
    String[] args = new String[] { "--throw-mode", "true", "--user", USER_NAME, "--password", USER_PASSWORD, "auth-restore", "--directory", DEFAULT_URL, "--init-password", "123456", "--strategy", "ignore" };
    HugeGraphCommand.main(args);
    List<String> idList = Lists.newArrayList();
    List<User> userList = this.client.auth().listUsers();
    Map<String, User> userMap = Maps.newHashMap();
    for (User user1 : userList) {
        userMap.put(user1.name(), user1);
    }
    Assert.assertTrue(userMap.containsKey("test_user1"));
    idList.add(userMap.get("test_user1").id().toString());
    List<Group> groups = this.client.auth().listGroups();
    Map<String, Group> groupMap = Maps.newHashMap();
    for (Group group : groups) {
        groupMap.put(group.name(), group);
    }
    Assert.assertTrue(groupMap.containsKey("test_group6"));
    idList.add(groupMap.get("test_group6").id().toString());
    List<Target> targets = this.client.auth().listTargets();
    Map<String, Target> targetMap = Maps.newHashMap();
    for (Target target : targets) {
        targetMap.put(target.name(), target);
    }
    Assert.assertTrue(targetMap.containsKey("test_target1"));
    idList.add(targetMap.get("test_target1").id().toString());
    List<Belong> belongs = this.client.auth().listBelongs();
    Assert.assertTrue(CollectionUtils.isNotEmpty(belongs));
    boolean checkUserAndGroup = false;
    for (Belong belong : belongs) {
        if (idList.contains(belong.user().toString()) && idList.contains(belong.group().toString())) {
            checkUserAndGroup = true;
            break;
        }
    }
    Assert.assertTrue(checkUserAndGroup);
    List<Access> accesses = this.client.auth().listAccesses();
    Assert.assertTrue(CollectionUtils.isNotEmpty(accesses));
    boolean checkGroupAndTarget = false;
    for (Access access : accesses) {
        if (idList.contains(access.group().toString()) && idList.contains(access.target().toString())) {
            checkGroupAndTarget = true;
            break;
        }
    }
    Assert.assertTrue(checkGroupAndTarget);
}
Also used : Group(com.baidu.hugegraph.structure.auth.Group) User(com.baidu.hugegraph.structure.auth.User) Access(com.baidu.hugegraph.structure.auth.Access) Target(com.baidu.hugegraph.structure.auth.Target) Belong(com.baidu.hugegraph.structure.auth.Belong) Test(org.junit.Test)

Example 4 with Belong

use of com.baidu.hugegraph.structure.auth.Belong in project incubator-hugegraph-toolchain by apache.

the class AuthManagerTest method testAuth.

@Test
public void testAuth() {
    User user = new User();
    user.name("bob");
    user.password("123456");
    user = auth().createUser(user);
    Group group = new Group();
    group.name("managers");
    group = auth().createGroup(group);
    Target gremlin = new Target();
    gremlin.name("gremlin");
    gremlin.graph("hugegraph");
    gremlin.url("127.0.0.1:8080");
    gremlin.resources(new HugeResource(HugeResourceType.GREMLIN));
    gremlin = auth().createTarget(gremlin);
    Target task = new Target();
    task.name("task");
    task.graph("hugegraph");
    task.url("127.0.0.1:8080");
    task.resources(new HugeResource(HugeResourceType.TASK));
    task = auth().createTarget(task);
    Belong belong = new Belong();
    belong.user(user);
    belong.group(group);
    belong = auth().createBelong(belong);
    Access access1 = new Access();
    access1.group(group);
    access1.target(gremlin);
    access1.permission(HugePermission.EXECUTE);
    access1 = auth().createAccess(access1);
    Access access2 = new Access();
    access2.group(group);
    access2.target(task);
    access2.permission(HugePermission.READ);
    access2 = auth().createAccess(access2);
    Project project1 = new Project("test");
    project1 = auth().createProject(project1);
    Assert.assertEquals("test", project1.name());
    Project project2 = new Project("test2");
    project2 = auth().createProject(project2);
    Assert.assertEquals("test2", project2.name());
    Project newProject1 = auth().getProject(project1);
    Assert.assertEquals(newProject1.id(), project1.id());
    Assert.assertTrue(CollectionUtils.isEmpty(newProject1.graphs()));
    List<Project> projects = auth().listProjects();
    Assert.assertNotNull(projects);
    Assert.assertEquals(2, projects.size());
    Set<String> graphs = ImmutableSet.of("graph1", "graph2");
    newProject1 = auth().projectAddGraphs(project1, graphs);
    Assert.assertNotNull(newProject1);
    Assert.assertEquals(graphs, newProject1.graphs());
    graphs = ImmutableSet.of("graph2");
    newProject1 = auth().projectRemoveGraphs(project1, ImmutableSet.of("graph1"));
    Assert.assertNotNull(newProject1);
    Assert.assertEquals(graphs, newProject1.graphs());
    Object project1Id = project1.id();
    project1 = new Project(project1Id);
    project1.description("test description");
    newProject1 = auth().updateProject(project1);
    Assert.assertEquals(newProject1.description(), project1.description());
    auth().deleteProject(project2);
    projects.remove(project2);
    List<Project> newProjects = auth().listProjects();
    Assert.assertEquals(newProjects, projects);
    UserRole role = auth().getUserRole(user);
    String r = "{\"roles\":{\"hugegraph\":" + "{\"READ\":[{\"type\":\"TASK\",\"label\":\"*\",\"properties\":null}]," + "\"EXECUTE\":[{\"type\":\"GREMLIN\",\"label\":\"*\",\"properties\":null}]}}}";
    Assert.assertEquals(r, role.toString());
    Login login = new Login();
    login.name("bob");
    login.password("123456");
    LoginResult result = auth().login(login);
    String token = result.token();
    HugeClient client = baseClient();
    client.setAuthContext("Bearer " + token);
    TokenPayload payload = auth().verifyToken();
    Assert.assertEquals("bob", payload.username());
    Assert.assertEquals(user.id(), payload.userId());
    auth().logout();
    client.resetAuthContext();
}
Also used : Group(com.baidu.hugegraph.structure.auth.Group) HugeClient(com.baidu.hugegraph.driver.HugeClient) User(com.baidu.hugegraph.structure.auth.User) LoginResult(com.baidu.hugegraph.structure.auth.LoginResult) Access(com.baidu.hugegraph.structure.auth.Access) Login(com.baidu.hugegraph.structure.auth.Login) TokenPayload(com.baidu.hugegraph.structure.auth.TokenPayload) Project(com.baidu.hugegraph.structure.auth.Project) Target(com.baidu.hugegraph.structure.auth.Target) UserRole(com.baidu.hugegraph.structure.auth.User.UserRole) HugeResource(com.baidu.hugegraph.structure.auth.HugeResource) Belong(com.baidu.hugegraph.structure.auth.Belong) Test(org.junit.Test)

Example 5 with Belong

use of com.baidu.hugegraph.structure.auth.Belong in project incubator-hugegraph-toolchain by apache.

the class BelongApiTest method clear.

@AfterClass
public static void clear() {
    List<Belong> belongs = api.list(null, null, -1);
    for (Belong belong : belongs) {
        api.delete(belong.id());
    }
    UserApiTest.clear();
    GroupApiTest.clear();
}
Also used : Belong(com.baidu.hugegraph.structure.auth.Belong) AfterClass(org.junit.AfterClass)

Aggregations

Belong (com.baidu.hugegraph.structure.auth.Belong)9 Test (org.junit.Test)6 Access (com.baidu.hugegraph.structure.auth.Access)3 Group (com.baidu.hugegraph.structure.auth.Group)3 Target (com.baidu.hugegraph.structure.auth.Target)3 User (com.baidu.hugegraph.structure.auth.User)3 Project (com.baidu.hugegraph.structure.auth.Project)2 HugeClient (com.baidu.hugegraph.driver.HugeClient)1 HugeResource (com.baidu.hugegraph.structure.auth.HugeResource)1 Login (com.baidu.hugegraph.structure.auth.Login)1 LoginResult (com.baidu.hugegraph.structure.auth.LoginResult)1 TokenPayload (com.baidu.hugegraph.structure.auth.TokenPayload)1 UserRole (com.baidu.hugegraph.structure.auth.User.UserRole)1 AfterClass (org.junit.AfterClass)1