Esempio n. 1
0
def set_position(position, members):
    """
    Sets a position

    Parameters:
        position - the position to set
        members - an array of members that hold the position

    Example: set_position('president', ['dtbartle'])
    """

    res = ld.search_s(cfg['ldap_users_base'], ldap.SCOPE_SUBTREE,
        '(&(objectClass=member)(position=%s))' % ldapi.escape(position))
    old = set([ member['uid'][0] for (_, member) in res ])
    new = set(members)
    mods = {
        'del': set(old) - set(new),
        'add': set(new) - set(old),
    }
    if len(mods['del']) == 0 and len(mods['add']) == 0:
        return

    for action in ['del', 'add']:
        for userid in mods[action]:
            dn = 'uid=%s,%s' % (ldapi.escape(userid), cfg['ldap_users_base'])
            entry1 = {'position' : [position]}
            entry2 = {} #{'position' : []}
            entry = ()
            if action == 'del':
                entry = (entry1, entry2)
            elif action == 'add':
                entry = (entry2, entry1)
            mlist = ldapi.make_modlist(entry[0], entry[1])
            ld.modify_s(dn, mlist)
Esempio n. 2
0
def change_group_member(action, group, userid):
    user_dn = 'uid=%s,%s' % (ldapi.escape(userid), cfg['ldap_users_base'])
    group_dn = 'cn=%s,%s' % (ldapi.escape(group), cfg['ldap_groups_base'])
    entry1 = {'uniqueMember' : []}
    entry2 = {'uniqueMember' : [user_dn]}
    entry = []
    if action == 'add' or action == 'insert':
        entry = (entry1, entry2)
    elif action == 'remove' or action == 'delete':
        entry = (entry2, entry1)
    else:
        raise InvalidArgument("action", action, "invalid action")
    mlist = ldapi.make_modlist(entry[0], entry[1])
    ld.modify_s(group_dn, mlist)
Esempio n. 3
0
 def keypress(self, size, key):
     if (key == 'tab' or key == 'shift tab') and self.ldap != None:
         if self.index != None:
             if key == 'tab':
                 self.index = (self.index + 1) % len(self.choices)
             elif key == 'shift tab':
                 self.index = (self.index - 1) % len(self.choices)
             text = self.choices[self.index]
             self.set_edit_text(text)
             self.set_edit_pos(len(text))
         else:
             try:
                 text = self.get_edit_text()
                 search = ldapi.escape(text)
                 matches = self.ldap.search_s(self.base,
                     ldap.SCOPE_SUBTREE, '(%s=%s*)' % (self.attr, search))
                 self.choices = [ text ]
                 for match in matches:
                     (_, attrs) = match
                     self.choices += attrs['uid']
                 self.choices.sort()
                 self.index = 0
                 self.keypress(size, key)
             except ldap.LDAPError, e:
                 pass
Esempio n. 4
0
def register_nonmember(userid, term_list):
    """Registers a non-member for one or more terms."""

    user_dn = 'uid=%s,%s' % (ldapi.escape(userid), cfg['ldap_users_base'])

    if type(term_list) in (str, unicode):
        term_list = [ term_list ]

    ldap_member = get(userid)
    if not ldap_member:
        raise NoSuchMember(userid)

    if 'term' not in ldap_member:
        ldap_member['term'] = []
    if 'nonMemberTerm' not in ldap_member:
        ldap_member['nonMemberTerm'] = []

    new_member = ldap_member.copy()
    new_member['nonMemberTerm'] = new_member['nonMemberTerm'][:]

    for term in term_list:

        # check term syntax
        if not re.match('^[wsf][0-9]{4}$', term):
            raise InvalidTerm(term)

        # add the term to the entry
        if not term in ldap_member['nonMemberTerm'] \
                and not term in ldap_member['term']:
            new_member['nonMemberTerm'].append(term)

    mlist = ldapi.make_modlist(ldap_member, new_member)
    ld.modify_s(user_dn, mlist)
