roryqi commented on code in PR #11209: URL: https://github.com/apache/gravitino/pull/11209#discussion_r3298383587
########## plugins/idp-basic/src/main/java/org/apache/gravitino/idp/IdpUserGroupManager.java: ########## @@ -0,0 +1,230 @@ +/* + * 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; + +/** + * 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) { + if (userExists(username)) { + throw new AlreadyExistsException("IdP user %s already exists", username); + } + + USER_SERVICE.insertIdpUser(newUserPO(username, passwordHasher.hash(password))); Review Comment: Could u use sql error code to judge whether user exists? ########## plugins/idp-basic/src/main/java/org/apache/gravitino/idp/IdpUserGroupManager.java: ########## @@ -0,0 +1,230 @@ +/* + * 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; + +/** + * 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) { + if (userExists(username)) { + throw new AlreadyExistsException("IdP user %s already exists", username); + } + + USER_SERVICE.insertIdpUser(newUserPO(username, passwordHasher.hash(password))); + return getUser(username); + } + + /** + * 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) { + if (!userExists(username)) { + return false; + } + return USER_SERVICE.deleteIdpUser(username); Review Comment: Could u use return value of deleting sql to judge the user exists? ########## plugins/idp-basic/src/main/java/org/apache/gravitino/idp/IdpUserGroupManager.java: ########## @@ -0,0 +1,230 @@ +/* + * 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; + +/** + * 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) { + if (userExists(username)) { + throw new AlreadyExistsException("IdP user %s already exists", username); + } + + USER_SERVICE.insertIdpUser(newUserPO(username, passwordHasher.hash(password))); + return getUser(username); + } + + /** + * 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) { + if (!userExists(username)) { + return false; + } + 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 The updated built-in IdP user. + */ + public IdpUser changePassword(String username, String password) { Review Comment: Why do you need to return IdpUser? -- 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]
