Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#12335] feat(authz): Add user by-id listener events and audit mappings (#12336)

MaSai9f5affdcb4a0
diff --git a/core/src/main/java/org/apache/gravitino/audit/AuditLog.java b/core/src/main/java/org/apache/gravitino/audit/AuditLog.java
index 6a91a41b24..7ebc32012e 100644
--- a/core/src/main/java/org/apache/gravitino/audit/AuditLog.java
+++ b/core/src/main/java/org/apache/gravitino/audit/AuditLog.java
@@ -384,6 +384,12 @@ public interface AuditLog {
GET_USER_BY_EXTERNAL_ID,
+ GET_USER_BY_ID,
+
+ REMOVE_USER_BY_ID,
+
+ ALTER_USER,
+
ENABLE_USER,
DISABLE_USER,
diff --git a/core/src/main/java/org/apache/gravitino/audit/v2/CompatibilityUtils.java b/core/src/main/java/org/apache/gravitino/audit/v2/CompatibilityUtils.java
index 3e254400ef..5814e5ecbc 100644
--- a/core/src/main/java/org/apache/gravitino/audit/v2/CompatibilityUtils.java
+++ b/core/src/main/java/org/apache/gravitino/audit/v2/CompatibilityUtils.java
@@ -123,6 +123,9 @@ public class CompatibilityUtils {
.put(OperationType.REMOVE_USER_BY_EXTERNAL_ID, Operation.REMOVE_USER_BY_EXTERNAL_ID)
.put(OperationType.GET_USER, Operation.GET_USER)
.put(OperationType.GET_USER_BY_EXTERNAL_ID, Operation.GET_USER_BY_EXTERNAL_ID)
+ .put(OperationType.GET_USER_BY_ID, Operation.GET_USER_BY_ID)
+ .put(OperationType.REMOVE_USER_BY_ID, Operation.REMOVE_USER_BY_ID)
+ .put(OperationType.ALTER_USER, Operation.ALTER_USER)
.put(OperationType.ENABLE_USER, Operation.ENABLE_USER)
.put(OperationType.DISABLE_USER, Operation.DISABLE_USER)
.put(OperationType.LIST_USERS, Operation.LIST_USERS)
diff --git a/core/src/main/java/org/apache/gravitino/listener/AccessControlEventDispatcher.java b/core/src/main/java/org/apache/gravitino/listener/AccessControlEventDispatcher.java
index c52406a4e6..db94a7e989 100644
--- a/core/src/main/java/org/apache/gravitino/listener/AccessControlEventDispatcher.java
+++ b/core/src/main/java/org/apache/gravitino/listener/AccessControlEventDispatcher.java
@@ -49,6 +49,9 @@ import org.apache.gravitino.listener.api.event.AddUserPreEvent;
import org.apache.gravitino.listener.api.event.AlterGroupEvent;
import org.apache.gravitino.listener.api.event.AlterGroupFailureEvent;
import org.apache.gravitino.listener.api.event.AlterGroupPreEvent;
+import org.apache.gravitino.listener.api.event.AlterUserEvent;
+import org.apache.gravitino.listener.api.event.AlterUserFailureEvent;
+import org.apache.gravitino.listener.api.event.AlterUserPreEvent;
import org.apache.gravitino.listener.api.event.CreateRoleEvent;
import org.apache.gravitino.listener.api.event.CreateRoleFailureEvent;
import org.apache.gravitino.listener.api.event.CreateRolePreEvent;
@@ -70,6 +73,9 @@ import org.apache.gravitino.listener.api.event.GetRolePreEvent;
import org.apache.gravitino.listener.api.event.GetUserByExternalIdEvent;
import org.apache.gravitino.listener.api.event.GetUserByExternalIdFailureEvent;
import org.apache.gravitino.listener.api.event.GetUserByExternalIdPreEvent;
+import org.apache.gravitino.listener.api.event.GetUserByIdEvent;
+import org.apache.gravitino.listener.api.event.GetUserByIdFailureEvent;
+import org.apache.gravitino.listener.api.event.GetUserByIdPreEvent;
import org.apache.gravitino.listener.api.event.GetUserEvent;
import org.apache.gravitino.listener.api.event.GetUserFailureEvent;
import org.apache.gravitino.listener.api.event.GetUserPreEvent;
@@ -112,6 +118,9 @@ import org.apache.gravitino.listener.api.event.RemoveGroupPreEvent;
import org.apache.gravitino.listener.api.event.RemoveUserByExternalIdEvent;
import org.apache.gravitino.listener.api.event.RemoveUserByExternalIdFailureEvent;
import org.apache.gravitino.listener.api.event.RemoveUserByExternalIdPreEvent;
+import org.apache.gravitino.listener.api.event.RemoveUserByIdEvent;
+import org.apache.gravitino.listener.api.event.RemoveUserByIdFailureEvent;
+import org.apache.gravitino.listener.api.event.RemoveUserByIdPreEvent;
import org.apache.gravitino.listener.api.event.RemoveUserEvent;
import org.apache.gravitino.listener.api.event.RemoveUserFailureEvent;
import org.apache.gravitino.listener.api.event.RemoveUserPreEvent;
@@ -265,20 +274,54 @@ public class AccessControlEventDispatcher implements AccessControlDispatcher {
@Override
public User getUserById(String metalake, long userId)
throws NoSuchUserException, NoSuchMetalakeException {
- return dispatcher.getUserById(metalake, userId);
+ String initiator = PrincipalUtils.getCurrentUserName();
+
+ eventBus.dispatchEvent(new GetUserByIdPreEvent(initiator, metalake, userId));
+ try {
+ User userObject = dispatcher.getUserById(metalake, userId);
+ eventBus.dispatchEvent(new GetUserByIdEvent(initiator, metalake, new UserInfo(userObject)));
+
+ return userObject;
+ } catch (Exception e) {
+ eventBus.dispatchEvent(new GetUserByIdFailureEvent(initiator, metalake, e, userId));
+ throw e;
+ }
}
/** {@inheritDoc} */
@Override
public boolean removeUserById(String metalake, long userId) throws NoSuchMetalakeException {
- return dispatcher.removeUserById(metalake, userId);
+ String initiator = PrincipalUtils.getCurrentUserName();
+
+ eventBus.dispatchEvent(new RemoveUserByIdPreEvent(initiator, metalake, userId));
+ try {
+ boolean isExists = dispatcher.removeUserById(metalake, userId);
+ eventBus.dispatchEvent(new RemoveUserByIdEvent(initiator, metalake, userId, isExists));
+
+ return isExists;
+ } catch (Exception e) {
+ eventBus.dispatchEvent(new RemoveUserByIdFailureEvent(initiator, metalake, e, userId));
+ throw e;
+ }
}
/** {@inheritDoc} */
@Override
public User alterUserById(String metalake, long userId, UserChange... changes)
throws NoSuchUserException, NoSuchMetalakeException {
- return dispatcher.alterUserById(metalake, userId, changes);
+ String initiator = PrincipalUtils.getCurrentUserName();
+
+ eventBus.dispatchEvent(new AlterUserPreEvent(initiator, metalake, userId, changes));
+ try {
+ User userObject = dispatcher.alterUserById(metalake, userId, changes);
+ eventBus.dispatchEvent(
+ new AlterUserEvent(initiator, metalake, changes, new UserInfo(userObject)));
+
+ return userObject;
+ } catch (Exception e) {
+ eventBus.dispatchEvent(new AlterUserFailureEvent(initiator, metalake, userId, changes, e));
+ throw e;
+ }
}
/** {@inheritDoc} */
diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/AlterUserEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/AlterUserEvent.java
new file mode 100644
index 0000000000..bfaa83d612
--- /dev/null
+++ b/core/src/main/java/org/apache/gravitino/listener/api/event/AlterUserEvent.java
@@ -0,0 +1,72 @@
+/*
+ * 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 javax.annotation.Nullable;
+import org.apache.gravitino.annotation.DeveloperApi;
+import org.apache.gravitino.authorization.AuthorizationUtils;
+import org.apache.gravitino.authorization.UserChange;
+import org.apache.gravitino.listener.api.info.UserInfo;
+
+/** Represents an event triggered upon the successful alteration of a user. */
+@DeveloperApi
+public final class AlterUserEvent extends UserEvent {
+ private final UserInfo updatedUserInfo;
+ private final UserChange[] userChanges;
+
+ /**
+ * Creates a new {@link AlterUserEvent}.
+ *
+ * @param initiator The user who initiated the request.
+ * @param metalake The metalake name.
+ * @param userChanges The changes applied to the user.
+ * @param updatedUserInfo The post-alteration state of the user.
+ */
+ public AlterUserEvent(
+ String initiator, String metalake, UserChange[] userChanges, UserInfo updatedUserInfo) {
+ super(initiator, AuthorizationUtils.ofUserId(metalake, updatedUserInfo.id()));
+ this.userChanges = userChanges != null ? userChanges.clone() : null;
+ this.updatedUserInfo = updatedUserInfo;
+ }
+
+ /**
+ * Returns the final state of the user after successful alteration.
+ *
+ * @return The updated user information.
+ */
+ public UserInfo updatedUserInfo() {
+ return updatedUserInfo;
+ }
+
+ /**
+ * Returns the specific changes that were made to the user.
+ *
+ * @return An array of {@link UserChange}, or null.
+ */
+ @Nullable
+ public UserChange[] changes() {
+ return userChanges;
+ }
+
+ @Override
+ public OperationType operationType() {
+ return OperationType.ALTER_USER;
+ }
+}
diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/AlterUserFailureEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/AlterUserFailureEvent.java
new file mode 100644
index 0000000000..a7ff8cc382
--- /dev/null
+++ b/core/src/main/java/org/apache/gravitino/listener/api/event/AlterUserFailureEvent.java
@@ -0,0 +1,70 @@
+/*
+ * 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 org.apache.gravitino.annotation.DeveloperApi;
+import org.apache.gravitino.authorization.AuthorizationUtils;
+import org.apache.gravitino.authorization.UserChange;
+
+/** Represents an event triggered when an attempt to alter a user fails due to an exception. */
+@DeveloperApi
+public class AlterUserFailureEvent extends UserFailureEvent {
+ private final long userId;
+ private final UserChange[] changes;
+
+ /**
+ * Creates a new {@link AlterUserFailureEvent}.
+ *
+ * @param initiator The user who initiated the request.
+ * @param metalake The metalake name.
+ * @param userId The Gravitino-assigned id of the user.
+ * @param changes The changes attempted to be made to the user.
+ * @param exception The exception that caused the failure.
+ */
+ public AlterUserFailureEvent(
+ String initiator, String metalake, long userId, UserChange[] changes, Exception exception) {
+ super(initiator, AuthorizationUtils.ofUserId(metalake, userId), exception);
+ this.userId = userId;
+ this.changes = changes;
+ }
+
+ /**
+ * Returns the Gravitino-assigned id of the user.
+ *
+ * @return The user id.
+ */
+ public long userId() {
+ return userId;
+ }
+
+ /**
+ * Returns the changes attempted to be made to the user.
+ *
+ * @return An array of {@link UserChange}.
+ */
+ public UserChange[] changes() {
+ return changes;
+ }
+
+ @Override
+ public OperationType operationType() {
+ return OperationType.ALTER_USER;
+ }
+}
diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/AlterUserPreEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/AlterUserPreEvent.java
new file mode 100644
index 0000000000..17cc51a2d2
--- /dev/null
+++ b/core/src/main/java/org/apache/gravitino/listener/api/event/AlterUserPreEvent.java
@@ -0,0 +1,68 @@
+/*
+ * 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 org.apache.gravitino.annotation.DeveloperApi;
+import org.apache.gravitino.authorization.AuthorizationUtils;
+import org.apache.gravitino.authorization.UserChange;
+
+/** Represents an event triggered before altering a user by Gravitino-assigned id. */
+@DeveloperApi
+public class AlterUserPreEvent extends UserPreEvent {
+ private final long userId;
+ private final UserChange[] changes;
+
+ /**
+ * Creates a new {@link AlterUserPreEvent}.
+ *
+ * @param initiator The user who initiated the request.
+ * @param metalake The metalake name.
+ * @param userId The Gravitino-assigned id of the user.
+ * @param changes The changes being applied to the user.
+ */
+ public AlterUserPreEvent(String initiator, String metalake, long userId, UserChange[] changes) {
+ super(initiator, AuthorizationUtils.ofUserId(metalake, userId));
+ this.userId = userId;
+ this.changes = changes;
+ }
+
+ /**
+ * Returns the Gravitino-assigned id of the user being altered.
+ *
+ * @return The user id.
+ */
+ public long userId() {
+ return userId;
+ }
+
+ /**
+ * Returns the changes being applied to the user.
+ *
+ * @return An array of {@link UserChange}.
+ */
+ public UserChange[] changes() {
+ return changes;
+ }
+
+ @Override
+ public OperationType operationType() {
+ return OperationType.ALTER_USER;
+ }
+}
diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/GetUserByIdEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/GetUserByIdEvent.java
new file mode 100644
index 0000000000..6f3f8e8b84
--- /dev/null
+++ b/core/src/main/java/org/apache/gravitino/listener/api/event/GetUserByIdEvent.java
@@ -0,0 +1,56 @@
+/*
+ * 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 org.apache.gravitino.annotation.DeveloperApi;
+import org.apache.gravitino.authorization.AuthorizationUtils;
+import org.apache.gravitino.listener.api.info.UserInfo;
+
+/** Represents an event triggered after successfully retrieving a user by Gravitino-assigned id. */
+@DeveloperApi
+public class GetUserByIdEvent extends UserEvent {
+ private final UserInfo loadedUserInfo;
+
+ /**
+ * Creates a new {@link GetUserByIdEvent}.
+ *
+ * @param initiator The user who initiated the request.
+ * @param metalake The metalake name.
+ * @param loadedUserInfo The retrieved user information.
+ */
+ public GetUserByIdEvent(String initiator, String metalake, UserInfo loadedUserInfo) {
+ super(initiator, AuthorizationUtils.ofUserId(metalake, loadedUserInfo.id()));
+ this.loadedUserInfo = loadedUserInfo;
+ }
+
+ /**
+ * Returns the retrieved user information.
+ *
+ * @return The user information.
+ */
+ public UserInfo loadedUserInfo() {
+ return loadedUserInfo;
+ }
+
+ @Override
+ public OperationType operationType() {
+ return OperationType.GET_USER_BY_ID;
+ }
+}
diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/GetUserByIdFailureEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/GetUserByIdFailureEvent.java
new file mode 100644
index 0000000000..5080b61a46
--- /dev/null
+++ b/core/src/main/java/org/apache/gravitino/listener/api/event/GetUserByIdFailureEvent.java
@@ -0,0 +1,57 @@
+/*
+ * 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 org.apache.gravitino.annotation.DeveloperApi;
+import org.apache.gravitino.authorization.AuthorizationUtils;
+
+/** Represents an event triggered when retrieving a user by Gravitino-assigned id fails. */
+@DeveloperApi
+public class GetUserByIdFailureEvent extends UserFailureEvent {
+ private final long userId;
+
+ /**
+ * Creates a new {@link GetUserByIdFailureEvent}.
+ *
+ * @param initiator The user who initiated the request.
+ * @param metalake The metalake name.
+ * @param exception The exception that caused the failure.
+ * @param userId The Gravitino-assigned id of the user.
+ */
+ public GetUserByIdFailureEvent(
+ String initiator, String metalake, Exception exception, long userId) {
+ super(initiator, AuthorizationUtils.ofUserId(metalake, userId), exception);
+ this.userId = userId;
+ }
+
+ /**
+ * Returns the Gravitino-assigned id of the user.
+ *
+ * @return The user id.
+ */
+ public long userId() {
+ return userId;
+ }
+
+ @Override
+ public OperationType operationType() {
+ return OperationType.GET_USER_BY_ID;
+ }
+}
diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/GetUserByIdPreEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/GetUserByIdPreEvent.java
new file mode 100644
index 0000000000..dc19ec144b
--- /dev/null
+++ b/core/src/main/java/org/apache/gravitino/listener/api/event/GetUserByIdPreEvent.java
@@ -0,0 +1,55 @@
+/*
+ * 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 org.apache.gravitino.annotation.DeveloperApi;
+import org.apache.gravitino.authorization.AuthorizationUtils;
+
+/** Represents an event triggered before retrieving a user by Gravitino-assigned id. */
+@DeveloperApi
+public class GetUserByIdPreEvent extends UserPreEvent {
+ private final long userId;
+
+ /**
+ * Creates a new {@link GetUserByIdPreEvent}.
+ *
+ * @param initiator The user who initiated the request.
+ * @param metalake The metalake name.
+ * @param userId The Gravitino-assigned id of the user.
+ */
+ public GetUserByIdPreEvent(String initiator, String metalake, long userId) {
+ super(initiator, AuthorizationUtils.ofUserId(metalake, userId));
+ this.userId = userId;
+ }
+
+ /**
+ * Returns the Gravitino-assigned id of the user being retrieved.
+ *
+ * @return The user id.
+ */
+ public long userId() {
+ return userId;
+ }
+
+ @Override
+ public OperationType operationType() {
+ return OperationType.GET_USER_BY_ID;
+ }
+}
diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/OperationType.java b/core/src/main/java/org/apache/gravitino/listener/api/event/OperationType.java
index f64b9b3b0f..c86c58e1b0 100644
--- a/core/src/main/java/org/apache/gravitino/listener/api/event/OperationType.java
+++ b/core/src/main/java/org/apache/gravitino/listener/api/event/OperationType.java
@@ -131,6 +131,9 @@ public enum OperationType {
GET_USER,
GET_USER_BY_EXTERNAL_ID,
REMOVE_USER_BY_EXTERNAL_ID,
+ GET_USER_BY_ID,
+ REMOVE_USER_BY_ID,
+ ALTER_USER,
ENABLE_USER,
DISABLE_USER,
LIST_USERS,
diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/RemoveUserByIdEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/RemoveUserByIdEvent.java
new file mode 100644
index 0000000000..404e63f904
--- /dev/null
+++ b/core/src/main/java/org/apache/gravitino/listener/api/event/RemoveUserByIdEvent.java
@@ -0,0 +1,67 @@
+/*
+ * 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 org.apache.gravitino.annotation.DeveloperApi;
+import org.apache.gravitino.authorization.AuthorizationUtils;
+
+/** Represents an event triggered after removing a user by Gravitino-assigned id. */
+@DeveloperApi
+public class RemoveUserByIdEvent extends UserEvent {
+ private final long userId;
+ private final boolean isExists;
+
+ /**
+ * Creates a new {@link RemoveUserByIdEvent}.
+ *
+ * @param initiator The user who initiated the request.
+ * @param metalake The metalake name.
+ * @param userId The Gravitino-assigned id of the user.
+ * @param isExists Whether the user existed and was removed.
+ */
+ public RemoveUserByIdEvent(String initiator, String metalake, long userId, boolean isExists) {
+ super(initiator, AuthorizationUtils.ofUserId(metalake, userId));
+ this.userId = userId;
+ this.isExists = isExists;
+ }
+
+ /**
+ * Returns the Gravitino-assigned id of the removed user.
+ *
+ * @return The user id.
+ */
+ public long userId() {
+ return userId;
+ }
+
+ /**
+ * Returns whether the user existed and was removed.
+ *
+ * @return {@code true} if the user was removed.
+ */
+ public boolean isExists() {
+ return isExists;
+ }
+
+ @Override
+ public OperationType operationType() {
+ return OperationType.REMOVE_USER_BY_ID;
+ }
+}
diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/RemoveUserByIdFailureEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/RemoveUserByIdFailureEvent.java
new file mode 100644
index 0000000000..5f86700c32
--- /dev/null
+++ b/core/src/main/java/org/apache/gravitino/listener/api/event/RemoveUserByIdFailureEvent.java
@@ -0,0 +1,57 @@
+/*
+ * 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 org.apache.gravitino.annotation.DeveloperApi;
+import org.apache.gravitino.authorization.AuthorizationUtils;
+
+/** Represents an event triggered when removing a user by Gravitino-assigned id fails. */
+@DeveloperApi
+public class RemoveUserByIdFailureEvent extends UserFailureEvent {
+ private final long userId;
+
+ /**
+ * Creates a new {@link RemoveUserByIdFailureEvent}.
+ *
+ * @param initiator The user who initiated the request.
+ * @param metalake The metalake name.
+ * @param exception The exception that caused the failure.
+ * @param userId The Gravitino-assigned id of the user.
+ */
+ public RemoveUserByIdFailureEvent(
+ String initiator, String metalake, Exception exception, long userId) {
+ super(initiator, AuthorizationUtils.ofUserId(metalake, userId), exception);
+ this.userId = userId;
+ }
+
+ /**
+ * Returns the Gravitino-assigned id of the user.
+ *
+ * @return The user id.
+ */
+ public long userId() {
+ return userId;
+ }
+
+ @Override
+ public OperationType operationType() {
+ return OperationType.REMOVE_USER_BY_ID;
+ }
+}
diff --git a/core/src/main/java/org/apache/gravitino/listener/api/event/RemoveUserByIdPreEvent.java b/core/src/main/java/org/apache/gravitino/listener/api/event/RemoveUserByIdPreEvent.java
new file mode 100644
index 0000000000..4696e555d5
--- /dev/null
+++ b/core/src/main/java/org/apache/gravitino/listener/api/event/RemoveUserByIdPreEvent.java
@@ -0,0 +1,55 @@
+/*
+ * 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 org.apache.gravitino.annotation.DeveloperApi;
+import org.apache.gravitino.authorization.AuthorizationUtils;
+
+/** Represents an event triggered before removing a user by Gravitino-assigned id. */
+@DeveloperApi
+public class RemoveUserByIdPreEvent extends UserPreEvent {
+ private final long userId;
+
+ /**
+ * Creates a new {@link RemoveUserByIdPreEvent}.
+ *
+ * @param initiator The user who initiated the request.
+ * @param metalake The metalake name.
+ * @param userId The Gravitino-assigned id of the user.
+ */
+ public RemoveUserByIdPreEvent(String initiator, String metalake, long userId) {
+ super(initiator, AuthorizationUtils.ofUserId(metalake, userId));
+ this.userId = userId;
+ }
+
+ /**
+ * Returns the Gravitino-assigned id of the user being removed.
+ *
+ * @return The user id.
+ */
+ public long userId() {
+ return userId;
+ }
+
+ @Override
+ public OperationType operationType() {
+ return OperationType.REMOVE_USER_BY_ID;
+ }
+}
diff --git a/core/src/test/java/org/apache/gravitino/audit/v2/TestCompatibilityUtils.java b/core/src/test/java/org/apache/gravitino/audit/v2/TestCompatibilityUtils.java
index 7c6b42db62..9e00a0b04e 100644
--- a/core/src/test/java/org/apache/gravitino/audit/v2/TestCompatibilityUtils.java
+++ b/core/src/test/java/org/apache/gravitino/audit/v2/TestCompatibilityUtils.java
@@ -128,6 +128,9 @@ public class TestCompatibilityUtils {
{OperationType.REMOVE_USER_BY_EXTERNAL_ID, Operation.REMOVE_USER_BY_EXTERNAL_ID},
{OperationType.GET_USER, Operation.GET_USER},
{OperationType.GET_USER_BY_EXTERNAL_ID, Operation.GET_USER_BY_EXTERNAL_ID},
+ {OperationType.GET_USER_BY_ID, Operation.GET_USER_BY_ID},
+ {OperationType.REMOVE_USER_BY_ID, Operation.REMOVE_USER_BY_ID},
+ {OperationType.ALTER_USER, Operation.ALTER_USER},
{OperationType.ENABLE_USER, Operation.ENABLE_USER},
{OperationType.DISABLE_USER, Operation.DISABLE_USER},
{OperationType.LIST_USERS, Operation.LIST_USERS},
diff --git a/core/src/test/java/org/apache/gravitino/listener/api/event/TestUserEvent.java b/core/src/test/java/org/apache/gravitino/listener/api/event/TestUserEvent.java
index 90c9161582..ef10a28cea 100644
--- a/core/src/test/java/org/apache/gravitino/listener/api/event/TestUserEvent.java
+++ b/core/src/test/java/org/apache/gravitino/listener/api/event/TestUserEvent.java
@@ -19,6 +19,8 @@
package org.apache.gravitino.listener.api.event;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -29,6 +31,7 @@ import java.util.Optional;
import org.apache.gravitino.NameIdentifier;
import org.apache.gravitino.authorization.AuthorizationUtils;
import org.apache.gravitino.authorization.User;
+import org.apache.gravitino.authorization.UserChange;
import org.apache.gravitino.exceptions.GravitinoRuntimeException;
import org.apache.gravitino.exceptions.NoSuchMetalakeException;
import org.apache.gravitino.exceptions.NoSuchUserException;
@@ -47,6 +50,7 @@ public class TestUserEvent {
private static final String METALAKE = "demo_metalake";
private static final String INEXIST_METALAKE = "inexist_metalake";
private static final String USER_EXT_ID = "ext-user-1";
+ private static final long USER_ID = 1L;
private AccessControlEventDispatcher dispatcher;
private AccessControlEventDispatcher failureDispatcher;
private DummyEventListener dummyEventListener;
@@ -531,6 +535,88 @@ public class TestUserEvent {
Assertions.assertEquals(OperationType.GET_USER_BY_EXTERNAL_ID, event.operationType());
}
+ @Test
+ void testGetUserByIdEvent() {
+ dispatcher.getUserById(METALAKE, USER_ID);
+
+ PreEvent preEvent = dummyEventListener.popPreEvent();
+ Assertions.assertEquals(GetUserByIdPreEvent.class, preEvent.getClass());
+ Assertions.assertEquals(OperationType.GET_USER_BY_ID, preEvent.operationType());
+ Assertions.assertEquals(USER_ID, ((GetUserByIdPreEvent) preEvent).userId());
+ Assertions.assertEquals(AuthorizationUtils.ofUserId(METALAKE, USER_ID), preEvent.identifier());
+
+ Event event = dummyEventListener.popPostEvent();
+ Assertions.assertEquals(GetUserByIdEvent.class, event.getClass());
+ Assertions.assertEquals(OperationType.GET_USER_BY_ID, event.operationType());
+ validateUserInfo(((GetUserByIdEvent) event).loadedUserInfo(), user);
+ }
+
+ @Test
+ void testGetUserByIdFailureEvent() {
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class, () -> failureDispatcher.getUserById(METALAKE, USER_ID));
+
+ dummyEventListener.popPreEvent();
+ Event event = dummyEventListener.popPostEvent();
+ Assertions.assertEquals(GetUserByIdFailureEvent.class, event.getClass());
+ Assertions.assertEquals(OperationType.GET_USER_BY_ID, event.operationType());
+ Assertions.assertEquals(USER_ID, ((GetUserByIdFailureEvent) event).userId());
+ }
+
+ @Test
+ void testRemoveUserByIdEvent() {
+ dispatcher.removeUserById(METALAKE, USER_ID);
+
+ PreEvent preEvent = dummyEventListener.popPreEvent();
+ Assertions.assertEquals(RemoveUserByIdPreEvent.class, preEvent.getClass());
+ Assertions.assertEquals(OperationType.REMOVE_USER_BY_ID, preEvent.operationType());
+ Assertions.assertEquals(USER_ID, ((RemoveUserByIdPreEvent) preEvent).userId());
+
+ Event event = dummyEventListener.popPostEvent();
+ Assertions.assertEquals(RemoveUserByIdEvent.class, event.getClass());
+ Assertions.assertEquals(OperationType.REMOVE_USER_BY_ID, event.operationType());
+ Assertions.assertTrue(((RemoveUserByIdEvent) event).isExists());
+ }
+
+ @Test
+ void testAlterUserEvent() {
+ UserChange[] changes =
+ new UserChange[] {
+ UserChange.updateEnabled(false), UserChange.updateExternalId("ext-user-2")
+ };
+ dispatcher.alterUserById(METALAKE, USER_ID, changes);
+
+ PreEvent preEvent = dummyEventListener.popPreEvent();
+ Assertions.assertEquals(AlterUserPreEvent.class, preEvent.getClass());
+ Assertions.assertEquals(OperationType.ALTER_USER, preEvent.operationType());
+ AlterUserPreEvent alterPreEvent = (AlterUserPreEvent) preEvent;
+ Assertions.assertEquals(USER_ID, alterPreEvent.userId());
+ Assertions.assertArrayEquals(changes, alterPreEvent.changes());
+
+ Event event = dummyEventListener.popPostEvent();
+ Assertions.assertEquals(AlterUserEvent.class, event.getClass());
+ Assertions.assertEquals(OperationType.ALTER_USER, event.operationType());
+ AlterUserEvent alterEvent = (AlterUserEvent) event;
+ Assertions.assertArrayEquals(changes, alterEvent.changes());
+ validateUserInfo(alterEvent.updatedUserInfo(), user);
+ }
+
+ @Test
+ void testAlterUserFailureEvent() {
+ UserChange change = UserChange.updateEnabled(true);
+ Assertions.assertThrowsExactly(
+ GravitinoRuntimeException.class,
+ () -> failureDispatcher.alterUserById(METALAKE, USER_ID, change));
+
+ dummyEventListener.popPreEvent();
+ Event event = dummyEventListener.popPostEvent();
+ Assertions.assertEquals(AlterUserFailureEvent.class, event.getClass());
+ Assertions.assertEquals(OperationType.ALTER_USER, event.operationType());
+ AlterUserFailureEvent failureEvent = (AlterUserFailureEvent) event;
+ Assertions.assertEquals(USER_ID, failureEvent.userId());
+ Assertions.assertArrayEquals(new UserChange[] {change}, failureEvent.changes());
+ }
+
private AccessControlEventDispatcher mockUserDispatcher() {
AccessControlEventDispatcher dispatcher = mock(AccessControlEventDispatcher.class);
when(dispatcher.addUser(METALAKE, userName)).thenReturn(user);
@@ -546,6 +632,10 @@ public class TestUserEvent {
when(dispatcher.getUser(METALAKE, userName)).thenReturn(user);
when(dispatcher.getUserByExternalId(METALAKE, USER_EXT_ID)).thenReturn(externalIdUser);
+ when(dispatcher.getUserById(METALAKE, USER_ID)).thenReturn(user);
+ when(dispatcher.removeUserById(METALAKE, USER_ID)).thenReturn(true);
+ when(dispatcher.alterUserById(eq(METALAKE), eq(USER_ID), any(UserChange[].class)))
+ .thenReturn(user);
when(dispatcher.getUser(METALAKE, inExistUserName))
.thenThrow(new NoSuchUserException("user not found"));
when(dispatcher.getUser(INEXIST_METALAKE, userName))