lasdf1234 commented on code in PR #10971:
URL: https://github.com/apache/gravitino/pull/10971#discussion_r3213076831


##########
plugins/idp-basic/src/main/java/org/apache/gravitino/idp/basic/authorization/IdpManager.java:
##########
@@ -0,0 +1,236 @@
+/*
+ * 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 com.google.common.base.Preconditions;
+import java.util.ArrayList;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.gravitino.Config;
+import org.apache.gravitino.Configs;
+import org.apache.gravitino.GravitinoEnv;
+import org.apache.gravitino.dto.IdpGroupDTO;
+import org.apache.gravitino.exceptions.ForbiddenException;
+import org.apache.gravitino.exceptions.GroupAlreadyExistsException;
+import org.apache.gravitino.exceptions.NoSuchGroupException;
+import org.apache.gravitino.exceptions.NoSuchUserException;
+import org.apache.gravitino.storage.IdGenerator;
+import org.apache.gravitino.storage.relational.po.IdpGroupPO;
+import org.apache.gravitino.storage.relational.po.IdpGroupUserRelPO;
+import org.apache.gravitino.storage.relational.po.IdpUserPO;
+import org.apache.gravitino.storage.relational.service.IdpGroupMetaService;
+import org.apache.gravitino.storage.relational.service.IdpUserMetaService;
+import org.apache.gravitino.utils.PrincipalUtils;
+
+/** Manager for built-in IdP group management APIs. */
+public class IdpGroupManager {
+  private static final long INITIAL_VERSION = 1L;
+
+  private final Config config;
+  private final IdGenerator idGenerator;
+  private final IdpUserMetaService userMetaService;
+  private final IdpGroupMetaService groupMetaService;
+
+  public static IdpGroupManager fromEnvironment() {
+    return new IdpGroupManager(
+        GravitinoEnv.getInstance().config(),
+        GravitinoEnv.getInstance().idGenerator(),
+        IdpUserMetaService.getInstance(),
+        IdpGroupMetaService.getInstance());
+  }
+
+  IdpGroupManager(
+      Config config,
+      IdGenerator idGenerator,
+      IdpUserMetaService userMetaService,
+      IdpGroupMetaService groupMetaService) {
+    this.config = config;
+    this.idGenerator = idGenerator;
+    this.userMetaService = userMetaService;
+    this.groupMetaService = groupMetaService;
+  }
+
+  public IdpGroupDTO createGroup(String groupName) {
+    ensureServiceAdmin();
+    validateGroupName(groupName);
+    if (groupMetaService().findGroup(groupName).isPresent()) {
+      throw new GroupAlreadyExistsException("Built-in IdP group %s already 
exists", groupName);
+    }
+
+    groupMetaService()
+        .createGroup(
+            IdpGroupPO.builder()
+                .withGroupId(nextId())
+                .withGroupName(groupName)
+                .withCurrentVersion(INITIAL_VERSION)
+                .withLastVersion(INITIAL_VERSION)
+                .withDeletedAt(0L)
+                .build());
+    return getGroup(groupName);
+  }
+
+  public IdpGroupDTO getGroup(String groupName) {
+    validateGroupName(groupName);
+    IdpGroupPO groupPO =
+        groupMetaService()
+            .findGroup(groupName)
+            .orElseThrow(
+                () -> new NoSuchGroupException("Built-in IdP group %s does not 
exist", groupName));
+    return toGroupDTO(groupPO);
+  }

Review Comment:
   The permissions are controlled by the upper-level Operations level.



##########
core/src/main/java/org/apache/gravitino/authorization/IdpUserManager.java:
##########
@@ -0,0 +1,172 @@
+/*
+ * 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 com.google.common.base.Preconditions;
+import java.util.List;
+import java.util.Optional;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.gravitino.Config;
+import org.apache.gravitino.Configs;
+import org.apache.gravitino.GravitinoEnv;
+import org.apache.gravitino.dto.IdpUserDTO;
+import org.apache.gravitino.exceptions.ForbiddenException;
+import org.apache.gravitino.exceptions.NoSuchUserException;
+import org.apache.gravitino.exceptions.UserAlreadyExistsException;
+import org.apache.gravitino.idp.basic.password.PasswordHasher;
+import org.apache.gravitino.idp.basic.password.PasswordHasherFactory;
+import org.apache.gravitino.storage.IdGenerator;
+import org.apache.gravitino.storage.relational.po.IdpUserPO;
+import org.apache.gravitino.storage.relational.service.IdpUserMetaService;
+import org.apache.gravitino.utils.PrincipalUtils;
+
+/** Manager for built-in IdP user management APIs. */
+public class IdpUserManager {
+  private static final long INITIAL_VERSION = 1L;
+
+  private final Config config;
+  private final IdGenerator idGenerator;
+  private final IdpUserMetaService userMetaService;
+  private final PasswordHasher passwordHasher;
+
+  public static IdpUserManager fromEnvironment() {
+    return fromEnvironment(PasswordHasherFactory.create());
+  }
+
+  public static IdpUserManager fromEnvironment(PasswordHasher passwordHasher) {
+    return new IdpUserManager(
+        GravitinoEnv.getInstance().config(),
+        GravitinoEnv.getInstance().idGenerator(),
+        IdpUserMetaService.getInstance(),
+        passwordHasher);
+  }
+
+  IdpUserManager(
+      Config config,
+      IdGenerator idGenerator,
+      IdpUserMetaService userMetaService,
+      PasswordHasher passwordHasher) {
+    this.config = config;
+    this.idGenerator = idGenerator;
+    this.userMetaService = userMetaService;
+    this.passwordHasher = passwordHasher;
+  }
+
+  public IdpUserDTO createUser(String userName, String password) {
+    ensureServiceAdmin();
+    validateUserName(userName);
+    validatePassword(password);
+    if (userMetaService().findUser(userName).isPresent()) {
+      throw new UserAlreadyExistsException("Built-in IdP user %s already 
exists", userName);
+    }
+
+    userMetaService()
+        .createUser(
+            IdpUserPO.builder()
+                .withUserId(nextId())
+                .withUserName(userName)
+                .withPasswordHash(passwordHasher.hash(password))
+                .withCurrentVersion(INITIAL_VERSION)
+                .withLastVersion(INITIAL_VERSION)
+                .withDeletedAt(0L)
+                .build());
+    return getUser(userName);
+  }
+
+  public IdpUserDTO getUser(String userName) {
+    validateUserName(userName);
+    IdpUserPO userPO =
+        userMetaService()
+            .findUser(userName)
+            .orElseThrow(
+                () -> new NoSuchUserException("Built-in IdP user %s does not 
exist", userName));
+    return toUserDTO(userPO);
+  }

Review Comment:
   The permissions are controlled by the upper-level Operations level.



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