Caideyipi commented on code in PR #13158:
URL: https://github.com/apache/iotdb/pull/13158#discussion_r1904959764


##########
iotdb-core/datanode/src/test/java/org/apache/iotdb/db/auth/entity/DataBasePrivilegeTest.java:
##########
@@ -0,0 +1,75 @@
+/*
+ * 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.iotdb.db.auth.entity;
+
+import org.apache.iotdb.commons.auth.entity.DatabasePrivilege;
+import org.apache.iotdb.commons.auth.entity.PrivilegeType;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+public class DataBasePrivilegeTest {
+  @Test
+  public void test() {
+    DatabasePrivilege dbPrivilege = new DatabasePrivilege("database");
+    dbPrivilege.grantDBPrivilege(PrivilegeType.ALTER);
+    dbPrivilege.grantDBPrivilege(PrivilegeType.INSERT);
+    dbPrivilege.grantDBGrantOption(PrivilegeType.ALTER);
+    dbPrivilege.grantTablePrivilege("test", PrivilegeType.SELECT);
+    dbPrivilege.grantTablePrivilege("test", PrivilegeType.DELETE);
+    dbPrivilege.grantTablePrivilege("test2", PrivilegeType.SELECT);
+    dbPrivilege.grantTableGrantOption("test", PrivilegeType.SELECT);
+    Assert.assertTrue(dbPrivilege.checkDBPrivilege(PrivilegeType.ALTER));
+    Assert.assertFalse(dbPrivilege.checkDBPrivilege(PrivilegeType.DELETE));
+    Assert.assertTrue(dbPrivilege.checkDBGrantOption(PrivilegeType.ALTER));
+    Assert.assertTrue(dbPrivilege.checkTablePrivilege("test", 
PrivilegeType.SELECT));
+    Assert.assertFalse(dbPrivilege.checkTablePrivilege("test2", 
PrivilegeType.CREATE));
+    Assert.assertTrue(dbPrivilege.checkTableGrantOption("test", 
PrivilegeType.SELECT));
+    DatabasePrivilege dbPrivilege2 = new DatabasePrivilege();
+    try {
+      ByteBuffer byteBuffer = dbPrivilege.serialize();
+      dbPrivilege2.deserialize(byteBuffer);
+    } catch (IOException e) {
+      Assert.fail();
+    }
+    Assert.assertEquals(dbPrivilege, dbPrivilege2);
+    String toString = dbPrivilege.toString();
+    Assert.assertEquals(
+        toString,
+        "Database(database):{ALTER_with_grant_option,INSERT,;"
+            + " Tables: [ test2(SELECT,) 
test(SELECT_with_grant_option,DELETE,)]}");
+    int mask = dbPrivilege.getAllPrivileges();
+    dbPrivilege2.revokeTablePrivilege("test", PrivilegeType.SELECT);
+    dbPrivilege2.revokeTableGrantOption("test", PrivilegeType.SELECT);
+    dbPrivilege2.revokeTablePrivilege("test", PrivilegeType.DELETE);
+    Assert.assertEquals(dbPrivilege2.getTablePrivilegeMap().size(), 1);
+    dbPrivilege2.revokeTablePrivilege("test2", PrivilegeType.SELECT);
+    dbPrivilege2.revokeDBPrivilege(PrivilegeType.INSERT);
+    dbPrivilege2.revokeDBGrantOption(PrivilegeType.ALTER);
+    Assert.assertEquals(dbPrivilege2.getTablePrivilegeMap().size(), 0);

Review Comment:
   Same as above



##########
iotdb-core/datanode/src/test/java/org/apache/iotdb/db/auth/entity/DataBasePrivilegeTest.java:
##########
@@ -0,0 +1,75 @@
+/*
+ * 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.iotdb.db.auth.entity;
+
+import org.apache.iotdb.commons.auth.entity.DatabasePrivilege;
+import org.apache.iotdb.commons.auth.entity.PrivilegeType;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+public class DataBasePrivilegeTest {
+  @Test
+  public void test() {
+    DatabasePrivilege dbPrivilege = new DatabasePrivilege("database");
+    dbPrivilege.grantDBPrivilege(PrivilegeType.ALTER);
+    dbPrivilege.grantDBPrivilege(PrivilegeType.INSERT);
+    dbPrivilege.grantDBGrantOption(PrivilegeType.ALTER);
+    dbPrivilege.grantTablePrivilege("test", PrivilegeType.SELECT);
+    dbPrivilege.grantTablePrivilege("test", PrivilegeType.DELETE);
+    dbPrivilege.grantTablePrivilege("test2", PrivilegeType.SELECT);
+    dbPrivilege.grantTableGrantOption("test", PrivilegeType.SELECT);
+    Assert.assertTrue(dbPrivilege.checkDBPrivilege(PrivilegeType.ALTER));
+    Assert.assertFalse(dbPrivilege.checkDBPrivilege(PrivilegeType.DELETE));
+    Assert.assertTrue(dbPrivilege.checkDBGrantOption(PrivilegeType.ALTER));
+    Assert.assertTrue(dbPrivilege.checkTablePrivilege("test", 
PrivilegeType.SELECT));
+    Assert.assertFalse(dbPrivilege.checkTablePrivilege("test2", 
PrivilegeType.CREATE));
+    Assert.assertTrue(dbPrivilege.checkTableGrantOption("test", 
PrivilegeType.SELECT));
+    DatabasePrivilege dbPrivilege2 = new DatabasePrivilege();
+    try {
+      ByteBuffer byteBuffer = dbPrivilege.serialize();
+      dbPrivilege2.deserialize(byteBuffer);
+    } catch (IOException e) {
+      Assert.fail();
+    }
+    Assert.assertEquals(dbPrivilege, dbPrivilege2);
+    String toString = dbPrivilege.toString();
+    Assert.assertEquals(
+        toString,
+        "Database(database):{ALTER_with_grant_option,INSERT,;"
+            + " Tables: [ test2(SELECT,) 
test(SELECT_with_grant_option,DELETE,)]}");
+    int mask = dbPrivilege.getAllPrivileges();
+    dbPrivilege2.revokeTablePrivilege("test", PrivilegeType.SELECT);
+    dbPrivilege2.revokeTableGrantOption("test", PrivilegeType.SELECT);
+    dbPrivilege2.revokeTablePrivilege("test", PrivilegeType.DELETE);
+    Assert.assertEquals(dbPrivilege2.getTablePrivilegeMap().size(), 1);

Review Comment:
   Notice the order of the expected value and the actual one



##########
iotdb-core/datanode/src/test/java/org/apache/iotdb/db/auth/entity/RoleTest.java:
##########
@@ -37,39 +39,55 @@ public void testRole_InitAndSerialize() throws 
IllegalPathException {
     Role role = new Role("role");
     PathPrivilege pathPrivilege = new PathPrivilege(new 
PartialPath("root.ln"));
     role.setPrivilegeList(Collections.singletonList(pathPrivilege));
-    role.addPathPrivilege(new PartialPath("root.ln"), 1, false);
-    role.addPathPrivilege(new PartialPath("root.ln"), 2, true);
+    role.grantPathPrivilege(new PartialPath("root.ln"), 
PrivilegeType.READ_SCHEMA, true);
+    role.grantPathPrivilege(new PartialPath("root.ln"), 
PrivilegeType.READ_DATA, false);
+
     Assert.assertEquals(
-        "Role{name='role', pathPrivilegeList=[root.ln : WRITE_DATA"
-            + " READ_SCHEMA_with_grant_option], systemPrivilegeSet=[]}",
+        "Role{name='role', pathPrivilegeList=[root.ln : "
+            + "READ_DATA READ_SCHEMA_with_grant_option], 
systemPrivilegeSet=[], "
+            + "AnyScopePrivilegeMap=[], objectPrivilegeSet={}}",
         role.toString());
     Role role1 = new Role("role1");
     role1.deserialize(role.serialize());
     Assert.assertEquals(
         "Role{name='role', pathPrivilegeList=[root.ln : "
-            + "WRITE_DATA READ_SCHEMA_with_grant_option], 
systemPrivilegeSet=[]}",
+            + "READ_DATA READ_SCHEMA_with_grant_option], 
systemPrivilegeSet=[], "
+            + "AnyScopePrivilegeMap=[], objectPrivilegeSet={}}",
         role1.toString());
 
     Role admin = new Role("root");
     PartialPath rootPath = new PartialPath(IoTDBConstant.PATH_ROOT + ".**");
     PathPrivilege pathPri = new PathPrivilege(rootPath);
+    DatabasePrivilege databasePrivilege = new DatabasePrivilege("testDB");
+    TablePrivilege tablePrivilege = new TablePrivilege("testTable");
+    databasePrivilege.getTablePrivilegeMap().put("testTable", tablePrivilege);
     for (PrivilegeType item : PrivilegeType.values()) {
-      if (!item.isPathRelevant()) {
-        admin.getSysPrivilege().add(item.ordinal());
-        admin.getSysPriGrantOpt().add(item.ordinal());
-      } else {
-        pathPri.grantPrivilege(item.ordinal(), true);
+      if (item.isSystemPrivilege()) {
+        admin.getSysPrivilege().add(item);
+        admin.getSysPriGrantOpt().add(item);
+      } else if (item.isPathPrivilege()) {
+        pathPri.grantPrivilege(item, true);
+      } else if (item.isRelationalPrivilege()) {
+        databasePrivilege.grantDBPrivilege(item);
+        databasePrivilege.grantDBGrantOption(item);
+        databasePrivilege.grantTablePrivilege("testTable", item);

Review Comment:
   Better add grant option for admin



-- 
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]

Reply via email to