yuqi1129 commented on code in PR #4323:
URL: https://github.com/apache/gravitino/pull/4323#discussion_r1699808016


##########
core/src/main/java/org/apache/gravitino/authorization/OwnershipManager.java:
##########
@@ -0,0 +1,138 @@
+/*
+ * 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.authorization;
+
+import java.io.IOException;
+import java.util.List;
+import org.apache.gravitino.Entity;
+import org.apache.gravitino.EntityStore;
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.exceptions.NoSuchEntityException;
+import org.apache.gravitino.exceptions.NotFoundException;
+import org.apache.gravitino.exceptions.OwnerNotFoundException;
+import org.apache.gravitino.meta.GroupEntity;
+import org.apache.gravitino.meta.UserEntity;
+import org.apache.gravitino.relation.Relation;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** OwnershipManager is used for manage the owner of securable objects, user, 
group and role. */
+public class OwnershipManager {
+  private static final Logger LOG = 
LoggerFactory.getLogger(OwnershipManager.class);
+  private final EntityStore store;
+
+  public OwnershipManager(EntityStore store) {
+    this.store = store;
+  }
+
+  public void setOwner(
+      NameIdentifier identifier, Entity.EntityType type, String ownerName, 
Owner.Type ownerType) {
+    try {
+      String metalake;
+      if (type == Entity.EntityType.METALAKE) {
+        metalake = identifier.name();
+      } else {
+        metalake = identifier.namespace().level(0);
+      }
+
+      if (ownerType == Owner.Type.USER) {
+        store
+            .relationOperations()
+            .insertRelation(
+                Relation.Type.OWNER_REL,
+                identifier,
+                type,
+                AuthorizationUtils.ofUser(metalake, ownerName),
+                Entity.EntityType.USER,
+                true);
+      } else if (ownerType == Owner.Type.GROUP) {
+        store
+            .relationOperations()
+            .insertRelation(
+                Relation.Type.OWNER_REL,
+                identifier,
+                type,
+                AuthorizationUtils.ofGroup(metalake, ownerName),
+                Entity.EntityType.GROUP,
+                true);
+      }
+    } catch (NoSuchEntityException nse) {
+      LOG.warn("Entity {} or owner {} is not found", identifier, ownerName, 
nse);
+      throw new NotFoundException(nse, "Entity %s or owner %s is not found", 
identifier, ownerName);
+    } catch (IOException ioe) {
+      LOG.info("Fail to set the owner {} of entity {}", ownerName, identifier, 
ioe);
+      throw new RuntimeException(ioe);
+    }
+  }
+
+  public Owner getOwner(NameIdentifier identifier, Entity.EntityType type) {
+    try {
+      OwnerImpl owner = new OwnerImpl();
+      List entities =
+          store
+              .relationOperations()
+              .listEntitiesByRelation(Relation.Type.OWNER_REL, identifier, 
type);
+      if (entities.isEmpty()) {

Review Comment:
   I'm afriad that existing entities may has no ower and then throw an 
exception here.



##########
core/src/main/java/org/apache/gravitino/authorization/AuthorizationUtils.java:
##########
@@ -131,58 +132,127 @@ public static <T> void prepareAuthorizationHooks(T 
manager, DispatcherHooks hook
       hooks.addPostHook(
           "createMetalake",
           (args, metalake) -> {
-            // TODO: Add the logic of setting the owner
+            NameIdentifier identifier = (NameIdentifier) ((Object[]) args)[0];

Review Comment:
   It's a little hacky to get instance of NameIdentifier here, how can I know 
the type of the first argument is `NameIdentifier`?



##########
api/src/main/java/org/apache/gravitino/authorization/Owner.java:
##########
@@ -0,0 +1,49 @@
+/*
+ * 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.authorization;
+
+/**
+ * Every metadata object has owner. Every role, user or group has owner, too. 
The owner can have all

Review Comment:
   Has an owner?



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