Skip to content
This repository was archived by the owner on Dec 28, 2025. It is now read-only.

Commit f94f17b

Browse files
committed
add user/project module
1 parent ee42aea commit f94f17b

File tree

20 files changed

+860
-40
lines changed

20 files changed

+860
-40
lines changed

hubble-be/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
<dependency>
121121
<groupId>com.baidu.hugegraph</groupId>
122122
<artifactId>hugegraph-client</artifactId>
123-
<version>1.9.5</version>
123+
<version>1.9.6</version>
124124
</dependency>
125125

126126
<dependency>

hubble-be/src/main/java/com/baidu/hugegraph/common/Constant.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,6 @@ public final class Constant {
7474
);
7575

7676
public static final String[] LIKE_WILDCARDS = {"%", "_", "^", "[", "]"};
77+
78+
public static final String BEARER_TOKEN_PREFIX = "Bearer ";
7779
}

hubble-be/src/main/java/com/baidu/hugegraph/config/AuthClientConfiguration.java renamed to hubble-be/src/main/java/com/baidu/hugegraph/config/ClientConfiguration.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,41 @@
2323
import org.springframework.context.annotation.Configuration;
2424

2525
import com.baidu.hugegraph.driver.HugeClient;
26+
import com.baidu.hugegraph.driver.HugeClientBuilder;
2627
import com.baidu.hugegraph.options.HubbleOptions;
28+
import com.baidu.hugegraph.util.SessionUtil;
2729

2830
import lombok.extern.log4j.Log4j2;
2931

3032
@Log4j2
3133
@Configuration
32-
public class AuthClientConfiguration {
34+
public class ClientConfiguration {
3335

3436
public static final String AUTH_CLIENT_NAME = "authClient";
37+
public static final String ADMIN_CLIENT_NAME = "adminClient";
3538

3639
@Bean(AUTH_CLIENT_NAME)
3740
public HugeClient authClient(HugeConfig config) {
3841
String authUrl = config.get(HubbleOptions.AUTH_REMOTE_URL);
3942
String authGraph = config.get(HubbleOptions.AUTH_GRAPH);
40-
return HugeClient.builder(authUrl, authGraph).build();
43+
HugeClient authClient = HugeClient.builder(authUrl, authGraph).build();
44+
45+
SessionUtil.authClient = authClient;
46+
47+
return authClient;
48+
}
49+
50+
@Bean(ADMIN_CLIENT_NAME)
51+
public HugeClient adminClient(HugeConfig config) {
52+
String authUrl = config.get(HubbleOptions.AUTH_REMOTE_URL);
53+
String authGraph = config.get(HubbleOptions.AUTH_GRAPH);
54+
String adminPassword = config.get(HubbleOptions.ADMIN_PASSWORD);
55+
HugeClient adminClient = new HugeClientBuilder(authUrl, authGraph)
56+
.configUser("admin", adminPassword)
57+
.build();
58+
59+
SessionUtil.adminClient = adminClient;
60+
61+
return adminClient;
4162
}
4263
}

hubble-be/src/main/java/com/baidu/hugegraph/config/JacksonConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
4848
return mapper;
4949
}
5050

