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


##########
iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/auth/role/LocalFileRoleAccessor.java:
##########
@@ -73,67 +76,146 @@
  * All user/role file store in config node's user/role folder. when our system 
start, we load all
  * user/role from folder. If we did some alter query, we just store raft log.
  */
-public class LocalFileRoleAccessor implements IRoleAccessor {
+public class LocalFileRoleAccessor implements IEntityAccessor {
   private static final Logger LOGGER = 
LoggerFactory.getLogger(LocalFileRoleAccessor.class);
-  private static final String TEMP_SUFFIX = ".temp";
-  private static final String STRING_ENCODING = "utf-8";
-  private static final String ROLE_SNAPSHOT_FILE_NAME = "system" + 
File.separator + "roles";
+  protected static final String TEMP_SUFFIX = ".temp";
+  protected static final String STRING_ENCODING = "utf-8";
+  protected final String entityDirPath;
 
-  private final String roleDirPath;
+  // It might be a good idea to use a Version number to control upgrade 
compatibility.
+  // Now it's version 1
+  protected static final int VERSION = 1;
 
   /**
    * Reused buffer for primitive types encoding/decoding, which aim to reduce 
memory fragments. Use
    * ThreadLocal for thread safety.
    */
-  private final ThreadLocal<ByteBuffer> encodingBufferLocal = new 
ThreadLocal<>();
+  protected final ThreadLocal<ByteBuffer> encodingBufferLocal = new 
ThreadLocal<>();
 
-  private final ThreadLocal<byte[]> strBufferLocal = new ThreadLocal<>();
+  protected final ThreadLocal<byte[]> strBufferLocal = new ThreadLocal<>();
 
   public LocalFileRoleAccessor(String roleDirPath) {
-    this.roleDirPath = roleDirPath;
+    this.entityDirPath = roleDirPath;
   }
 
-  /**
-   * @return role struct
-   * @throws IOException
-   */
-  @Override
-  public Role loadRole(String rolename) throws IOException {
-    File roleProfile =
+  protected String getEntitySnapshotFileName() {
+    return "system" + File.separator + "roles";
+  }
+
+  protected void saveEntityVersion(BufferedOutputStream outputStream) throws 
IOException {
+    IOUtils.writeInt(outputStream, VERSION, encodingBufferLocal);
+  }
+
+  protected void saveEntityName(BufferedOutputStream outputStream, Role role) 
throws IOException {
+    IOUtils.writeString(outputStream, role.getName(), STRING_ENCODING, 
encodingBufferLocal);
+  }
+
+  protected void savePrivileges(BufferedOutputStream outputStream, Role role) 
throws IOException {
+    IOUtils.writeInt(outputStream, role.getAllSysPrivileges(), 
encodingBufferLocal);
+    int privilegeNum = role.getPathPrivilegeList().size();
+    IOUtils.writeInt(outputStream, privilegeNum, encodingBufferLocal);
+    for (int i = 0; i < privilegeNum; i++) {
+      PathPrivilege pathPrivilege = role.getPathPrivilegeList().get(i);
+      IOUtils.writePathPrivilege(outputStream, pathPrivilege, STRING_ENCODING, 
encodingBufferLocal);
+    }
+    IOUtils.writeInt(outputStream, role.getAnyScopePrivileges(), 
encodingBufferLocal);
+    privilegeNum = role.getDBScopePrivilegeMap().size();
+    IOUtils.writeInt(outputStream, privilegeNum, encodingBufferLocal);
+    for (Map.Entry<String, DatabasePrivilege> objectPrivilegeMap :
+        role.getDBScopePrivilegeMap().entrySet()) {
+      IOUtils.writeObjectPrivilege(
+          outputStream, objectPrivilegeMap.getValue(), STRING_ENCODING, 
encodingBufferLocal);
+    }
+  }
+
+  protected void loadPrivileges(DataInputStream dataInputStream, Role role)
+      throws IOException, IllegalPathException {
+    role.setSysPrivilegesWithMask(dataInputStream.readInt());
+    int num = ReadWriteIOUtils.readInt(dataInputStream);
+    List<PathPrivilege> pathPrivilegeList = new ArrayList<>();
+    for (int i = 0; i < num; i++) {
+      pathPrivilegeList.add(
+          IOUtils.readPathPrivilege(dataInputStream, STRING_ENCODING, 
strBufferLocal));
+    }
+    role.setPrivilegeList(pathPrivilegeList);
+    
role.setAnyScopePrivilegeSetWithMask(ReadWriteIOUtils.readInt(dataInputStream));
+    Map<String, DatabasePrivilege> objectPrivilegeMap = new HashMap<>();
+    num = ReadWriteIOUtils.readInt(dataInputStream);
+    for (int i = 0; i < num; i++) {
+      DatabasePrivilege databasePrivilege =
+          IOUtils.readRelationalPrivilege(dataInputStream, STRING_ENCODING, 
strBufferLocal);
+      objectPrivilegeMap.put(databasePrivilege.getDatabaseName(), 
databasePrivilege);
+    }
+    role.setObjectPrivilegeMap(objectPrivilegeMap);
+  }
+
+  protected void saveRoles(Role role) throws IOException {
+    // Just used in LocalFileUserAccessor.java.
+    // Do nothing.
+  }
+
+  protected File checkFileAvailable(String entityName, String suffix) {

Review Comment:
   Better check @Override in the whole file...



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