redbrick/useradm

View on GitHub
scripts/newyear_ldapmodify_ldif.py

Summary

Maintainability
A
0 mins
Test Coverage
#!/usr/bin/python3
import string
import sys

"""
-mak
This simply takes the ldif generated by newyear_ldif.py
and builds it into an ldapmodify formatted ldif.

To be used with the ldap modify query below
ldapmodify -x -D cn=root,ou=ldap,o=redbrick -y /etc/ldap.secret \
        -f [LDIF_FROM_THIS_SCRIPT]
"""

years_paid = ''
uid = ''

# print modify ldif template


def modifyTemplate(uid, years_paid, newbie, reserved):
    if uid != '' and years_paid != '' and reserved is False:
        modTemp = "dn: uid=" + uid.strip() + \
            "\nchangetype: modify\nreplace: years_paid\nyears_paid: " + \
            years_paid.strip() + "\n"
        if newbie == '1':
            modTemp += "-\nreplace: newbie\nnewbie: FALSE\n\n"
        else:
            modTemp += "\n"
        print(modTemp)


# open ldif
with open(sys.argv[1], 'r') as content:
    ldif = content.read()
# split by user
getdn = string.split(ldif, 'dn: uid=')
for i in range(1, len(getdn)):
    thisdn = getdn[i].split('\n')
    newbie = 'NONE'
    reserved = False
    # split by users variables
    for j in range(0, len(thisdn)):
        x = thisdn[j].rstrip()
        uid = thisdn[0].rstrip()
        if 'reserved' in uid:
            reserved = True
        try:
            if x.startswith("years_paid:"):
                years_paid = str(int(x.split()[1])).strip()
            elif x.startswith("newbie:"):
                newbie = '1'
        except IndexError:
            break
    modifyTemplate(uid, years_paid, newbie, reserved)