luozenglin commented on code in PR #11948:
URL: https://github.com/apache/doris/pull/11948#discussion_r952725674


##########
fe/fe-core/src/main/java/org/apache/doris/ldap/LdapManager.java:
##########
@@ -0,0 +1,197 @@
+// 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.ldap;
+
+import org.apache.doris.analysis.UserIdentity;
+import org.apache.doris.catalog.Env;
+import org.apache.doris.cluster.ClusterNamespace;
+import org.apache.doris.common.LdapConfig;
+import org.apache.doris.mysql.privilege.PaloAuth;
+import org.apache.doris.mysql.privilege.PaloRole;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.parquet.Strings;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+/**
+ * Encapsulates LDAP service interfaces and caches user LDAP information.
+ */
+public class LdapManager {
+    private static final Logger LOG = LogManager.getLogger(LdapManager.class);
+
+    private static final String LDAP_GROUPS_PRIVS_NAME = "ldapGroupsPrivs";
+
+    private final LdapClient ldapClient = new LdapClient();
+
+    private final Map<String, LdapUserInfo> ldapUserInfoCache = 
Maps.newHashMap();
+
+    private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
+
+    private long lastTimeStamp = System.currentTimeMillis();
+
+    // If the user exists in LDAP, the LDAP information of the user is 
returned; otherwise, null is returned.
+    public LdapUserInfo getUserInfo(String fullName) {
+        if (!checkParam(fullName)) {
+            return null;
+        }
+        LdapUserInfo ldapUserInfo = getUserInfoFromCache(fullName);
+        if (ldapUserInfo != null && !ldapUserInfo.checkTimeout()) {
+            return ldapUserInfo;
+        }
+        return updateUserInfo(fullName);
+    }
+
+    public boolean doesUserExist(String fullName) {
+        if (!checkParam(fullName)) {
+            return false;
+        }
+        return !Objects.isNull(getUserInfo(fullName));
+    }
+
+    public boolean checkUserPasswd(String fullName, String passwd) {
+        String userName = ClusterNamespace.getNameFromFullName(fullName);
+        if (!LdapConfig.ldap_authentication_enabled || 
Strings.isNullOrEmpty(userName) || Objects.isNull(passwd)) {
+            return false;
+        }
+        LdapUserInfo ldapUserInfo = getUserInfo(fullName);
+        if (Objects.isNull(ldapUserInfo)) {
+            return false;
+        }
+
+        if (ldapUserInfo.isSetPasswd() && 
ldapUserInfo.getPasswd().equals(passwd)) {
+            return true;
+        }
+        boolean isRightPasswd = ldapClient.checkPassword(userName, passwd);
+        if (!isRightPasswd) {
+            return false;
+        }
+        updatePasswd(ldapUserInfo, passwd);
+        return true;
+    }
+
+    public boolean checkUserPasswd(String fullName, String passwd, String 
remoteIp, List<UserIdentity> currentUser) {
+        if (checkUserPasswd(fullName, passwd)) {
+            
currentUser.add(UserIdentity.createAnalyzedUserIdentWithIp(fullName, remoteIp));
+            return true;
+        }
+        return false;
+    }
+
+    private boolean checkParam(String fullName) {
+        return LdapConfig.ldap_authentication_enabled && 
!Strings.isNullOrEmpty(fullName) && !fullName.equalsIgnoreCase(
+                PaloAuth.ROOT_USER) && 
!fullName.equalsIgnoreCase(PaloAuth.ADMIN_USER);
+    }
+
+    private LdapUserInfo updateUserInfo(String fulName) {

Review Comment:
   done. thank you.



-- 
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: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to