misc/scripts/cwuser_ldap2system.py
author Julien Cristau <julien.cristau@logilab.fr>
Wed, 14 Oct 2015 16:18:25 +0200
changeset 10805 de4403644b1c
parent 10589 7c23b7de2b8d
permissions -rw-r--r--
[server/datafeed] drop URLLibResponseAdapter.info I don't know where it's used
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 9460
diff changeset
     1
from __future__ import print_function
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 9460
diff changeset
     2
6387
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
     3
import base64
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
     4
from cubicweb.server.utils import crypt_password
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
     5
9460
a2a0bc984863 [config] cleanup/refactor server sources file values handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6387
diff changeset
     6
dbdriver  = config.system_source_config['db-driver']
6387
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
     7
from logilab.database import get_db_helper
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
     8
dbhelper = get_db_helper(driver)
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
     9
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    10
insert = ('INSERT INTO cw_cwuser (cw_creation_date,'
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    11
          '                       cw_eid,'
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    12
          '                       cw_modification_date,'
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    13
          '                       cw_login,'
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    14
          '                       cw_firstname,'
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    15
          '                       cw_surname,'
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    16
          '                       cw_last_login_time,' 
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    17
          '                       cw_upassword,'
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    18
          '                       cw_cwuri) '
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    19
          "VALUES (%(mtime)s, %(eid)s, %(mtime)s, %(login)s, "
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    20
          "        %(firstname)s, %(surname)s, %(mtime)s, %(pwd)s, 'foo');")
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    21
update = "UPDATE entities SET source='system' WHERE eid=%(eid)s;"
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    22
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
    23
for eid, type, source, extid, mtime in rset:
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    24
    if type != 'CWUser':
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 9460
diff changeset
    25
        print("don't know what to do with entity type", type)
6387
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    26
        continue
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    27
    if not source.lower().startswith('ldap'):
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 9460
diff changeset
    28
        print("don't know what to do with source type", source)
6387
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    29
        continue
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    30
    extid = base64.decodestring(extid)
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    31
    ldapinfos = [x.strip().split('=') for x in extid.split(',')]
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    32
    login = ldapinfos[0][1]
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    33
    firstname = login.capitalize()
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    34
    surname = login.capitalize()
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    35
    args = dict(eid=eid, type=type, source=source, login=login,
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    36
                firstname=firstname, surname=surname, mtime=mtime,
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    37
                pwd=dbhelper.binary_value(crypt_password('toto')))
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 9460
diff changeset
    38
    print(args)
6387
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    39
    sql(insert, args)
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    40
    sql(update, args)
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    41
edbc53707bac new script cwuser_ldap2system
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    42
commit()