Skip to content

Commit

Permalink
add user/project module
Browse files Browse the repository at this point in the history
  • Loading branch information
corgiboygsj committed Jul 19, 2021
1 parent ee42aea commit f94f17b
Show file tree
Hide file tree
Showing 20 changed files with 860 additions and 40 deletions.
2 changes: 1 addition & 1 deletion hubble-be/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
<dependency>
<groupId>com.baidu.hugegraph</groupId>
<artifactId>hugegraph-client</artifactId>
<version>1.9.5</version>
<version>1.9.6</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,6 @@ public final class Constant {
);

public static final String[] LIKE_WILDCARDS = {"%", "_", "^", "[", "]"};

public static final String BEARER_TOKEN_PREFIX = "Bearer ";
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,41 @@
import org.springframework.context.annotation.Configuration;

import com.baidu.hugegraph.driver.HugeClient;
import com.baidu.hugegraph.driver.HugeClientBuilder;
import com.baidu.hugegraph.options.HubbleOptions;
import com.baidu.hugegraph.util.SessionUtil;

import lombok.extern.log4j.Log4j2;

@Log4j2
@Configuration
public class AuthClientConfiguration {
public class ClientConfiguration {

public static final String AUTH_CLIENT_NAME = "authClient";
public static final String ADMIN_CLIENT_NAME = "adminClient";

@Bean(AUTH_CLIENT_NAME)
public HugeClient authClient(HugeConfig config) {
String authUrl = config.get(HubbleOptions.AUTH_REMOTE_URL);
String authGraph = config.get(HubbleOptions.AUTH_GRAPH);
return HugeClient.builder(authUrl, authGraph).build();
HugeClient authClient = HugeClient.builder(authUrl, authGraph).build();

SessionUtil.authClient = authClient;

return authClient;
}

@Bean(ADMIN_CLIENT_NAME)
public HugeClient adminClient(HugeConfig config) {
String authUrl = config.get(HubbleOptions.AUTH_REMOTE_URL);
String authGraph = config.get(HubbleOptions.AUTH_GRAPH);
String adminPassword = config.get(HubbleOptions.ADMIN_PASSWORD);
HugeClient adminClient = new HugeClientBuilder(authUrl, authGraph)
.configUser("admin", adminPassword)
.build();

SessionUtil.adminClient = adminClient;

return adminClient;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
return mapper;
}

public static class ResponseSerailizer extends JsonSerializer<Response> {
public static class ResponseSerializer extends JsonSerializer<Response> {

@Override
public void serialize(Response response, JsonGenerator generator,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.baidu.hugegraph.controller.project;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baidu.hugegraph.common.Constant;
import com.baidu.hugegraph.entity.project.ProjectEntity;
import com.baidu.hugegraph.service.project.ProjectService;
import com.baidu.hugegraph.util.Ex;
import com.baomidou.mybatisplus.core.metadata.IPage;

@RestController
@RequestMapping(Constant.API_VERSION + "graph-connections/project")
public class ProjectController {

@Autowired
private ProjectService projectService;

@PostMapping
public ProjectEntity createProject(@RequestBody ProjectEntity project) {
this.checkParamsValid(project, true);
return this.projectService.createProject(project);
}

@PutMapping
public ProjectEntity updateProject(@RequestBody ProjectEntity project) {
this.checkParamsValid(project, false);
return this.projectService.updateProject(project);
}

@DeleteMapping("{id}")
public void deleteProject(@PathVariable("id") String projectId) {
this.projectService.deleteProject(projectId);
}

@GetMapping("/list")
public IPage<ProjectEntity> list(@RequestParam(value = "project_name",
required = false)
String projectName,
@RequestParam(name = "page_no",
required = false,
defaultValue = "1")
int pageNo,
@RequestParam(name = "page_size",
required = false,
defaultValue = "10")
int pageSize) {
return this.projectService.list(projectName, pageNo, pageSize);
}

private void checkParamsValid(ProjectEntity entity, boolean create) {
Ex.check(StringUtils.isNotEmpty(entity.getName()),
"common.param.cannot-be-null-or-empty", "project_name");
Ex.check(CollectionUtils.isNotEmpty(entity.getAdminUsers()),
"common.param.cannot-be-null-or-empty", "admin_users");
Ex.check(CollectionUtils.isNotEmpty(entity.getOpUsers()),
"common.param.cannot-be-null-or-empty", "op_users");
if (create) {
Ex.check(entity.getId() == null,
"common.param.must-be-null", "id");
} else {
Ex.check(entity.getId() != null,
"common.param.cannot-be-null", "id");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import com.baidu.hugegraph.controller.BaseController;
import com.baidu.hugegraph.entity.login.LoginBody;
import com.baidu.hugegraph.entity.login.LoginResult;
import com.baidu.hugegraph.entity.user.HubbleUser;
import com.baidu.hugegraph.entity.user.UserEntity;
import com.baidu.hugegraph.service.system.AuthService;
import com.baidu.hugegraph.util.E;

Expand All @@ -55,7 +55,7 @@ public void logout() {
}

@GetMapping("/user")
public HubbleUser currentUser(@RequestHeader(HttpHeaders.AUTHORIZATION)
public UserEntity currentUser(@RequestHeader(HttpHeaders.AUTHORIZATION)
String token) {
E.checkArgumentNotNull(token,
"Request header Authorization must not be null");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.baidu.hugegraph.controller.user;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baidu.hugegraph.common.Constant;
import com.baidu.hugegraph.entity.user.UserEntity;
import com.baidu.hugegraph.service.user.UserService;
import com.baidu.hugegraph.util.Ex;
import com.baomidou.mybatisplus.core.metadata.IPage;

@RestController
@RequestMapping(Constant.API_VERSION + "graph-connections/user")
public class UserController {

@Autowired
private UserService userService;

@PostMapping
public UserEntity create(@RequestBody UserEntity user) {
this.checkUser(user, true);
return this.userService.createUser(user);
}

@PutMapping
public UserEntity update(@RequestBody UserEntity user) {
this.checkUser(user, false);
return this.userService.updateUser(user);
}

@DeleteMapping
public void delete(@RequestBody List<String> userIds) {
this.userService.deleteUser(userIds);
}

@GetMapping
public IPage<UserEntity> list(@RequestParam(value = "user_name",
required = false)
String userName,
@RequestParam(name = "page_no",
required = false,
defaultValue = "1")
int pageNo,
@RequestParam(name = "page_size",
required = false,
defaultValue = "10")
int pageSize) {
return this.userService.list(userName, pageNo, pageSize);
}

private void checkUser(UserEntity entity, boolean create) {
Ex.check(StringUtils.isNotEmpty(entity.getUsername()),
"common.param.cannot-be-null-or-empty", "user_name");
Ex.check(StringUtils.isNotEmpty(entity.getPassword()),
"common.param.cannot-be-null-or-empty", "user_password");
if (create) {
Ex.check(entity.getId() == null,
"common.param.must-be-null", "id");
} else {
Ex.check(entity.getId() != null,
"common.param.cannot-be-null", "id");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package com.baidu.hugegraph.entity.login;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonProperty;

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

@JsonProperty("token")
private String token;

@JsonProperty("allowed_menus")
private List<String> allowedMenus;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@
* under the License.
*/

package com.baidu.hugegraph.entity.user;
package com.baidu.hugegraph.entity.project;

import java.util.List;
import java.util.Set;

import com.baidu.hugegraph.entity.user.AuthElement;
import com.baidu.hugegraph.structure.auth.Project;
import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.AllArgsConstructor;
Expand All @@ -30,13 +35,30 @@
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Group extends AuthElement {
public class ProjectEntity extends AuthElement {

private static final long serialVersionUID = 3848451889435904990L;
private static final long serialVersionUID = 6004765261545877970L;

@JsonProperty("group_name")
@JsonProperty("project_name")
private String name;

@JsonProperty("group_description")
@JsonProperty("project_graphs")
private Set<String> graphs;

@JsonProperty("admin_users")
private List<String> adminUsers;

@JsonProperty("op_users")
private List<String> opUsers;

@JsonProperty("description")
private String description;

public static Project convertToProject(ProjectEntity entity) {
Project project = new Project();
project.description(entity.description);
project.name(entity.name);
project.graphs(entity.graphs);
return project;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@

import java.util.List;

import com.baidu.hugegraph.structure.auth.Group;
import com.baidu.hugegraph.structure.auth.HugeGroupTag;
import com.baidu.hugegraph.structure.auth.User;
import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.AllArgsConstructor;
Expand All @@ -32,7 +35,7 @@
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class HubbleUser extends AuthElement {
public class UserEntity extends AuthElement {

private static final long serialVersionUID = 3121398441504040737L;

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

@JsonProperty("user_groups")
private List<Group> groups;

@JsonProperty("platform_role")
private List<HugeGroupTag> platformRoles;

public static User convertToUser(UserEntity userEntity) {
User user = new User();
user.name(userEntity.username);
user.password(userEntity.password);
user.phone(userEntity.phone);
user.email(userEntity.email);
user.description(userEntity.description);
user.id(userEntity.id);
return user;
}

public static UserEntity convertFromUser(User user) {
UserEntity userEntity = new UserEntity();
userEntity.username = user.name();
userEntity.password = user.password();
userEntity.phone = user.phone();
userEntity.email = user.email();
userEntity.description = user.description();
userEntity.id = user.id().toString();
return userEntity;
}
}
Loading

0 comments on commit f94f17b

Please sign in to comment.