【发布时间】:2018-12-13 22:23:22
【问题描述】:
我目前正在使用 Python 和 LDAP 为用户查询 Active Directory。
我有一个名列前茅的名字。不够具体,无法找到确切的用户。
我想要一个过滤器,可以找到所有匹配“Last,First*”的用户 并属于其中包含关键字的任何组。
_filter = '''(& (objectclass=user) (objectcategory=person) (name={}*) )'''.format(search_string)
我已经尝试添加...
(memberOf=CN=*Keyword*,OU=Delegated,OU=Groups,DC=amr,DC=corp,DC=xxxxxx,DC=com)
到我的过滤器,但没有成功。
如果这是 SQL,我会这样写:
Select *
From
Users
Where
Users.name like 'First, Last%'
and Users.memberOf like 'Keyword%'
更新:
在查看加布里埃尔的答案后,我正在运行这个。
def get_idsids(self, search_string): _filter = '''(& (objectclass=user) (objectcategory=person) (anr={}) )'''.format(search_string) # Search for user. # Will return list of users matching criteria. # The results are wrapped up as a list(tuple(dict))) where the dict vals are binary strings or lists of binary strings. users = self.con.search_s(ActiveDirUser.BASEDN, ldap.SCOPE_SUBTREE, _filter, ['displayName', 'sAMAccountName', 'memberOf']) # This line is ugly... It just converts the results to a list of ids # So long as the user has at least one group with 'Keyword' in the name. # upper() is used to make the Keyword requriement case insensitive. return [user[1]['sAMAccountName'][0].decode() for user in users if 'KEYWORD' in ''.join(map(str, user[1]['memberOf'])).upper()]
我想知道,我可以搜索名称中带有“关键字”的组并从中构建过滤器吗?此外,这会更快吗?我认为它会像 AD 一样散列组成员身份。
我会去阅读,但我认为组名是通配符可搜索的?
【问题讨论】:
标签: python active-directory ldap