Skip to content

Commit 310a9d1

Browse files
committed
fix(server): fix npe in non-auth mode
1 parent b12425c commit 310a9d1

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/auth/ManagerAPI.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
import jakarta.ws.rs.QueryParam;
5050
import jakarta.ws.rs.core.Context;
5151

52+
53+
54+
5255
@Path("graphspaces/{graphspace}/auth/managers")
5356
@Singleton
5457
@Tag(name = "ManagerAPI")
@@ -73,7 +76,7 @@ public String createManager(@Context GraphManager manager,
7376
AuthManager authManager = manager.authManager();
7477
validUser(authManager, user);
7578

76-
String creator = HugeGraphAuthProxy.getContext().user().username();
79+
String creator = HugeGraphAuthProxy.username();
7780
switch (type) {
7881
case SPACE:
7982
validGraphSpace(manager, graphSpace);
@@ -124,7 +127,7 @@ public void delete(@Context GraphManager manager,
124127
AuthManager authManager = manager.authManager();
125128
validType(type);
126129
validUser(authManager, user);
127-
String actionUser = HugeGraphAuthProxy.getContext().user().username();
130+
String actionUser = HugeGraphAuthProxy.username();
128131

129132
switch (type) {
130133
case SPACE:
@@ -193,7 +196,7 @@ public String checkRole(@Context GraphManager manager,
193196

194197
validType(type);
195198
AuthManager authManager = manager.authManager();
196-
String user = HugeGraphAuthProxy.getContext().user().username();
199+
String user = HugeGraphAuthProxy.username();
197200

198201
boolean result;
199202
switch (type) {

hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/GraphsAPI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public Object create(@Context GraphManager manager,
199199
}
200200
}
201201

202-
String creator = HugeGraphAuthProxy.getContext().user().username();
202+
String creator = HugeGraphAuthProxy.username();
203203

204204
if (StringUtils.isNotEmpty(clone)) {
205205
// Clone from existing graph

hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/space/GraphSpaceAPI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public String create(@Context GraphManager manager,
104104

105105
jsonGraphSpace.checkCreate(false);
106106

107-
String creator = HugeGraphAuthProxy.getContext().user().username();
107+
String creator = HugeGraphAuthProxy.username();
108108
GraphSpace exist = manager.graphSpace(jsonGraphSpace.name);
109109
E.checkArgument(exist == null, "The graph space '%s' has existed",
110110
jsonGraphSpace.name);

hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/auth/HugeGraphAuthProxy.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ public static Context setAdmin() {
186186
public static Context getContext() {
187187
// Return task context first
188188
String taskContext = TaskManager.getContext();
189+
190+
if (taskContext == null) {
191+
return null;
192+
}
193+
189194
User user = User.fromJson(taskContext);
190195
if (user != null) {
191196
return new Context(user);
@@ -953,6 +958,14 @@ public void updateTime(Date updateTime) {
953958
this.hugegraph.updateTime(updateTime);
954959
}
955960

961+
public static String username() {
962+
Context context = HugeGraphAuthProxy.getContext();
963+
if (context == null) {
964+
return "anonymous";
965+
}
966+
return context.user.username();
967+
}
968+
956969
private <V> Cache<Id, V> cache(String prefix, long capacity,
957970
long expiredTime) {
958971
String name = prefix + "-" + this.hugegraph.spaceGraphName();

0 commit comments

Comments
 (0)