Esempio n. 5
0
 def __init__(self, uri, base, attr, *args):
     try:
         self.ldap = ldap.initialize(uri)
         self.ldap.simple_bind_s("", "")
     except ldap.LDAPError:
         return WordEdit.__init__(self, *args)
     self.base = base
     self.attr = ldapi.escape(attr)
     return WordEdit.__init__(self, *args)
Esempio n. 6
0
 def keypress(self, size, key):
     if self.ldap != None:
         if key == 'enter' or key == 'down' or key == 'up':
             search = ldapi.escape(self.get_edit_text())
             try:
                 matches = self.ldap.search_s(self.base,
                     ldap.SCOPE_SUBTREE, '(%s=%s)' % (self.attr, search))
                 if len(matches) > 0:
                     (_, attrs) = matches[0]
                     for (k, v) in self.map.items():
                         if attrs.has_key(k) and len(attrs[k]) > 0:
                             v.set_edit_text(attrs[k][0])
             except ldap.LDAPError:
                 pass
     return LdapWordEdit.keypress(self, size, key)
Esempio n. 7
0
def register(userid, term_list):
    """
    Registers a member for one or more terms.

    Parameters:
        userid  - the member's username
        term_list - the term to register for, or a list of terms

    Exceptions:
        InvalidTerm - if a term is malformed

    Example: register(3349, "w2007")

    Example: register(3349, ["w2007", "s2007"])
    """

    user_dn = 'uid=%s,%s' % (ldapi.escape(userid), cfg['ldap_users_base'])

    if type(term_list) in (str, unicode):
        term_list = [ term_list ]

    ldap_member = get(userid)
    if ldap_member and 'term' not in ldap_member:
        ldap_member['term'] = []

    if not ldap_member:
        raise NoSuchMember(userid)

    new_member = ldap_member.copy()
    new_member['term'] = new_member['term'][:]

    for term in term_list:

        # check term syntax
        if not re.match('^[wsf][0-9]{4}$', term):
            raise InvalidTerm(term)

        # add the term to the entry
        if not term in ldap_member['term']:
            new_member['term'].append(term)

    mlist = ldapi.make_modlist(ldap_member, new_member)
    ld.modify_s(user_dn, mlist)
Esempio n. 8
0
 def main(self, args):
   mlist = members.list_all().items()
   uwl = ldap.initialize(uwldap.uri())
   fd = sys.stdin.fileno()
   for (dn, member) in mlist:
     uid = member['uid'][0]
     user = uwl.search_s(uwldap.base(), ldap.SCOPE_SUBTREE,
       '(uid=%s)' % ldapi.escape(uid))
     if len(user) == 0:
       continue
     user = user[0][1]
     oldprog = member.get('program', [''])[0]
     newprog = user.get('ou', [''])[0]
     if oldprog == newprog or newprog == '' or newprog.lower() in blacklist:
       continue
     sys.stdout.write("%s: '%s' => '%s'? (y/n) " % (uid, oldprog, newprog))
     new = old = termios.tcgetattr(fd)
     new[3] = new[3] & ~termios.ICANON
     try:
       termios.tcsetattr(fd, termios.TCSANOW, new)
       try:
         if sys.stdin.read(1) != 'y':
           continue
       except KeyboardInterrupt:
         return ''
     finally:
       print ''
       termios.tcsetattr(fd, termios.TCSANOW, old)
     old = new = {}
     if oldprog != '':
       old = {'program': [oldprog]}
     if newprog != '':
       new = {'program': [newprog]}
     mlist = ldapi.make_modlist(old, new)
     # TODO: don't use members.ld directly
     #if newprog != '':
     #  members.set_program(uid, newprog)
     members.ld.modify_s(dn, mlist)
Esempio n. 9
0
def uid2dn(uid):
    return 'uid=%s,%s' % (ldapi.escape(uid), cfg['ldap_users_base'])