cubicweb/misc/scripts/cwuser_ldap2system.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Sat, 16 Jan 2016 13:48:51 +0100
changeset 11057 0b59724cb3f2
parent 10589 misc/scripts/cwuser_ldap2system.py@7c23b7de2b8d
permissions -rw-r--r--
Reorganize source tree to have a "cubicweb" top-level package Basically: mkdir cubicweb hg mv *.py -X setup.py cubicweb hg mv dataimport devtools entities etwist ext hooks i18n misc schemas server skeleton sobjects test web wsgi cubicweb Other changes: * adjust path to cubicweb-ctl in devtools tests * update setup.py to avoid importing __pkginfo__ (exec it instead), replace os.path.walk by os.walk and prepend `modname` here and there * update tox.ini to account for new test locations * update doc/conf.py so that it still finds __pkginfo__.py and CWDIR in doc/Makefile

from __future__ import print_function

import base64
from cubicweb.server.utils import crypt_password

dbdriver  = config.system_source_config['db-driver']
from logilab.database import get_db_helper
dbhelper = get_db_helper(driver)

insert = ('INSERT INTO cw_cwuser (cw_creation_date,'
          '                       cw_eid,'
          '                       cw_modification_date,'
          '                       cw_login,'
          '                       cw_firstname,'
          '                       cw_surname,'
          '                       cw_last_login_time,' 
          '                       cw_upassword,'
          '                       cw_cwuri) '
          "VALUES (%(mtime)s, %(eid)s, %(mtime)s, %(login)s, "
          "        %(firstname)s, %(surname)s, %(mtime)s, %(pwd)s, 'foo');")
update = "UPDATE entities SET source='system' WHERE eid=%(eid)s;"
rset = sql("SELECT eid,type,source,extid,mtime FROM entities WHERE source!='system'", ask_confirm=False)
for eid, type, source, extid, mtime in rset:
    if type != 'CWUser':
        print("don't know what to do with entity type", type)
        continue
    if not source.lower().startswith('ldap'):
        print("don't know what to do with source type", source)
        continue
    extid = base64.decodestring(extid)
    ldapinfos = [x.strip().split('=') for x in extid.split(',')]
    login = ldapinfos[0][1]
    firstname = login.capitalize()
    surname = login.capitalize()
    args = dict(eid=eid, type=type, source=source, login=login,
                firstname=firstname, surname=surname, mtime=mtime,
                pwd=dbhelper.binary_value(crypt_password('toto')))
    print(args)
    sql(insert, args)
    sql(update, args)

commit()