Search code examples
python-2.7flaskactive-directorypyad

Error trying to use pyad to query accounts on another domain


I'm using pyad 0.5.20 to do many operations on active directories which user can specify their connection details for example:

 pyad.set_defaults(ldap_server="my.server", username=myusername, password=xxx,ssl=False,ldap_port=123)

And then i can for example retrieve a group by it's name:

group = pyad.adgroup.ADGroup.from_cn(groupname)

my problem is with the pyad.adquery.ADQuery() when i'm trying to use it after specifying connection details i got an error

AttributeError: 'module' object has no attribute 'set_defaults'

And here is the full code:

import pythoncom
pythoncom.CoInitialize()
import pyad.adquery
ad = Auth_Type_Config.query.filter_by(auth_type_id=2).first()
if ad:
    pyad.set_defaults(ldap_server=ad.host, username=ad.username,
                      password=ad.password, ssl=ad.is_ssl,
                      ldap_port=ad.port)
else:
    return jsonify({'msg': 'You must configure the authentication method for LDAP.'
                   , 'status': 'error'})

q = pyad.adquery.ADQuery()
where_clause = "SamAccountName = '{}'"
for (index, item) in enumerate(usernames, start=1):
    if index == 1:
        continue
    where_clause += " or SamAccountName = '{}'"

q.execute_query(attributes=['distinguishedName', 'mail', 'name',
                'SamAccountName'],
                where_clause=where_clause.format(*usernames),
                base_dn='CN=Users,DC=dsp,DC=local')

rows = []
for row in q.get_results():
    rows.append(row)
user = (rows[0] if len(rows) > 0 else None)
return rows

Solution

  • I guess that's a little late, but the correct function seems to be pyad.pyad_setdefaults and not pyad.set_defaults