roryqi commented on code in PR #11209:
URL: https://github.com/apache/gravitino/pull/11209#discussion_r3298981309


##########
plugins/idp-basic/src/main/java/org/apache/gravitino/idp/storage/utils/IdpSQLExceptionUtils.java:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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.gravitino.idp.storage.utils;
+
+import java.sql.SQLException;
+import org.apache.gravitino.idp.exception.AlreadyExistsException;
+
+/** Utilities for translating JDBC exceptions in built-in IdP storage. */
+public final class IdpSQLExceptionUtils {
+
+  /** MySQL duplicate entry error code. */
+  private static final int MYSQL_DUPLICATE_ENTRY_ERROR_CODE = 1062;
+
+  /** H2 duplicate entry error code. */
+  private static final int H2_DUPLICATE_ENTRY_ERROR_CODE = 23505;
+
+  /** PostgreSQL duplicate entry SQL state. */
+  private static final String POSTGRESQL_DUPLICATE_ENTRY_SQL_STATE = "23505";
+
+  private IdpSQLExceptionUtils() {}
+
+  /**
+   * Converts duplicate-key SQL failures into {@link AlreadyExistsException}.
+   *
+   * @param re The runtime exception thrown by JDBC/MyBatis.
+   * @param resourceType The resource type in the error message, for example 
{@code user}.
+   * @param name The resource name.
+   */
+  public static void checkDuplicateEntry(RuntimeException re, String 
resourceType, String name) {
+    SQLException sqlException = extractSQLException(re);
+    if (sqlException != null && isDuplicateEntry(sqlException)) {
+      throw new AlreadyExistsException("IdP %s %s already exists", 
resourceType, name);
+    }
+  }
+
+  private static SQLException extractSQLException(Throwable throwable) {
+    Throwable current = throwable;
+    while (current != null) {
+      if (current instanceof SQLException) {
+        return (SQLException) current;
+      }
+      current = current.getCause();
+    }
+    return null;
+  }
+
+  private static boolean isDuplicateEntry(SQLException sqlException) {
+    int errorCode = sqlException.getErrorCode();
+    if (errorCode == MYSQL_DUPLICATE_ENTRY_ERROR_CODE

Review Comment:
   Could use the correct and unique error code?



##########
plugins/idp-basic/src/main/java/org/apache/gravitino/idp/IdpUserGroupManager.java:
##########
@@ -0,0 +1,194 @@
+/*
+ * 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.gravitino.idp;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
+import org.apache.gravitino.Config;
+import org.apache.gravitino.idp.basic.password.PasswordHasher;
+import org.apache.gravitino.idp.basic.password.PasswordHasherFactory;
+import org.apache.gravitino.idp.model.IdpGroup;
+import org.apache.gravitino.idp.model.IdpUser;
+import org.apache.gravitino.idp.storage.po.IdpGroupPO;
+import org.apache.gravitino.idp.storage.po.IdpUserPO;
+import org.apache.gravitino.idp.storage.relational.IdpGarbageCollector;
+import org.apache.gravitino.idp.storage.relational.IdpRelationalStorage;
+import org.apache.gravitino.idp.storage.service.IdpGroupMetaService;
+import org.apache.gravitino.idp.storage.service.IdpUserMetaService;
+import org.apache.gravitino.storage.IdGenerator;
+import org.apache.gravitino.storage.relational.utils.POConverters;
+
+/**
+ * Manager for built-in IdP users and groups. It mirrors {@link
+ * org.apache.gravitino.authorization.UserGroupManager} but operates on global 
IdP metadata.
+ */
+public class IdpUserGroupManager implements Closeable {
+
+  private static final IdpUserMetaService USER_SERVICE = 
IdpUserMetaService.getInstance();
+  private static final IdpGroupMetaService GROUP_SERVICE = 
IdpGroupMetaService.getInstance();
+
+  private final IdpRelationalStorage relationalStorage;
+  private final IdGenerator idGenerator;
+  private final PasswordHasher passwordHasher;
+  private final IdpGarbageCollector garbageCollector;
+
+  /**
+   * Creates a built-in IdP user and group manager.
+   *
+   * @param config The server configuration.
+   * @param idGenerator The id generator.
+   */
+  public IdpUserGroupManager(Config config, IdGenerator idGenerator) {
+    this.relationalStorage = new IdpRelationalStorage(config);
+    this.idGenerator = idGenerator;
+    this.passwordHasher = PasswordHasherFactory.create();
+    this.garbageCollector = new IdpGarbageCollector(config);
+    garbageCollector.start();
+  }
+
+  /**
+   * Adds a built-in IdP user.
+   *
+   * @param username The username.
+   * @param password The plaintext password.
+   * @return The created built-in IdP user.
+   */
+  public IdpUser addUser(String username, String password) {
+    USER_SERVICE.insertIdpUser(newUserPO(username, 
passwordHasher.hash(password)));
+    return new IdpUser(username, Collections.emptyList());
+  }
+
+  /**
+   * Removes a built-in IdP user.
+   *
+   * @param username The username.
+   * @return True if the user was removed, false if it did not exist.
+   */
+  public boolean removeUser(String username) {
+    return USER_SERVICE.deleteIdpUser(username);
+  }
+
+  /**
+   * Gets a built-in IdP user.
+   *
+   * @param username The username.
+   * @return The built-in IdP user.
+   */
+  public IdpUser getUser(String username) {
+    IdpUserPO userPO = USER_SERVICE.getIdpUserByUsername(username);
+    return new IdpUser(userPO.getUsername(), 
USER_SERVICE.listGroupNamesByUsername(username));
+  }
+
+  /**
+   * Changes the password for a built-in IdP user.
+   *
+   * @param username The username.
+   * @param password The new plaintext password.
+   * @return True if the password was updated, false if the user did not exist.
+   */
+  public boolean changePassword(String username, String password) {
+    return USER_SERVICE.updateIdpUserPassword(username, 
passwordHasher.hash(password));
+  }
+
+  /**
+   * Adds a built-in IdP group.
+   *
+   * @param groupName The group name.
+   * @return The created built-in IdP group.
+   */
+  public IdpGroup addGroup(String groupName) {
+    GROUP_SERVICE.insertIdpGroup(newGroupPO(groupName));
+    return new IdpGroup(groupName, Collections.emptyList());
+  }
+
+  /**
+   * Removes a built-in IdP group.
+   *
+   * @param groupName The group name.
+   * @param force Whether to force delete a non-empty group.
+   * @return True if the group was removed, false if it did not exist.
+   */
+  public boolean removeGroup(String groupName, boolean force) {
+    return GROUP_SERVICE.deleteIdpGroup(groupName, force);
+  }
+
+  /**
+   * Gets a built-in IdP group.
+   *
+   * @param groupName The group name.
+   * @return The built-in IdP group.
+   */
+  public IdpGroup getGroup(String groupName) {
+    IdpGroupPO groupPO = GROUP_SERVICE.getIdpGroupByName(groupName);
+    return new IdpGroup(groupPO.getGroupName(), 
GROUP_SERVICE.listUsernamesByGroupName(groupName));
+  }
+
+  /**
+   * Adds users to a built-in IdP group.
+   *
+   * @param groupName The group name.
+   * @param usernames The usernames to add.
+   * @return The updated built-in IdP group.
+   */
+  public IdpGroup addUsersToGroup(String groupName, List<String> usernames) {

Review Comment:
   Why do u use two methods here?
   In the upper layer, you should 
   ```
   {
      List<String> additionals;
      List<String> removals;
   }
   ```



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