Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #8299

Re: LDAP: How get all users belongs to a group.

References <542151da-ebfb-4cfb-b83d-14f2ba641bf4@x38g2000pri.googlegroups.com>
From Ken Watford <kwatford+python@gmail.com>
Date 2011-06-23 09:59 -0400
Subject Re: LDAP: How get all users belongs to a group.
Newsgroups comp.lang.python
Message-ID <mailman.322.1308837607.1164.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Jun 23, 2011 at 9:14 AM, sajuptpm <sajuptpm@gmail.com> wrote:
> Hi,
> How get all users belongs to a group using python ldap module.

Depends on what you mean by "users" and "group", what information you
already have, and what information you want to get. I'll assume you
mean posix accounts and groups, and that you already know how to
connect to the LDAP server.

If you already know the distinguished name of the group, you can get a
list of the member names like so (ignoring error handling):

dn, entry = connection.search_s(group_dn, ldap.SCOPE_BASE)[0]
member_list = entry['memberUid']

That will only get you the usernames. If you need to get the user's
entry (or don't know the group_dn above), then you'll have to do a bit
more searching.

To find a user's entry given their uid:

results = connection.search_s(base_dn, ldap.SCOPE_SUBTREE, "(uid=*)")
for dn, entry in results:
     if uid in entry['uid']:
         # this is your guy. return, or break, or whatever

The "(uid=*)" filter just means to only find entries that have user id
fields. If you wanted to be more specific about it, you could limit it
to only posixAccount objects with "(objectClass=posixAccount)". This
would probably be necessary if you wanted to search for groups (via
"(objectClass=posixGroup)" ), since those don't have a special field
for their name - they usually just use the cn (common name) field for
that. A slightly more complex filter could be written to avoid the
python loop.

If your groups are not posixGroup objects but instead groupOfNames,
then the appropriate attribute is "member" rather than "memberUid",
and the values there are user DNs instead of uids. In that case, if
you need the uid you'll have to look up those users and pull it out.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

LDAP: How get all users belongs to a group. sajuptpm <sajuptpm@gmail.com> - 2011-06-23 06:14 -0700
  Re: LDAP: How get all users belongs to a group. Ken Watford <kwatford+python@gmail.com> - 2011-06-23 09:59 -0400
  Re: LDAP: How get all users belongs to a group. Michael Ströder <michael@stroeder.com> - 2011-06-24 11:14 +0200
    Re: LDAP: How get all users belongs to a group. sajuptpm <sajuptpm@gmail.com> - 2011-06-24 02:58 -0700
      Re: LDAP: How get all users belongs to a group. sajuptpm <sajuptpm@gmail.com> - 2011-06-24 03:06 -0700
      Re: LDAP: How get all users belongs to a group. sajuptpm <sajuptpm@gmail.com> - 2011-06-24 03:16 -0700
        Re: LDAP: How get all users belongs to a group. Michael Ströder <michael@stroeder.com> - 2011-06-24 13:02 +0200
      Re: LDAP: How get all users belongs to a group. John Gordon <gordon@panix.com> - 2011-06-24 14:57 +0000
        Re: LDAP: How get all users belongs to a group. sajuptpm <sajuptpm@gmail.com> - 2011-06-25 00:08 -0700
          Re: LDAP: How get all users belongs to a group. Michael Ströder <michael@stroeder.com> - 2011-06-25 12:20 +0200

csiph-web