server/repository.py
branchtls-sprint
changeset 1398 5fe84a5f7035
parent 1372 d4264cd876e1
child 1482 93c613913912
equal deleted inserted replaced
1397:6cbc7bc8ea6d 1398:5fe84a5f7035
   178         elif config.bootstrap_schema:
   178         elif config.bootstrap_schema:
   179             # usually during repository creation
   179             # usually during repository creation
   180             self.warning("set fs application'schema as bootstrap schema")
   180             self.warning("set fs application'schema as bootstrap schema")
   181             config.bootstrap_cubes()
   181             config.bootstrap_cubes()
   182             self.set_bootstrap_schema(self.config.load_schema())
   182             self.set_bootstrap_schema(self.config.load_schema())
   183             # need to load the Any and EUser entity types
   183             # need to load the Any and CWUser entity types
   184             self.vreg.schema = self.schema
   184             self.vreg.schema = self.schema
   185             etdirectory = join(CW_SOFTWARE_ROOT, 'entities')
   185             etdirectory = join(CW_SOFTWARE_ROOT, 'entities')
   186             self.vreg.init_registration([etdirectory])
   186             self.vreg.init_registration([etdirectory])
   187             self.vreg.load_file(join(etdirectory, '__init__.py'),
   187             self.vreg.load_file(join(etdirectory, '__init__.py'),
   188                                 'cubicweb.entities.__init__')
   188                                 'cubicweb.entities.__init__')
   192             # test start: use the file system schema (quicker)
   192             # test start: use the file system schema (quicker)
   193             self.warning("set fs application'schema")
   193             self.warning("set fs application'schema")
   194             config.bootstrap_cubes()
   194             config.bootstrap_cubes()
   195             self.set_schema(self.config.load_schema())
   195             self.set_schema(self.config.load_schema())
   196         if not config.creating:
   196         if not config.creating:
   197             if 'EProperty' in self.schema:
   197             if 'CWProperty' in self.schema:
   198                 self.vreg.init_properties(self.properties())
   198                 self.vreg.init_properties(self.properties())
   199             # call source's init method to complete their initialisation if
   199             # call source's init method to complete their initialisation if
   200             # needed (for instance looking for persistent configuration using an
   200             # needed (for instance looking for persistent configuration using an
   201             # internal session, which is not possible until pools have been
   201             # internal session, which is not possible until pools have been
   202             # initialized)
   202             # initialized)
   255             try:
   255             try:
   256                 deserialize_schema(appschema, session)
   256                 deserialize_schema(appschema, session)
   257             except BadSchemaDefinition:
   257             except BadSchemaDefinition:
   258                 raise
   258                 raise
   259             except Exception, ex:
   259             except Exception, ex:
       
   260                 import traceback
       
   261                 traceback.print_exc()
   260                 raise Exception('Is the database initialised ? (cause: %s)' % 
   262                 raise Exception('Is the database initialised ? (cause: %s)' % 
   261                                 (ex.args and ex.args[0].strip() or 'unknown')), \
   263                                 (ex.args and ex.args[0].strip() or 'unknown')), \
   262                                 None, sys.exc_info()[-1]
   264                                 None, sys.exc_info()[-1]
   263             self.info('set the actual schema')
   265             self.info('set the actual schema')
   264             # XXX have to do this since EProperty isn't in the bootstrap schema
   266             # XXX have to do this since CWProperty isn't in the bootstrap schema
   265             #     it'll be redone in set_schema
   267             #     it'll be redone in set_schema
   266             self.set_bootstrap_schema(appschema)
   268             self.set_bootstrap_schema(appschema)
   267             # 2.49 migration
   269             # 2.49 migration
   268             if exists(join(self.config.apphome, 'vc.conf')):
   270             if exists(join(self.config.apphome, 'vc.conf')):
   269                 session.set_pool()
   271                 session.set_pool()
   270                 if not 'template' in file(join(self.config.apphome, 'vc.conf')).read():
   272                 if not 'template' in file(join(self.config.apphome, 'vc.conf')).read():
   271                     # remaning from cubicweb < 2.38...
   273                     # remaning from cubicweb < 2.38...
   272                     session.execute('DELETE EProperty X WHERE X pkey "system.version.template"')
   274                     session.execute('DELETE CWProperty X WHERE X pkey "system.version.template"')
   273                     session.commit()
   275                     session.commit()
   274         finally:
   276         finally:
   275             session.close()
   277             session.close()
   276         self.config.init_cubes(self.get_cubes())
   278         self.config.init_cubes(self.get_cubes())
   277         self.set_schema(appschema)
   279         self.set_schema(appschema)
   381         except ZeroDivisionError:
   383         except ZeroDivisionError:
   382             pass
   384             pass
   383         
   385         
   384     def authenticate_user(self, session, login, password):
   386     def authenticate_user(self, session, login, password):
   385         """validate login / password, raise AuthenticationError on failure
   387         """validate login / password, raise AuthenticationError on failure
   386         return associated EUser instance on success
   388         return associated CWUser instance on success
   387         """
   389         """
   388         for source in self.sources:
   390         for source in self.sources:
   389             if source.support_entity('EUser'):
   391             if source.support_entity('CWUser'):
   390                 try:
   392                 try:
   391                     eid = source.authenticate(session, login, password)
   393                     eid = source.authenticate(session, login, password)
   392                     break
   394                     break
   393                 except AuthenticationError:
   395                 except AuthenticationError:
   394                     continue
   396                     continue
   399                not euser.state in euser.AUTHENTICABLE_STATES:
   401                not euser.state in euser.AUTHENTICABLE_STATES:
   400             raise AuthenticationError('user is not in authenticable state')
   402             raise AuthenticationError('user is not in authenticable state')
   401         return euser
   403         return euser
   402 
   404 
   403     def _build_user(self, session, eid):
   405     def _build_user(self, session, eid):
   404         """return a EUser entity for user with the given eid"""
   406         """return a CWUser entity for user with the given eid"""
   405         cls = self.vreg.etype_class('EUser')
   407         cls = self.vreg.etype_class('CWUser')
   406         rql = cls.fetch_rql(session.user, ['X eid %(x)s'])
   408         rql = cls.fetch_rql(session.user, ['X eid %(x)s'])
   407         rset = session.execute(rql, {'x': eid}, 'x')
   409         rset = session.execute(rql, {'x': eid}, 'x')
   408         assert len(rset) == 1, rset
   410         assert len(rset) == 1, rset
   409         euser = rset.get_entity(0, 0)
   411         euser = rset.get_entity(0, 0)
   410         # pylint: disable-msg=W0104
   412         # pylint: disable-msg=W0104
   445         from logilab.common.changelog import Version
   447         from logilab.common.changelog import Version
   446         vcconf = {}
   448         vcconf = {}
   447         session = self.internal_session()
   449         session = self.internal_session()
   448         try:
   450         try:
   449             for pk, version in session.execute(
   451             for pk, version in session.execute(
   450                 'Any K,V WHERE P is EProperty, P value V, P pkey K, '
   452                 'Any K,V WHERE P is CWProperty, P value V, P pkey K, '
   451                 'P pkey ~="system.version.%"', build_descr=False):
   453                 'P pkey ~="system.version.%"', build_descr=False):
   452                 cube = pk.split('.')[-1]
   454                 cube = pk.split('.')[-1]
   453                 # XXX cubicweb migration
   455                 # XXX cubicweb migration
   454                 if cube in CW_MIGRATION_MAP:
   456                 if cube in CW_MIGRATION_MAP:
   455                     cube = CW_MIGRATION_MAP[cube]
   457                     cube = CW_MIGRATION_MAP[cube]
   482 
   484 
   483     def properties(self):
   485     def properties(self):
   484         """return a result set containing system wide properties"""
   486         """return a result set containing system wide properties"""
   485         session = self.internal_session()
   487         session = self.internal_session()
   486         try:
   488         try:
   487             return session.execute('Any K,V WHERE P is EProperty,'
   489             return session.execute('Any K,V WHERE P is CWProperty,'
   488                                    'P pkey K, P value V, NOT P for_user U',
   490                                    'P pkey K, P value V, NOT P for_user U',
   489                                    build_descr=False)
   491                                    build_descr=False)
   490         finally:
   492         finally:
   491             session.close()
   493             session.close()
   492 
   494 
   497         """
   499         """
   498         session = self.internal_session()
   500         session = self.internal_session()
   499         # for consistency, keep same error as unique check hook (although not required)
   501         # for consistency, keep same error as unique check hook (although not required)
   500         errmsg = session._('the value "%s" is already used, use another one')
   502         errmsg = session._('the value "%s" is already used, use another one')
   501         try:
   503         try:
   502             if (session.execute('EUser X WHERE X login %(login)s', {'login': login})
   504             if (session.execute('CWUser X WHERE X login %(login)s', {'login': login})
   503                 or session.execute('EUser X WHERE X use_email C, C address %(login)s',
   505                 or session.execute('CWUser X WHERE X use_email C, C address %(login)s',
   504                                    {'login': login})):
   506                                    {'login': login})):
   505                 raise ValidationError(None, {'login': errmsg % login})
   507                 raise ValidationError(None, {'login': errmsg % login})
   506             # we have to create the user
   508             # we have to create the user
   507             user = self.vreg.etype_class('EUser')(session, None)
   509             user = self.vreg.etype_class('CWUser')(session, None)
   508             if isinstance(password, unicode):
   510             if isinstance(password, unicode):
   509                 # password should *always* be utf8 encoded
   511                 # password should *always* be utf8 encoded
   510                 password = password.encode('UTF8')
   512                 password = password.encode('UTF8')
   511             kwargs['login'] = login
   513             kwargs['login'] = login
   512             kwargs['upassword'] = password
   514             kwargs['upassword'] = password