繁体   English   中英

Python ldap3 搜索何时创建

[英]Python ldap3 Search whenCreated

我正在尝试搜索广告以查找在特定日期创建的用户,它为我提供了所有用户。 我该如何过滤?

def creating_date(self, when_Created = "2020-03-11", attribute="whenCreated"):        
    """
    Queries for users created on specific date
    """
    connection = self.conn

    attributes = ["cn",  
                  "displayName",
                  "mail",
                  "whenCreated"]

    search_filter = f"(&(objectClass=person))"
    connection.search(self.search_base, search_filter, attributes=attributes)

    entry = self.conn.entries

    try:
        return entry

    except (IndexError):
        return "{}"

请指教。

您必须构造 search_filter 以包含whenCreated属性和传递给方法的when_Created值。

像这样的东西:

ldapFilter = ldap.filter.escape_filter_chars(when_Created)
search_filter = f"(&(objectClass=person)(whenCreated=" + ldapFilter + "))"

请注意,尽管您可以连接when_Created值,但使用escape_filter_chars()可以防止代码注入。

编辑:

只是看了我自己的一个脚本,它使用ldap.filter.filter_format()来构造过滤器: https://www.python-ldap.org/en/python-ldap-3.3.0/reference/ldap-filter .html#ldap.filter.filter_format

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM