Skip to content

Commit d7bfdb6

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

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public String createManager(@Context GraphManager manager,
7373
AuthManager authManager = manager.authManager();
7474
validUser(authManager, user);
7575

76-
String creator = HugeGraphAuthProxy.getContext().user().username();
76+
String creator = HugeGraphAuthProxy.username();
7777
switch (type) {
7878
case SPACE:
7979
validGraphSpace(manager, graphSpace);
@@ -124,7 +124,7 @@ public void delete(@Context GraphManager manager,
124124
AuthManager authManager = manager.authManager();
125125
validType(type);
126126
validUser(authManager, user);
127-
String actionUser = HugeGraphAuthProxy.getContext().user().username();
127+
String actionUser = HugeGraphAuthProxy.username();
128128

129129
switch (type) {
130130
case SPACE:
@@ -193,7 +193,7 @@ public String checkRole(@Context GraphManager manager,
193193

194194
validType(type);
195195
AuthManager authManager = manager.authManager();
196-
String user = HugeGraphAuthProxy.getContext().user().username();
196+
String user = HugeGraphAuthProxy.username();
197197

198198
boolean result;
199199
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)