repoapi.py
changeset 9062 fa5bc8aef7ed
parent 9061 e86fdab3d296
child 9096 9d1b5f07d9ca
equal deleted inserted replaced
9061:e86fdab3d296 9062:fa5bc8aef7ed
    16 # You should have received a copy of the GNU Lesser General Public License along
    16 # You should have received a copy of the GNU Lesser General Public License along
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    18 """Official API to access the content of a repository
    18 """Official API to access the content of a repository
    19 """
    19 """
    20 from cubicweb.utils import parse_repo_uri
    20 from cubicweb.utils import parse_repo_uri
    21 from cubicweb import ConnectionError, ProgrammingError
    21 from cubicweb import ConnectionError, ProgrammingError, AuthenticationError
    22 from uuid import uuid4
    22 from uuid import uuid4
    23 from contextlib import contextmanager
    23 from contextlib import contextmanager
    24 from cubicweb.req import RequestSessionBase
    24 from cubicweb.req import RequestSessionBase
    25 
    25 
    26 ### private function for specific method ############################
    26 ### private function for specific method ############################
    84     sessionid = repo.connect(login, **kwargs)
    84     sessionid = repo.connect(login, **kwargs)
    85     session = repo._get_session(sessionid)
    85     session = repo._get_session(sessionid)
    86     # XXX the autoclose_session should probably be handle on the session directly
    86     # XXX the autoclose_session should probably be handle on the session directly
    87     # this is something to consider once we have proper server side Connection.
    87     # this is something to consider once we have proper server side Connection.
    88     return ClientConnection(session, autoclose_session=True)
    88     return ClientConnection(session, autoclose_session=True)
       
    89 
       
    90 def anonymous_cnx(repo):
       
    91     """return a ClientConnection for Anonymous user.
       
    92 
       
    93     The ClientConnection is associated to a new Session object that will be
       
    94     closed when the ClientConnection is closed.
       
    95 
       
    96     raises an AuthenticationError if anonymous usage is not allowed
       
    97     """
       
    98     anoninfo = getattr(repo.config, 'anonymous_user', lambda: None)()
       
    99     if anoninfo is None: # no anonymous user
       
   100         raise AuthenticationError('anonymous access is not authorized')
       
   101     anon_login, anon_password = anoninfo
       
   102     # use vreg's repository cache
       
   103     return connect(repo, anon_login, password=anon_password)
    89 
   104 
    90 def _srv_cnx_func(name):
   105 def _srv_cnx_func(name):
    91     """Decorate ClientConnection method blindly forward to Connection
   106     """Decorate ClientConnection method blindly forward to Connection
    92     THIS TRANSITIONAL PURPOSE
   107     THIS TRANSITIONAL PURPOSE
    93 
   108