FANNG1 commented on code in PR #6714: URL: https://github.com/apache/gravitino/pull/6714#discussion_r2005378002
########## core/src/main/java/org/apache/gravitino/listener/api/event/AccessControlEventDispatcher.java: ########## @@ -0,0 +1,366 @@ +/* + * 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.listener.api.event; + +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.apache.gravitino.MetadataObject; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.authorization.AccessControlDispatcher; +import org.apache.gravitino.authorization.Group; +import org.apache.gravitino.authorization.Privilege; +import org.apache.gravitino.authorization.Role; +import org.apache.gravitino.authorization.SecurableObject; +import org.apache.gravitino.authorization.User; +import org.apache.gravitino.exceptions.GroupAlreadyExistsException; +import org.apache.gravitino.exceptions.IllegalRoleException; +import org.apache.gravitino.exceptions.NoSuchGroupException; +import org.apache.gravitino.exceptions.NoSuchMetadataObjectException; +import org.apache.gravitino.exceptions.NoSuchMetalakeException; +import org.apache.gravitino.exceptions.NoSuchRoleException; +import org.apache.gravitino.exceptions.NoSuchUserException; +import org.apache.gravitino.exceptions.RoleAlreadyExistsException; +import org.apache.gravitino.exceptions.UserAlreadyExistsException; +import org.apache.gravitino.listener.EventBus; +import org.apache.gravitino.listener.api.info.UserInfo; +import org.apache.gravitino.utils.PrincipalUtils; + +/** + * An implementation of the {@link AccessControlDispatcher} interface that dispatches events to the + * specified event bus. + */ +public class AccessControlEventDispatcher implements AccessControlDispatcher { + private final EventBus eventBus; + private final AccessControlDispatcher dispatcher; + + /** + * Construct a new {@link AccessControlEventDispatcher} instance with the specified event bus and + * access control dispatcher. + * + * @param eventBus The EventBus to which events will be dispatched. + * @param dispatcher The underlying {@link AccessControlDispatcher} that will perform the actual + * access control operations. + */ + public AccessControlEventDispatcher(EventBus eventBus, AccessControlDispatcher dispatcher) { + this.eventBus = eventBus; + this.dispatcher = dispatcher; + } + + /** {@inheritDoc} */ + @Override + public User addUser(String metalake, String user) + throws UserAlreadyExistsException, NoSuchMetalakeException { + String initiator = PrincipalUtils.getCurrentUserName(); + UserInfo userInfo = new UserInfo(user); + + eventBus.dispatchEvent(new AddUserPreEvent(initiator, NameIdentifier.of(metalake), userInfo)); + try { + // TODO add Event + return dispatcher.addUser(metalake, user); + } catch (Exception e) { + // TODO: add failure event + throw e; + } + } + + /** {@inheritDoc} */ + @Override + public boolean removeUser(String metalake, String user) throws NoSuchMetalakeException { + String initiator = PrincipalUtils.getCurrentUserName(); + UserInfo userInfo = new UserInfo(user); + + eventBus.dispatchEvent( + new RemoveUserPreEvent(initiator, NameIdentifier.of(metalake), userInfo)); + try { + // TODO: add Event + return dispatcher.removeUser(metalake, user); + } catch (Exception e) { + // TODO: add failure event + throw e; + } + } + + /** {@inheritDoc} */ + @Override + public User getUser(String metalake, String user) + throws NoSuchUserException, NoSuchMetalakeException { + String initiator = PrincipalUtils.getCurrentUserName(); + UserInfo userInfo = new UserInfo(user); + + eventBus.dispatchEvent(new GetUserPreEvent(initiator, NameIdentifier.of(metalake), userInfo)); + try { + return dispatcher.getUser(metalake, user); + } catch (Exception e) { + // TODO: add failure event + throw e; + } + } + + /** {@inheritDoc} */ + @Override + public User[] listUsers(String metalake) throws NoSuchMetalakeException { + String initiator = PrincipalUtils.getCurrentUserName(); + + eventBus.dispatchEvent(new ListUserPreEvent(initiator, NameIdentifier.of(metalake))); Review Comment: Could you use `ListUsersPreEvent` for `listUsers` operation? ########## core/src/main/java/org/apache/gravitino/listener/api/info/UserInfo.java: ########## @@ -0,0 +1,80 @@ +/* + * 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.listener.api.info; + +import java.util.List; +import java.util.Optional; +import org.apache.gravitino.authorization.User; + +/** Provides read-only access to user information for event listeners. */ +public class UserInfo { + private final String name; + private Optional<List<String>> roles; Review Comment: any reason to add roles to `UserInfo`? -- 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]
