zakird / pyad

Python Active Directory Tools | *Not actively maintained*
http://zakird.github.io/pyad/
176 stars 72 forks source link

How to retrieve members of a group? #34

Closed maphew closed 10 years ago

maphew commented 10 years ago

I've been unable to deduce how to retrieve a list of group members. I don't know whether it's because I'm not very python savvy or I've stumbled over pyad bug. Here's my code:

from pyad import adgroup
group = adgroup.ADGroup.from_dn(unit + domain)
print(group)

for user in group.get_members():
    print(user.description)

and result:

<ADGroup 'CN=my-group,OU=OurDept,OU=Users and Groups,DC=example,DC=org'>
Traceback (most recent call last):
  File "C:\Python27\ArcGIS10.3\lib\bdb.py", line 400, in run
    exec cmd in globals, locals
  File "D:\code\ENV\list-ad-users.py", line 1, in <module>
    domain = "DC=example,DC=org"
  File "D:\code\ENV\list-ad-users.py", line 20, in two
    for user in group.get_members():
  File "C:\Python27\ArcGIS10.3\lib\site-packages\pyad\adgroup.py", line 32, in get_members
    return self._get_members(recursive, ignoreGroups, [])
  File "C:\Python27\ArcGIS10.3\lib\site-packages\pyad\adgroup.py", line 43, in _get_members
    pyADobj.adjust_pyad_type()
  File "C:\Python27\ArcGIS10.3\lib\site-packages\pyad\adobject.py", line 516, in adjust_pyad_type
    raise Exception("Unkown type. Adjustment not possible.")
Exception: Unkown type. Adjustment not possible.
zakird commented 10 years ago

This looks like a bug, where you've ended up with some sort of object in the group that pyad doesn't handle. Can you easily tell me the type of objects in the group? At the very least you should be getting back a generic ADObject.

maphew commented 10 years ago

I'm not sure how to answer the question. Like this?

    from pyad import adgroup
    group = adgroup.ADGroup.from_dn(group_name + org_unit + domain)
    print "----- group:\n%s" % group
    print "----- dir:\n%s" % dir(group)
    print "----- type:\n%s" % type(group)

    # for user in group.get_members():
        # print(user.description)
