【问题标题】:Python ldap3 Search whenCreatedPython ldap3 搜索何时创建
【发布时间】:2021-02-08 19:48:19
【问题描述】:

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

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 "{}"

请指教。

【问题讨论】:

    标签: python python-3.x active-directory ldap


    【解决方案1】:

    您必须构造 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

    【讨论】:

      猜你喜欢
      • 2016-10-22
      • 1970-01-01
      • 1970-01-01
      • 2023-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-27
      • 1970-01-01
      相关资源
      最近更新 更多