misc/scripts/cwuser_ldap2system.py
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Tue, 27 May 2014 18:47:24 +0200
changeset 10087 ed0b076c119b
parent 9460 a2a0bc984863
child 10589 7c23b7de2b8d
permissions -rw-r--r--
[rset] kill the rset._rqlst cache Right now it "works" for the standard, internal uses. However when we will fold ClientConnection and Connection, it will hurt, because suddenly we get more cache hits, and the following situation would become commonplace: * there is an un-annotated _rqlst given by the querier * some view (e.g. facets) requests the .syntax_tree, which takes a copy of _rqlst * the view actually expects the rql syntax tree to be annotated, but it was not, hence we crash. Related to #3837233.

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()