[dbapi] makes anonymous_connection a computed property
authorPierre-Yves David <pierre-yves.david@logilab.fr>
Mon, 24 Jun 2013 14:53:19 +0200
changeset 9042 6cc13a0a9145
parent 9041 753a159c8822
child 9043 97c3bb9a7c99
[dbapi] makes anonymous_connection a computed property The current implementation is a boolean flag set manually by client code after connection creation. This led to different way to decide a anonymous_connection should be True (eg. different in the test than in the actual application code). It should not be client responsibility to set this flag. ``cnx.anonymous_connection`` is now a purely computed property. Connection with user in the "guests" group are anonymous, the other ain't. ``Session.anonymous_session`` is computed from ``cnx.anonymous_connection`` and get the updated behavior transparently. Closes #2953943
dbapi.py
devtools/testlib.py
doc/4.0.rst
web/test/unittest_views_basetemplates.py
web/views/authentication.py
--- a/dbapi.py	Mon Jun 24 11:31:47 2013 +0200
+++ b/dbapi.py	Mon Jun 24 14:53:19 2013 +0200
@@ -34,7 +34,7 @@
 from urlparse import  urlparse
 
 from logilab.common.logging_ext import set_log_methods
-from logilab.common.decorators import monkeypatch
+from logilab.common.decorators import monkeypatch, cachedproperty
 from logilab.common.deprecation import deprecated
 
 from cubicweb import ETYPE_NAME_MAP, ConnectionError, AuthenticationError,\
@@ -541,7 +541,6 @@
     # make exceptions available through the connection object
     ProgrammingError = ProgrammingError
     # attributes that may be overriden per connection instance
-    anonymous_connection = False
     cursor_class = Cursor
     vreg = None
     _closed = None
@@ -567,6 +566,13 @@
             return False
         return isinstance(self._repo, Repository)
 
+    @property # could be a cached property but we want to prevent assigment to
+              # catch potential programming error.
+    def anonymous_connection(self):
+        login = self._repo.user_info(self.sessionid)[1]
+        anon_login = self.vreg.config.get('anonymous-user')
+        return login == anon_login
+
     def __repr__(self):
         if self.anonymous_connection:
             return '<Connection %s (anonymous)>' % self.sessionid
--- a/devtools/testlib.py	Mon Jun 24 11:31:47 2013 +0200
+++ b/devtools/testlib.py	Mon Jun 24 14:53:19 2013 +0200
@@ -401,8 +401,6 @@
             kwargs['password'] = str(login)
         self.set_cnx(dbapi._repo_connect(self.repo, unicode(login), **kwargs))
         self.websession = dbapi.DBAPISession(self.cnx)
-        if login == self.vreg.config.anonymous_user()[0]:
-            self.cnx.anonymous_connection = True
         if autoclose:
             return TestCaseConnectionProxy(self, self.cnx)
         return self.cnx
--- a/doc/4.0.rst	Mon Jun 24 11:31:47 2013 +0200
+++ b/doc/4.0.rst	Mon Jun 24 14:53:19 2013 +0200
@@ -1,6 +1,14 @@
 What's new in CubicWeb 4.0?
 ============================
 
+Behavior Changes
+----------------
+
+* The anonymous property of Session and Connection are now computed from the
+  related user login. If it match the ``anonymous-user`` in the config the
+  connection is anonymous. Beware that the ``anonymous-user`` config is web
+  specific. Therefore, no session may be anonymous in repository only setup.
+
 API changes
 -----------
 
--- a/web/test/unittest_views_basetemplates.py	Mon Jun 24 11:31:47 2013 +0200
+++ b/web/test/unittest_views_basetemplates.py	Mon Jun 24 14:53:19 2013 +0200
@@ -18,16 +18,15 @@
 
 from cubicweb.devtools.testlib import CubicWebTC
 from cubicweb.devtools.htmlparser import XMLValidator
+from cubicweb.dbapi import DBAPISession
 
 
 class LogFormTemplateTC(CubicWebTC):
 
     def _login_labels(self):
         valid = self.content_type_validators.get('text/html', XMLValidator)()
-        req = self.request()
-        req.cnx.anonymous_connection = True
-        page = valid.parse_string(self.vreg['views'].main_template(self.request(), 'login'))
-        req.cnx.anonymous_connection = False
+        req = self.requestcls(self.vreg, url='login')
+        page = valid.parse_string(self.vreg['views'].main_template(req, 'login'))
         return page.find_tag('label')
 
     def test_label(self):
--- a/web/views/authentication.py	Mon Jun 24 11:31:47 2013 +0200
+++ b/web/views/authentication.py	Mon Jun 24 14:53:19 2013 +0200
@@ -163,7 +163,6 @@
         login, authinfo = self.anoninfo
         if login:
             cnx = self._authenticate(login, authinfo)
-            cnx.anonymous_connection = True
             return cnx, login
         raise AuthenticationError()