51-
public static class ResponseSerailizer extends JsonSerializer<Response> {
51+
public static class ResponseSerializer extends JsonSerializer<Response> {
5252

5353
@Override
5454
public void serialize(Response response, JsonGenerator generator,
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright 2017 HugeGraph Authors
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with this
6+
* work for additional information regarding copyright ownership. The ASF
7+
* licenses this file to You under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.baidu.hugegraph.controller.project;
21+
22+
import org.apache.commons.collections.CollectionUtils;
23+
import org.apache.commons.lang3.StringUtils;
24+
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.web.bind.annotation.DeleteMapping;
26+
import org.springframework.web.bind.annotation.GetMapping;
27+
import org.springframework.web.bind.annotation.PathVariable;
28+
import org.springframework.web.bind.annotation.PostMapping;
29+
import org.springframework.web.bind.annotation.PutMapping;
30+
import org.springframework.web.bind.annotation.RequestBody;
31+
import org.springframework.web.bind.annotation.RequestMapping;
32+
import org.springframework.web.bind.annotation.RequestParam;
33+
import org.springframework.web.bind.annotation.RestController;
34+
35+
import com.baidu.hugegraph.common.Constant;
36+
import com.baidu.hugegraph.entity.project.ProjectEntity;
37+
import com.baidu.hugegraph.service.project.ProjectService;
38+
import com.baidu.hugegraph.util.Ex;
39+
import com.baomidou.mybatisplus.core.metadata.IPage;
40+
41+
@RestController
42+
@RequestMapping(Constant.API_VERSION + "graph-connections/project")
43+
public class ProjectController {
44+
45+
@Autowired
46+
private ProjectService projectService;
47+
48+
@PostMapping
49+
public ProjectEntity createProject(@RequestBody ProjectEntity project) {
50+
this.checkParamsValid(project, true);
51+
return this.projectService.createProject(project);
52+
}
53+
54+
@PutMapping
55+
public ProjectEntity updateProject(@RequestBody ProjectEntity project) {
56+
this.checkParamsValid(project, false);
57+
return this.projectService.updateProject(project);
58+
}
59+
60+
@DeleteMapping("{id}")
61+
public void deleteProject(@PathVariable("id") String projectId) {
62+
this.projectService.deleteProject(projectId);
63+
}
64+
65+
@GetMapping("/list")
66+
public IPage<ProjectEntity> list(@RequestParam(value = "project_name",
67+
required = false)
68+
String projectName,
69+
@RequestParam(name = "page_no",
70+
required = false,
71+
defaultValue = "1")
72+
int pageNo,
73+
@RequestParam(name = "page_size",
74+
required = false,
75+
defaultValue = "10")
76+
int pageSize) {
77+
return this.projectService.list(projectName, pageNo, pageSize);
78+
}
79+
80+
private void checkParamsValid(ProjectEntity entity, boolean create) {
81+
Ex.check(StringUtils.isNotEmpty(entity.getName()),
82+
"common.param.cannot-be-null-or-empty", "project_name");
83+
Ex.check(CollectionUtils.isNotEmpty(entity.getAdminUsers()),
84+
"common.param.cannot-be-null-or-empty", "admin_users");
85+
Ex.check(CollectionUtils.isNotEmpty(entity.getOpUsers()),
86+
"common.param.cannot-be-null-or-empty", "op_users");
87+
if (create) {
88+
Ex.check(entity.getId() == null,
89+
"common.param.must-be-null", "id");
90+
} else {
91+
Ex.check(entity.getId() != null,
92+
"common.param.cannot-be-null", "id");
93+
}
94+
}
95+
}

hubble-be/src/main/java/com/baidu/hugegraph/controller/system/LoginController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import com.baidu.hugegraph.controller.BaseController;
3434
import com.baidu.hugegraph.entity.login.LoginBody;
3535
import com.baidu.hugegraph.entity.login.LoginResult;
36-
import com.baidu.hugegraph.entity.user.HubbleUser;
36+
import com.baidu.hugegraph.entity.user.UserEntity;
3737
import com.baidu.hugegraph.service.system.AuthService;
3838
import com.baidu.hugegraph.util.E;
3939

@@ -55,7 +55,7 @@ public void logout() {
5555
}
5656

5757
@GetMapping("/user")
58-
public HubbleUser currentUser(@RequestHeader(HttpHeaders.AUTHORIZATION)
58+
public UserEntity currentUser(@RequestHeader(HttpHeaders.AUTHORIZATION)
5959
String token) {
6060
E.checkArgumentNotNull(token,
6161
"Request header Authorization must not be null");
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2017 HugeGraph Authors
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with this
6+
* work for additional information regarding copyright ownership. The ASF
7+
* licenses this file to You under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.baidu.hugegraph.controller.user;
21+
22+
import java.util.List;
23+
24+
import org.apache.commons.lang3.StringUtils;
25+
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.web.bind.annotation.DeleteMapping;
27+
import org.springframework.web.bind.annotation.GetMapping;
28+
import org.springframework.web.bind.annotation.PostMapping;
29+
import org.springframework.web.bind.annotation.PutMapping;
30+
import org.springframework.web.bind.annotation.RequestBody;
31+
import org.springframework.web.bind.annotation.RequestMapping;
32+
import org.springframework.web.bind.annotation.RequestParam;
33+
import org.springframework.web.bind.annotation.RestController;
34+
35+
import com.baidu.hugegraph.common.Constant;
36+
import com.baidu.hugegraph.entity.user.UserEntity;
37+
import com.baidu.hugegraph.service.user.UserService;
38+
import com.baidu.hugegraph.util.Ex;
39+
import com.baomidou.mybatisplus.core.metadata.IPage;
40+
41+
@RestController
42+
@RequestMapping(Constant.API_VERSION + "graph-connections/user")
43+
public class UserController {
44+
45+
@Autowired
46+
private UserService userService;
47+
48+
@PostMapping
49+
public UserEntity create(@RequestBody UserEntity user) {
50+
this.checkUser(user, true);
51+
return this.userService.createUser(user);
52+
}
53+
54+
@PutMapping
55+
public UserEntity update(@RequestBody UserEntity user) {
56+
this.checkUser(user, false);
57+
return this.userService.updateUser(user);
58+
}
59+
60+
@DeleteMapping
61+
public void delete(@RequestBody List<String> userIds) {
62+
this.userService.deleteUser(userIds);
63+
}
64+
65+
@GetMapping
66+
public IPage<UserEntity> list(@RequestParam(value = "user_name",
67+
required = false)
68+
String userName,
69+
@RequestParam(name = "page_no",
70+
required = false,
71+
defaultValue = "1")
72+
int pageNo,
73+
@RequestParam(name = "page_size",
74+
required = false,
75+
defaultValue = "10")
76+
int pageSize) {
77+
return this.userService.list(userName, pageNo, pageSize);
78+
}
79+
80+
private void checkUser(UserEntity entity, boolean create) {
81+
Ex.check(StringUtils.isNotEmpty(entity.getUsername()),
82+
"common.param.cannot-be-null-or-empty", "user_name");
83+
Ex.check(StringUtils.isNotEmpty(entity.getPassword()),
84+
"common.param.cannot-be-null-or-empty", "user_password");
85+
if (create) {
86+
Ex.check(entity.getId() == null,
87+
"common.param.must-be-null", "id");
88+
} else {
89+
Ex.check(entity.getId() != null,
90+
"common.param.cannot-be-null", "id");
91+
}
92+
}
93+
}

hubble-be/src/main/java/com/baidu/hugegraph/entity/login/LoginResult.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
package com.baidu.hugegraph.entity.login;
2121

22+
import java.util.List;
23+
2224
import com.fasterxml.jackson.annotation.JsonProperty;
2325

2426
import lombok.AllArgsConstructor;
@@ -34,4 +36,7 @@ public class LoginResult {
3436

3537
@JsonProperty("token")
3638
private String token;
39+
40+
@JsonProperty("allowed_menus")
41+
private List<String> allowedMenus;
3742
}

hubble-be/src/main/java/com/baidu/hugegraph/entity/user/Group.java renamed to hubble-be/src/main/java/com/baidu/hugegraph/entity/project/ProjectEntity.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@
1717
* under the License.
1818
*/
1919

20-
package com.baidu.hugegraph.entity.user;
20+
package com.baidu.hugegraph.entity.project;
2121

22+
import java.util.List;
23+
import java.util.Set;
24+
25+
import com.baidu.hugegraph.entity.user.AuthElement;
26+
import com.baidu.hugegraph.structure.auth.Project;
2227
import com.fasterxml.jackson.annotation.JsonProperty;
2328

2429
import lombok.AllArgsConstructor;
@@ -30,13 +35,30 @@
3035
@NoArgsConstructor
3136
@AllArgsConstructor
3237
@Builder
33-
public class Group extends AuthElement {
38+
public class ProjectEntity extends AuthElement {
3439

35-
private static final long serialVersionUID = 3848451889435904990L;
40+
private static final long serialVersionUID = 6004765261545877970L;
3641

37-
@JsonProperty("group_name")
42+
@JsonProperty("project_name")
3843
private String name;
3944

40-
@JsonProperty("group_description")
45+
@JsonProperty("project_graphs")
46+
private Set<String> graphs;
47+
48+
@JsonProperty("admin_users")
49+
private List<String> adminUsers;
50+
51+
@JsonProperty("op_users")
52+
private List<String> opUsers;
53+
54+
@JsonProperty("description")
4155
private String description;
56+
57+
public static Project convertToProject(ProjectEntity entity) {
58+
Project project = new Project();
59+
project.description(entity.description);
60+
project.name(entity.name);
61+
project.graphs(entity.graphs);
62+
return project;
63+
}
4264
}

hubble-be/src/main/java/com/baidu/hugegraph/entity/user/HubbleUser.java renamed to hubble-be/src/main/java/com/baidu/hugegraph/entity/user/UserEntity.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
import java.util.List;
2323

24+
import com.baidu.hugegraph.structure.auth.Group;
25+
import com.baidu.hugegraph.structure.auth.HugeGroupTag;
26+
import com.baidu.hugegraph.structure.auth.User;
2427
import com.fasterxml.jackson.annotation.JsonProperty;
2528

2629
import lombok.AllArgsConstructor;
@@ -32,7 +35,7 @@
3235
@NoArgsConstructor
3336
@AllArgsConstructor
3437
@Builder
35-
public class HubbleUser extends AuthElement {
38+
public class UserEntity extends AuthElement {
3639

3740
private static final long serialVersionUID = 3121398441504040737L;
3841

@@ -53,4 +56,29 @@ public class HubbleUser extends AuthElement {
5356

5457
@JsonProperty("user_groups")
5558
private List<Group> groups;
59+
60+
@JsonProperty("platform_role")
61+
private List<HugeGroupTag> platformRoles;
62+
63+
public static User convertToUser(UserEntity userEntity) {
64+
User user = new User();
65+
user.name(userEntity.username);
66+
user.password(userEntity.password);
67+
user.phone(userEntity.phone);
68+
user.email(userEntity.email);
69+
user.description(userEntity.description);
70+
user.id(userEntity.id);
71+
return user;
72+
}
73+
74+
public static UserEntity convertFromUser(User user) {
75+
UserEntity userEntity = new UserEntity();
76+
userEntity.username = user.name();
77+
userEntity.password = user.password();
78+
userEntity.phone = user.phone();
79+
userEntity.email = user.email();
80+
userEntity.description = user.description();
81+
userEntity.id = user.id().toString();
82+
return userEntity;
83+
}
5684
}

0 commit comments

Comments
 (0)