[repo] Fix register_user
authorJulien Cristau <julien.cristau@logilab.fr>
Wed, 09 Apr 2014 18:14:45 +0200
changeset 9676 3f62606c01a2
parent 9675 8aabfefc8a81
child 9677 f0130c270793
[repo] Fix register_user Make sure we have a connection to the db around user creation.
server/repository.py
--- a/server/repository.py	Tue Apr 08 18:00:14 2014 +0200
+++ b/server/repository.py	Wed Apr 09 18:14:45 2014 +0200
@@ -620,34 +620,35 @@
         registration on public web site.
         """
         with self.internal_cnx() as cnx:
-            # for consistency, keep same error as unique check hook (although not required)
-            errmsg = cnx._('the value "%s" is already used, use another one')
-            if (cnx.execute('CWUser X WHERE X login %(login)s', {'login': login},
-                            build_descr=False)
-                or cnx.execute('CWUser X WHERE X use_email C, C address %(login)s',
-                               {'login': login}, build_descr=False)):
-                qname = role_name('login', 'subject')
-                raise ValidationError(None, {qname: errmsg % login})
-            # we have to create the user
-            user = self.vreg['etypes'].etype_class('CWUser')(cnx)
-            if isinstance(password, unicode):
-                # password should *always* be utf8 encoded
-                password = password.encode('UTF8')
-            kwargs['login'] = login
-            kwargs['upassword'] = password
-            self.glob_add_entity(cnx, EditedEntity(user, **kwargs))
-            cnx.execute('SET X in_group G WHERE X eid %(x)s, G name "users"',
-                        {'x': user.eid})
-            if email or '@' in login:
-                d = {'login': login, 'email': email or login}
-                if cnx.execute('EmailAddress X WHERE X address %(email)s', d,
-                               build_descr=False):
-                    qname = role_name('address', 'subject')
-                    raise ValidationError(None, {qname: errmsg % d['email']})
-                cnx.execute('INSERT EmailAddress X: X address %(email)s, '
-                            'U primary_email X, U use_email X '
-                            'WHERE U login %(login)s', d, build_descr=False)
-            cnx.commit()
+            with cnx.ensure_cnx_set:
+                # for consistency, keep same error as unique check hook (although not required)
+                errmsg = cnx._('the value "%s" is already used, use another one')
+                if (cnx.execute('CWUser X WHERE X login %(login)s', {'login': login},
+                                build_descr=False)
+                    or cnx.execute('CWUser X WHERE X use_email C, C address %(login)s',
+                                   {'login': login}, build_descr=False)):
+                    qname = role_name('login', 'subject')
+                    raise ValidationError(None, {qname: errmsg % login})
+                # we have to create the user
+                user = self.vreg['etypes'].etype_class('CWUser')(cnx)
+                if isinstance(password, unicode):
+                    # password should *always* be utf8 encoded
+                    password = password.encode('UTF8')
+                kwargs['login'] = login
+                kwargs['upassword'] = password
+                self.glob_add_entity(cnx, EditedEntity(user, **kwargs))
+                cnx.execute('SET X in_group G WHERE X eid %(x)s, G name "users"',
+                            {'x': user.eid})
+                if email or '@' in login:
+                    d = {'login': login, 'email': email or login}
+                    if cnx.execute('EmailAddress X WHERE X address %(email)s', d,
+                                   build_descr=False):
+                        qname = role_name('address', 'subject')
+                        raise ValidationError(None, {qname: errmsg % d['email']})
+                    cnx.execute('INSERT EmailAddress X: X address %(email)s, '
+                                'U primary_email X, U use_email X '
+                                'WHERE U login %(login)s', d, build_descr=False)
+                cnx.commit()
         return True
 
     def find_users(self, fetch_attrs, **query_attrs):