yuqi1129 commented on code in PR #11209: URL: https://github.com/apache/gravitino/pull/11209#discussion_r3297023076
########## plugins/idp-basic/src/main/java/org/apache/gravitino/idp/IdpUserGroupManager.java: ########## @@ -0,0 +1,243 @@ +/* + * 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.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.exception.AlreadyExistsException; +import org.apache.gravitino.idp.exception.NotFoundException; +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; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * 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 Logger LOG = LoggerFactory.getLogger(IdpUserGroupManager.class); + + private static final IdpUserMetaService USER_SERVICE = IdpUserMetaService.getInstance(); + private static final IdpGroupMetaService GROUP_SERVICE = IdpGroupMetaService.getInstance(); + + 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(config, idGenerator, PasswordHasherFactory.create()); + } + + IdpUserGroupManager(Config config, IdGenerator idGenerator, PasswordHasher passwordHasher) { + IdpRelationalStorage.initialize(config); + this.idGenerator = idGenerator; + this.passwordHasher = passwordHasher; + 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) { + if (userExists(username)) { + AlreadyExistsException exception = + new AlreadyExistsException("IdP user %s already exists", username); + LOG.warn("IdP user {} already exists", username, exception); Review Comment: It's not so proper to use the warning level if you throw an exception in L87. -- 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]
