backport stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 11 Jul 2011 09:21:44 +0200
changeset 7642 64eee2a83bfa
parent 7639 5c9e94a09e97 (current diff)
parent 7641 790038f88b8b (diff)
child 7643 f3e3892fc7e3
backport stable
schema.py
server/sources/datafeed.py
server/sources/rql2sql.py
server/test/unittest_rql2sql.py
test/unittest_schema.py
--- a/schema.py	Mon Jul 11 09:15:54 2011 +0200
+++ b/schema.py	Mon Jul 11 09:21:44 2011 +0200
@@ -1012,6 +1012,9 @@
         else:
             expression = 'S eid %(s)s, O eid %(o)s, ' + self.expression
             args = {'s': eidfrom, 'o': eidto}
+        if 'U' in self.rqlst.defined_vars:
+            expression = 'U eid %(u)s, ' + expression
+            args['u'] = session.user.eid
         rql = 'Any %s WHERE %s' % (','.join(sorted(self.mainvars)), expression)
         if self.distinct_query:
             rql = 'DISTINCT ' + rql
--- a/server/sources/datafeed.py	Mon Jul 11 09:15:54 2011 +0200
+++ b/server/sources/datafeed.py	Mon Jul 11 09:21:44 2011 +0200
@@ -66,7 +66,8 @@
         )
     def __init__(self, repo, source_config, eid=None):
         AbstractSource.__init__(self, repo, source_config, eid)
-        self.update_config(None, self.check_conf_dict(eid, source_config))
+        self.update_config(None, self.check_conf_dict(eid, source_config,
+                                                      fail_if_unknown=False))
 
     def check_config(self, source_entity):
         """check configuration of source entity"""
--- a/server/sources/rql2sql.py	Mon Jul 11 09:15:54 2011 +0200
+++ b/server/sources/rql2sql.py	Mon Jul 11 09:21:44 2011 +0200
@@ -1251,6 +1251,8 @@
                         return condition
                     self._state.add_outer_join_condition(leftalias, condition)
                 return
+        if leftalias is None:
+            leftalias = leftvar._q_sql.split('.', 1)[0]
         self._state.replace_tables_by_outer_join(
             leftalias, rightalias, outertype, '%s=%s' % (lhssql, rhs.accept(self)))
         return ''
--- a/server/test/unittest_rql2sql.py	Mon Jul 11 09:15:54 2011 +0200
+++ b/server/test/unittest_rql2sql.py	Mon Jul 11 09:21:44 2011 +0200
@@ -958,6 +958,12 @@
      '''SELECT rel_tags0.eid_from, rel_tags0.eid_to, rel_tags1.eid_from
 FROM tags_relation AS rel_tags0 LEFT OUTER JOIN tags_relation AS rel_tags1 ON (rel_tags1.eid_to=123)
 WHERE rel_tags0.eid_from=123'''),
+
+    ('Any CASE, CALIBCFG, CFG '
+     'WHERE CASE eid 1, CFG ecrit_par CASE, CALIBCFG? ecrit_par CASE',
+     '''SELECT _CFG.cw_ecrit_par, _CALIBCFG.cw_eid, _CFG.cw_eid
+FROM cw_Note AS _CFG LEFT OUTER JOIN cw_Note AS _CALIBCFG ON (_CALIBCFG.cw_ecrit_par=_CFG.cw_ecrit_par)
+WHERE _CFG.cw_ecrit_par=1'''),
     ]
 
 VIRTUAL_VARS = [
--- a/test/unittest_schema.py	Mon Jul 11 09:15:54 2011 +0200
+++ b/test/unittest_schema.py	Mon Jul 11 09:21:44 2011 +0200
@@ -26,7 +26,7 @@
 
 from rql import RQLSyntaxError
 
-from yams import BadSchemaDefinition
+from yams import ValidationError, BadSchemaDefinition
 from yams.constraints import SizeConstraint, StaticVocabularyConstraint
 from yams.buildobjs import RelationDefinition, EntityType, RelationType
 from yams.reader import fill_schema
@@ -37,6 +37,7 @@
     RQLExpression, ERQLExpression, RRQLExpression,
     normalize_expression, order_eschemas, guess_rrqlexpr_mainvars)
 from cubicweb.devtools import TestServerConfiguration as TestConfiguration
+from cubicweb.devtools.testlib import CubicWebTC
 
 DATADIR = join(dirname(__file__), 'data')
 
@@ -324,5 +325,14 @@
         self.assertEqual(mainvars, set(['S', 'O']))
 
 
+class RQLConstraintTC(CubicWebTC):
+    def test_user_constraint(self):
+        cstr = RQLConstraint('U identity O')
+        anoneid = self.execute('Any X WHERE X login "anon"')[0][0]
+        self.assertRaises(ValidationError, cstr.repo_check, self.session, 1, 'rel', anoneid)
+        self.assertEqual(cstr.repo_check(self.session, 1, self.session.user.eid),
+        None) # no validation error, constraint checked
+
+
 if __name__ == '__main__':
     unittest_main()