misc/scripts/ldap_change_base_dn.py
changeset 10589 7c23b7de2b8d
parent 9460 a2a0bc984863
equal deleted inserted replaced
10588:fdaa0e4b7eaf 10589:7c23b7de2b8d
       
     1 from __future__ import print_function
       
     2 
     1 from base64 import b64decode, b64encode
     3 from base64 import b64decode, b64encode
     2 try:
     4 try:
     3     uri, newdn = __args__
     5     uri, newdn = __args__
     4 except ValueError:
     6 except ValueError:
     5     print 'USAGE: cubicweb-ctl shell <instance> ldap_change_base_dn.py -- <ldap source uri> <new dn>'
     7     print('USAGE: cubicweb-ctl shell <instance> ldap_change_base_dn.py -- <ldap source uri> <new dn>')
     6     print
     8     print()
     7     print 'you should not have updated your sources file yet'
     9     print('you should not have updated your sources file yet')
     8 
    10 
     9 olddn = repo.sources_by_uri[uri].config['user-base-dn']
    11 olddn = repo.sources_by_uri[uri].config['user-base-dn']
    10 
    12 
    11 assert olddn != newdn
    13 assert olddn != newdn
    12 
    14 
    14 
    16 
    15 for eid, extid in sql("SELECT eid, extid FROM entities WHERE source='%s'" % uri):
    17 for eid, extid in sql("SELECT eid, extid FROM entities WHERE source='%s'" % uri):
    16     olduserdn = b64decode(extid)
    18     olduserdn = b64decode(extid)
    17     newuserdn = olduserdn.replace(olddn, newdn)
    19     newuserdn = olduserdn.replace(olddn, newdn)
    18     if newuserdn != olduserdn:
    20     if newuserdn != olduserdn:
    19         print olduserdn, '->', newuserdn
    21         print(olduserdn, '->', newuserdn)
    20         sql("UPDATE entities SET extid='%s' WHERE eid=%s" % (b64encode(newuserdn), eid))
    22         sql("UPDATE entities SET extid='%s' WHERE eid=%s" % (b64encode(newuserdn), eid))
    21 
    23 
    22 commit()
    24 commit()
    23 
    25 
    24 print 'you can now update the sources file to the new dn and restart the instance'
    26 print('you can now update the sources file to the new dn and restart the instance')