zddr commented on code in PR #51845:
URL: https://github.com/apache/doris/pull/51845#discussion_r2155941886


##########
regression-test/suites/nereids_p0/show/test_nereids_show_create_user.groovy:
##########
@@ -0,0 +1,30 @@
+// 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.
+
+suite("test_nereids_show_create_user") {
+    sql "DROP USER IF EXISTS 'xxxxxxx'"
+    sql "CREATE USER IF NOT EXISTS 'xxxxxxx' IDENTIFIED BY '12345' 
PASSWORD_EXPIRE INTERVAL 10 DAY FAILED_LOGIN_ATTEMPTS 3 PASSWORD_LOCK_TIME 1 
DAY;"
+
+    checkNereidsExecute("SHOW ALL CREATE USER")
+    checkNereidsExecute("SHOW CREATE USER xxxxxxx")
+
+    def res1 = sql """SHOW ALL CREATE USER"""
+    assertEquals(true, res1.size() > 1)
+
+    def res2 = sql """SHOW CREATE USER xxxxxxx"""
+    assertEquals('xxxxxxx', res2.get(0).get(0))

Review Comment:
   - add case like create user1@"192.158.1.%" 
   - add case or ut like create user2@[xx.xx.com]
   - Add a test case to verify that the output of SHOW CREATE USER can be 
executed (passwords can be replaced).



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowCreateUserCommand.java:
##########
@@ -0,0 +1,194 @@
+// 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.doris.nereids.trees.plans.commands;
+
+import org.apache.doris.analysis.UserIdentity;
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.catalog.ScalarType;
+import org.apache.doris.common.AnalysisException;
+import org.apache.doris.common.ErrorCode;
+import org.apache.doris.common.ErrorReport;
+import org.apache.doris.mysql.privilege.PrivPredicate;
+import org.apache.doris.mysql.privilege.User;
+import org.apache.doris.nereids.trees.plans.PlanType;
+import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor;
+import org.apache.doris.qe.ConnectContext;
+import org.apache.doris.qe.ShowResultSet;
+import org.apache.doris.qe.ShowResultSetMetaData;
+import org.apache.doris.qe.StmtExecutor;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * show create user command
+ */
+public class ShowCreateUserCommand extends ShowCommand {
+    private static final ShowResultSetMetaData META_DATA =
+            ShowResultSetMetaData.builder()
+                .addColumn(new Column("User Identity", 
ScalarType.createVarchar(512)))
+                .addColumn(new Column("Create Stmt", 
ScalarType.createVarchar(1024)))
+                .build();
+    private UserIdentity userIdent;
+    private final boolean showAllUser;
+
+    /**
+     * constructor for show create user
+     */
+    public ShowCreateUserCommand(UserIdentity userIdent, boolean showAllUser) {
+        super(PlanType.SHOW_CREATE_USER_COMMAND);
+        this.userIdent = userIdent;
+        this.showAllUser = showAllUser;
+    }
+
+    @VisibleForTesting
+    protected ShowResultSet handleShowCreateUser(ConnectContext ctx, 
StmtExecutor executor) throws Exception {
+        if (userIdent != null) {
+            if (showAllUser) {
+                throw new AnalysisException("Can not specified keyword ALL 
when specified user");
+            }
+            userIdent.analyze();
+        } else {
+            if (!showAllUser) {
+                // self
+                userIdent = ConnectContext.get().getCurrentUserIdentity();
+            }
+        }
+        Preconditions.checkState(showAllUser || userIdent != null);
+        UserIdentity self = ConnectContext.get().getCurrentUserIdentity();
+
+        // if show all grants, or show other user's grants, need global GRANT 
priv.
+        if (showAllUser || !self.equals(userIdent)) {
+            if 
(!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ConnectContext.get(), 
PrivPredicate.GRANT)) {
+                
ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, 
"GRANT");
+            }
+        }
+        if (userIdent != null && 
!Env.getCurrentEnv().getAccessManager().getAuth().doesUserExist(userIdent)) {
+            throw new AnalysisException(String.format("User: %s does not 
exist", userIdent));
+        }
+
+        // get all user identity
+        List<List<String>> infos = new ArrayList<>();
+        List<UserIdentity> userList = new ArrayList<>();
+        if (!showAllUser) {
+            if (userIdent != null) {
+                userList.add(userIdent);
+            }
+        } else {
+            if (Env.getCurrentEnv().getAuth().getUserManager() != null) {
+                Map<String, List<User>> usersMap = 
Env.getCurrentEnv().getAuth().getUserManager().getNameToUsers();
+                for (List<User> users : usersMap.values()) {
+                    for (User user : users) {
+                        if (user == null) {
+                            continue;
+                        }
+                        userList.add(user.getUserIdentity());
+                    }
+                }
+            }
+        }
+
+        // get all user's create stmt
+        for (UserIdentity identity : userList) {
+            if (identity == null) {
+                continue;
+            }
+            List<String> userInfo = new ArrayList<>();
+            userInfo.add(identity.getQualifiedUser());
+            userInfo.add(toSql(identity));
+            infos.add(userInfo);
+        }
+
+        // order by UserIdentity
+        infos.sort(Comparator.comparing(list -> list.isEmpty() ? "" : 
list.get(0)));
+        return new ShowResultSet(getMetaData(), infos);
+    }
+
+    @VisibleForTesting
+    protected String toSql(UserIdentity user) {
+        if (user == null) {
+            return "";
+        }
+        StringBuilder sb = new StringBuilder("CREATE USER ");
+
+        // user identity
+        sb.append(user);
+
+        // user password
+        sb.append(" IDENTIFIED BY *** ");
+
+        // default role
+        Set<String> roles = Env.getCurrentEnv().getAuth().getRolesByUser(user, 
true);
+        if (roles != null && !roles.isEmpty()) {
+            sb.append(" DEFAULT ROLE '").append(String.join(",", 
roles)).append("' ");
+        }
+
+        // password policy
+        if (Env.getCurrentEnv().getAuth().getPasswdPolicyManager() != null) {
+            List<List<String>> policies = 
Env.getCurrentEnv().getAuth().getPasswdPolicyManager().getPolicyInfo(user);
+            if (policies != null) {
+                if (policies.size() > 1) {

Review Comment:
   Explain what 1, 3, and 7 mean
   



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to