misc/scripts/cwuser_ldap2system.py
changeset 6387 edbc53707bac
child 9460 a2a0bc984863
equal deleted inserted replaced
6384:89d5b339ebdd 6387:edbc53707bac
       
     1 import base64
       
     2 from cubicweb.server.utils import crypt_password
       
     3 
       
     4 dbdriver  = config.sources()['system']['db-driver']
       
     5 from logilab.database import get_db_helper
       
     6 dbhelper = get_db_helper(driver)
       
     7 
       
     8 insert = ('INSERT INTO cw_cwuser (cw_creation_date,'
       
     9           '                       cw_eid,'
       
    10           '                       cw_modification_date,'
       
    11           '                       cw_login,'
       
    12           '                       cw_firstname,'
       
    13           '                       cw_surname,'
       
    14           '                       cw_last_login_time,' 
       
    15           '                       cw_upassword,'
       
    16           '                       cw_cwuri) '
       
    17           "VALUES (%(mtime)s, %(eid)s, %(mtime)s, %(login)s, "
       
    18           "        %(firstname)s, %(surname)s, %(mtime)s, %(pwd)s, 'foo');")
       
    19 update = "UPDATE entities SET source='system' WHERE eid=%(eid)s;"
       
    20 rset = sql("SELECT eid,type,source,extid,mtime FROM entities WHERE source!='system'", ask_confirm=False)
       
    21 for eid, type, source, extid, mtime in rset:
       
    22     if type != 'CWUser':
       
    23         print "don't know what to do with entity type", type
       
    24         continue
       
    25     if not source.lower().startswith('ldap'):
       
    26         print "don't know what to do with source type", source
       
    27         continue
       
    28     extid = base64.decodestring(extid)
       
    29     ldapinfos = [x.strip().split('=') for x in extid.split(',')]
       
    30     login = ldapinfos[0][1]
       
    31     firstname = login.capitalize()
       
    32     surname = login.capitalize()
       
    33     args = dict(eid=eid, type=type, source=source, login=login,
       
    34                 firstname=firstname, surname=surname, mtime=mtime,
       
    35                 pwd=dbhelper.binary_value(crypt_password('toto')))
       
    36     print args
       
    37     sql(insert, args)
       
    38     sql(update, args)
       
    39 
       
    40 commit()