added a function to create admin/anon user during db initialization process so one get a chance to monkey patch it stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 17 Feb 2010 13:23:36 +0100
branchstable
changeset 4612 d6ae30c5d055
parent 4611 599189430c0e
child 4613 141a4f613f8a
added a function to create admin/anon user during db initialization process so one get a chance to monkey patch it
misc/migration/postcreate.py
server/__init__.py
--- a/misc/migration/postcreate.py	Wed Feb 17 13:16:50 2010 +0100
+++ b/misc/migration/postcreate.py	Wed Feb 17 13:23:36 2010 +0100
@@ -5,7 +5,6 @@
 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
 """
-
 # insert versions
 create_entity('CWProperty', pkey=u'system.version.cubicweb',
               value=unicode(config.cubicweb_version()))
@@ -38,9 +37,8 @@
         print 'you are using a manager account as anonymous user.'
         print 'Hopefully this is not a production instance...'
     elif anonlogin:
-        rql('INSERT CWUser X: X login %(login)s, X upassword %(pwd)s,'
-            'X in_group G WHERE G name "guests"',
-            {'login': unicode(anonlogin), 'pwd': anonpwd})
+        from cubicweb.server import create_user
+        create_user(session, unicode(anonlogin), anonpwd, 'guests')
 
 # need this since we already have at least one user in the database (the default admin)
 for user in rql('Any X WHERE X is CWUser').entities():
--- a/server/__init__.py	Wed Feb 17 13:16:50 2010 +0100
+++ b/server/__init__.py	Wed Feb 17 13:23:36 2010 +0100
@@ -93,6 +93,14 @@
 
 # database initialization ######################################################
 
+def create_user(session, login, pwd, *groups):
+    # monkey patch this method if you want to customize admin/anon creation
+    # (that maybe necessary if you change CWUser's schema)
+    session.create_entity('CWUser', login=login, upassword=pwd)
+    for group in groups:
+        session.execute('SET U in_group G WHERE G name %(group)s',
+                        {'group': group})
+
 def init_repository(config, interactive=True, drop=False, vreg=None):
     """initialise a repository database by creating tables add filling them
     with the minimal set of entities (ie at least the schema, base groups and
@@ -161,9 +169,7 @@
     for group in sorted(BASE_GROUPS):
         session.execute('INSERT CWGroup X: X name %(name)s',
                         {'name': unicode(group)})
-    session.execute('INSERT CWUser X: X login %(login)s, X upassword %(pwd)s',
-                    {'login': login, 'pwd': pwd})
-    session.execute('SET U in_group G WHERE G name "managers"')
+    create_user(session, login, pwd, 'managers')
     session.commit()
     # reloging using the admin user
     config._cubes = None # avoid assertion error