d:\code\ENV>py list-ad-users.py
----- group:
<ADGroup 'CN=env-gis,OU=Environment,OU=Users and Groups,DC=YNet,DC=gov,DC=yk,DC=ca'>
----- dir:
['DEFAULTS_OPTIONS_MAPPINGS', '_ADObject__ads_path', '_ADObject__distinguished_name', '_ADObject__get_object_si
d', '_ADObject__get_parent_container', '_ADObject__get_prefixed_cn', '_ADObject__object_guid', '_ADObject__set_
adsi_obj', '_ADObject__set_gc_adsi_obj', '__class__', '__cmp__', '__delattr__', '__dict__', '__doc__', '__forma
t__', '__getattr__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__redu
ce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_domain_pyad_
obj', '_flush', '_gc_adsi_obj', '_get_memberOfs', '_get_members', '_get_password_last_set', '_init_global_catal
og_object', '_init_schema_object', '_ldap_adsi_obj', '_make_options', '_mandatory_attributes', '_optional_attri
butes', '_py_ad_object_mappings', '_safe_default_domain', '_safe_default_forest', '_schema_adsi_obj', '_set_att
ribute', '_set_defaults', '_type', 'add_members', 'add_to_group', 'adjust_pyad_type', 'adsPath', 'adsi_provider
', 'append_to_attribute', 'check_contains_member', 'clear_attribute', 'clear_managedby', 'create', 'default_dom
ain', 'default_forest', 'default_gc_port', 'default_gc_server', 'default_ldap_authentication_flag', 'default_ld
ap_port', 'default_ldap_protocol', 'default_ldap_server', 'default_password', 'default_ssl', 'default_username'
, 'delete', 'disable', 'dn', 'dump_to_xml', 'enable', 'from_cn', 'from_com_object', 'from_dn', 'from_guid', 'ge
t_allowed_attributes', 'get_attribute', 'get_group_scope', 'get_group_type', 'get_last_login', 'get_mandatory_a
ttributes', 'get_memberOfs', 'get_members', 'get_optional_attributes', 'get_uSNChanged', 'get_user_account_cont
rol_settings', 'guid', 'guid_str', 'is_member_of', 'move', 'parent_container', 'parent_container_path', 'prefix
ed_cn', 'remove_all_members', 'remove_from_attribute', 'remove_from_group', 'remove_members', 'rename', 'set_gr
oup_scope', 'set_group_type', 'set_managedby', 'set_user_account_control_setting', 'sid', 'sync_membership', 't
ype', 'update_attribute', 'update_attributes']
----- type:
<class 'pyad.adgroup.ADGroup'>
maphew commented 10 years ago

even just this fails:

 from pyad import adgroup
 group = adgroup.ADGroup.from_dn(group_name + org_unit + domain)
 group.get_members()
d:\code\ENV>py list-ad-users.py
Traceback (most recent call last):
  File "list-ad-users.py", line 47, in <module>
    two()
  File "list-ad-users.py", line 31, in two
    group.get_members()
  File "C:\Python27\ArcGIS10.3\lib\site-packages\pyad\adgroup.py", line 32, in get_members
    return self._get_members(recursive, ignoreGroups, [])
  File "C:\Python27\ArcGIS10.3\lib\site-packages\pyad\adgroup.py", line 43, in _get_members
    pyADobj.adjust_pyad_type()
  File "C:\Python27\ArcGIS10.3\lib\site-packages\pyad\adobject.py", line 516, in adjust_pyad_type
    raise Exception("Unkown type. Adjustment not possible.")
Exception: Unkown type. Adjustment not possible.
zakird commented 10 years ago

I just pushed a change to github that should fix problem. Can you run

from pyad import adgroup group = adgroup.ADGroup.from_dn(group_name + org_unit + domain) for m in group.get_members(): print m.get_attribute('objectCategory', False)

and send unique categories that come back?

On Thu, Oct 30, 2014 at 12:59 PM, matt wilkie notifications@github.com wrote:

even just this fails:

from pyad import adgroup group = adgroup.ADGroup.from_dn(group_name + org_unit + domain) group.get_members()

d:\code\ENV>py list-ad-users.py Traceback (most recent call last): File "list-ad-users.py", line 47, in two() File "list-ad-users.py", line 31, in two group.get_members() File "C:\Python27\ArcGIS10.3\lib\site-packages\pyad\adgroup.py", line 32, in get_members return self._get_members(recursive, ignoreGroups, []) File "C:\Python27\ArcGIS10.3\lib\site-packages\pyad\adgroup.py", line 43, in _get_members pyADobj.adjust_pyad_type() File "C:\Python27\ArcGIS10.3\lib\site-packages\pyad\adobject.py", line 516, in adjust_pyad_type raise Exception("Unkown type. Adjustment not possible.") Exception: Unkown type. Adjustment not possible.

— Reply to this email directly or view it on GitHub https://github.com/zakird/pyad/issues/34#issuecomment-61159623.

maphew commented 10 years ago

71 lines of the below. There are 71 members of the group.

CN=Person,CN=Schema,CN=Configuration,DC=gov,DC=yk,DC=ca
CN=Person,CN=Schema,CN=Configuration,DC=gov,DC=yk,DC=ca
CN=Person,CN=Schema,CN=Configuration,DC=gov,DC=yk,DC=ca
zakird commented 10 years ago

OK. I looked at the schema for Person (h/t @kbrautig) and it looks like they're pretty generic objects, which will likely be well represented by the parent ADObject class. Therefore, the fix from earlier should be sufficient. I'll push out the new version to pypi.

maphew commented 9 years ago

thanks for the fix, this is much appreciated!