doc/book/en/annexes/cookbook.rst
branchtls-sprint
changeset 1714 a721966779be
parent 1477 b056a49c16dc
child 1808 aa09e20dd8c0
equal deleted inserted replaced
1499:fd8751c3f3ee 1714:a721966779be
       
     1 .. -*- coding: utf-8 -*-
       
     2 
       
     3 Cook book
       
     4 =========
       
     5 
       
     6 We gathered together some of our tricks and scripts that could make
       
     7 life easier.
       
     8 
       
     9 
       
    10 * How to import LDAP users in `CubicWeb`?
       
    11 
       
    12   Here is a very useful script which enables you to import LDAP users
       
    13   into your `CubicWeb` application by running the following: ::
       
    14 
       
    15 
       
    16     import os
       
    17     import pwd
       
    18     import sys
       
    19 
       
    20     from logilab.common.db import get_connection
       
    21 
       
    22     def getlogin():
       
    23         """avoid usinng os.getlogin() because of strange tty / stdin problems
       
    24         (man 3 getlogin)
       
    25         Another solution would be to use $LOGNAME, $USER or $USERNAME
       
    26         """
       
    27         return pwd.getpwuid(os.getuid())[0]
       
    28 
       
    29 
       
    30     try:
       
    31         database = sys.argv[1]
       
    32     except IndexError:
       
    33         print 'USAGE: python ldap2system.py <database>'
       
    34         sys.exit(1)
       
    35 
       
    36     if raw_input('update %s db ? [y/n]: ' % database).strip().lower().startswith('y'):
       
    37         cnx = get_connection(user=getlogin(), database=database)
       
    38         cursor = cnx.cursor()
       
    39 
       
    40         insert = ('INSERT INTO euser (creation_date, eid, modification_date, login, firstname, surname, last_login_time, upassword) '
       
    41                   "VALUES (%(mtime)s, %(eid)s, %(mtime)s, %(login)s, %(firstname)s, %(surname)s, %(mtime)s, './fqEz5LeZnT6');")
       
    42         update = "UPDATE entities SET source='system' WHERE eid=%(eid)s;"
       
    43         cursor.execute("SELECT eid,type,source,extid,mtime FROM entities WHERE source!='system'")
       
    44         for eid, type, source, extid, mtime in cursor.fetchall():
       
    45             if type != 'CWUser':
       
    46                 print "don't know what to do with entity type", type
       
    47                 continue
       
    48             if source != 'ldapuser':
       
    49                 print "don't know what to do with source type", source
       
    50                 continue
       
    51             ldapinfos = dict(x.strip().split('=') for x in extid.split(','))
       
    52             login = ldapinfos['uid']
       
    53             firstname = ldapinfos['uid'][0].upper()
       
    54             surname = ldapinfos['uid'][1:].capitalize()
       
    55             if login != 'jcuissinat':
       
    56                 args = dict(eid=eid, type=type, source=source, login=login,
       
    57                             firstname=firstname, surname=surname, mtime=mtime)
       
    58                 print args
       
    59                 cursor.execute(insert, args)
       
    60                 cursor.execute(update, args)
       
    61 
       
    62         cnx.commit()
       
    63         cnx.close()
       
    64