misc/scripts/cwuser_ldap2system.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 21 Jan 2013 18:01:25 +0100
changeset 8669 62213a34726e
parent 6387 edbc53707bac
child 9460 a2a0bc984863
permissions -rw-r--r--
[db-api/configuration] simplify db-api and configuration so that all the connection information is in the repository url, closes #2521848 eg no more specific option of pyro ns host, group, etc. This also fixes broken ZMQ sources. Changes: * dropped pyro-ns-host, pyro-instance-id, pyro-ns-group from client side config, in favor of repository-uri. No migration done, supposing there is **no web-only config** in the wild. Also stop discovering the connection method through the repo_method class attribute of the configuration, varying according to the configuration class. This is a first step on the way to a simpler configuration handling. Notice those pyro options are still available for repository only / all-in-one configurations as they are needed to configure the pyro server. * stop telling connection method using ConnectionProperties, this is so boring. Also, drop _cnxtype from Connection and cnxtype from Session. The former is replaced by a is_repo_in_memory property and the later is totaly useless. * deprecate in_memory_cnx which becomes useless, use _repo_connect instead
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6387
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
     1
import base64
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
     2
from cubicweb.server.utils import crypt_password
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
     3
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
     4
dbdriver  = config.sources()['system']['db-driver']
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
     5
from logilab.database import get_db_helper
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
     6
dbhelper = get_db_helper(driver)
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
     7
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
     8
insert = ('INSERT INTO cw_cwuser (cw_creation_date,'
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
     9
          '                       cw_eid,'
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    10
          '                       cw_modification_date,'
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    11
          '                       cw_login,'
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    12
          '                       cw_firstname,'
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    13
          '                       cw_surname,'
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    14
          '                       cw_last_login_time,' 
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    15
          '                       cw_upassword,'
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    16
          '                       cw_cwuri) '
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    17
          "VALUES (%(mtime)s, %(eid)s, %(mtime)s, %(login)s, "
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    18
          "        %(firstname)s, %(surname)s, %(mtime)s, %(pwd)s, 'foo');")
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    19
update = "UPDATE entities SET source='system' WHERE eid=%(eid)s;"
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    20
rset = sql("SELECT eid,type,source,extid,mtime FROM entities WHERE source!='system'", ask_confirm=False)
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    21
for eid, type, source, extid, mtime in rset:
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    22
    if type != 'CWUser':
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    23
        print "don't know what to do with entity type", type
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    24
        continue
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    25
    if not source.lower().startswith('ldap'):
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    26
        print "don't know what to do with source type", source
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    27
        continue
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    28
    extid = base64.decodestring(extid)
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    29
    ldapinfos = [x.strip().split('=') for x in extid.split(',')]
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    30
    login = ldapinfos[0][1]
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    31
    firstname = login.capitalize()
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    32
    surname = login.capitalize()
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    33
    args = dict(eid=eid, type=type, source=source, login=login,
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    34
                firstname=firstname, surname=surname, mtime=mtime,
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    35
                pwd=dbhelper.binary_value(crypt_password('toto')))
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    36
    print args
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    37
    sql(insert, args)
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    38
    sql(update, args)
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    39
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    40
commit()