imbajin commented on code in PR #3159:
URL: https://github.com/apache/hugegraph/pull/3159#discussion_r3883995645


##########
hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/space/GraphSpaceAPI.java:
##########
@@ -203,20 +205,25 @@ public String checkDefaultRole(@Context GraphManager 
manager,
             defaultRole.equals(HugeDefaultRole.SPACE)) {
             throw new ForbiddenException("Forbidden to check role " + role);
         }
-        boolean hasGraph = defaultRole.equals(HugeDefaultRole.OBSERVER);
-        E.checkArgument(!hasGraph || StringUtils.isNotEmpty(graph),
-                        "Must set a graph for observer");
+        boolean hasGraph = defaultRole.equals(HugeDefaultRole.OBSERVER) &&
+                           StringUtils.isNotEmpty(graph);
         if (hasGraph) {
             validGraph(manager, name, graph);
         }
 
         boolean result;
         if (hasGraph) {
-            result = authManager.isDefaultRole(name, graph, user,
-                                               defaultRole);
+            result = authManager.isDefaultRole(name, graph, user, defaultRole);
         } else {
-            result = authManager.isDefaultRole(name, user,
-                                               defaultRole);
+            result = authManager.isDefaultRole(name, user, defaultRole);
+            if (!result && defaultRole.equals(HugeDefaultRole.OBSERVER)) {
+                for (String currentGraph : manager.graphs(name)) {
+                    if (authManager.isDefaultRole(name, currentGraph, user, 
defaultRole)) {
+                        result = true;
+                        break;
+                    }
+                }
+            }

Review Comment:
   Removed on `6851b1cd`: the request-time O(number of graphs) legacy observer 
fallback no longer exists.



##########
hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/auth/ManagerAPI.java:
##########
@@ -287,20 +287,24 @@ public String checkDefaultRole(@Context GraphManager 
manager,
             defaultRole = null; // unreachable, satisfies compiler
         }
         validGraphSpace(manager, graphSpace);
-        boolean hasGraph = defaultRole.equals(HugeDefaultRole.OBSERVER);
-        E.checkArgument(!hasGraph || StringUtils.isNotEmpty(graph),
-                        "Must set a graph for observer");
+        boolean hasGraph = defaultRole.equals(HugeDefaultRole.OBSERVER) && 
StringUtils.isNotEmpty(graph);
         if (hasGraph) {
             validGraph(manager, graphSpace, graph);
         }
 
         boolean result;
         if (hasGraph) {
-            result = authManager.isDefaultRole(graphSpace, graph, user,
-                                               defaultRole);
+            result = authManager.isDefaultRole(graphSpace, graph, user, 
defaultRole);
         } else {
-            result = authManager.isDefaultRole(graphSpace, user,
-                                               defaultRole);
+            result = authManager.isDefaultRole(graphSpace, user, defaultRole);
+            if (!result && defaultRole.equals(HugeDefaultRole.OBSERVER)) {
+                for (String currentGraph : manager.graphs(graphSpace)) {
+                    if (authManager.isDefaultRole(graphSpace, currentGraph, 
user, defaultRole)) {
+                        result = true;
+                        break;
+                    }
+                }
+            }

Review Comment:
   Removed on `6851b1cd` with the same legacy observer compatibility rollback; 
API 0.72 uses the explicit `ALL_GRAPHS` role.



##########
hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/api/space/SchemaTemplateAPITest.java:
##########
@@ -0,0 +1,81 @@
+/*
+ * 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 org.apache.hugegraph.unit.api.space;
+
+import java.util.function.Supplier;
+
+import org.apache.hugegraph.api.space.SchemaTemplateAPI;
+import org.apache.hugegraph.auth.AuthManager;
+import org.apache.hugegraph.testutil.Assert;
+import org.apache.hugegraph.testutil.Whitebox;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+public class SchemaTemplateAPITest {
+
+    private static final String GRAPHSPACE = "space";
+    private static final String CREATOR = "creator";
+
+    @Test
+    public void testCreatorCanManageTemplate() {
+        Supplier<AuthManager> authManager =
+                Mockito.mock(Supplier.class);
+
+        Assert.assertTrue(canManage(authManager, CREATOR));
+        Mockito.verifyZeroInteractions(authManager);

Review Comment:
   Fixed on `6851b1cd`: the test now uses `verifyNoInteractions`.



##########
hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/auth/HugeAuthenticator.java:
##########
@@ -359,8 +363,15 @@ public static boolean match(Object role, RolePermission 
grant,
                 }
             }
 
-            RolePermission rolePerm = RolePermission.fromJson(role);
-            return rolePerm.contains(grant);
+            RolePermission grantedRole = RolePermission.fromJson(grant);
+            RolePerm rolePerm = RolePerm.fromJson(role);
+            if (resourceObject != null &&
+                !RolePermission.isAdmin(grantedRole) &&
+                grantedRole.roles().containsKey(resourceObject.graphSpace()) &&
+                rolePerm.matchSpace(resourceObject.graphSpace(), "space")) {
+                return true;
+            }
+            return RolePermission.fromJson(role).contains(grantedRole);

Review Comment:
   Fixed on `6851b1cd`: `RolePermission` is parsed once and reused for the 
single-space manager decision.



##########
hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java:
##########
@@ -2434,19 +2428,14 @@ public static ConsumerWrapper wrap(Consumer consumer) {
 
         @Override
         public void accept(T t) {
-            boolean grpcThread = false;
             try {
-                grpcThread = Thread.currentThread().getName().contains("grpc");
-                if (grpcThread) {
-                    HugeGraphAuthProxy.setAdmin();
+                if (Thread.currentThread().getName().contains("grpc")) {
+                    HugeGraphAuthProxy.runAsAdmin(() -> 
this.consumer.accept(t));
+                } else {
+                    this.consumer.accept(t);
                 }

Review Comment:
   Fixed on `6851b1cd`: thread-name privilege inference was removed. The 
private metadata callback wrapper now elevates explicitly and restores every 
context.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to