cubicweb/repoapi.py
changeset 11057 0b59724cb3f2
parent 10688 fa29f3628a1b
child 11195 5de859b95988
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
       
     1 # copyright 2013-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    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/>.
       
    18 """Official API to access the content of a repository
       
    19 """
       
    20 from warnings import warn
       
    21 
       
    22 from six import add_metaclass
       
    23 
       
    24 from logilab.common.deprecation import class_deprecated
       
    25 
       
    26 from cubicweb.utils import parse_repo_uri
       
    27 from cubicweb import AuthenticationError
       
    28 from cubicweb.server.session import Connection
       
    29 
       
    30 
       
    31 ### public API ######################################################
       
    32 
       
    33 def get_repository(uri=None, config=None, vreg=None):
       
    34     """get a repository for the given URI or config/vregistry (in case we're
       
    35     loading the repository for a client, eg web server, configuration).
       
    36 
       
    37     The returned repository may be an in-memory repository or a proxy object
       
    38     using a specific RPC method, depending on the given URI.
       
    39     """
       
    40     if uri is not None:
       
    41         warn('[3.22] get_repository only wants a config')
       
    42 
       
    43     assert config is not None, 'get_repository(config=config)'
       
    44     return config.repository(vreg)
       
    45 
       
    46 def connect(repo, login, **kwargs):
       
    47     """Take credential and return associated Connection.
       
    48 
       
    49     raise AuthenticationError if the credential are invalid."""
       
    50     sessionid = repo.connect(login, **kwargs)
       
    51     session = repo._get_session(sessionid)
       
    52     # XXX the autoclose_session should probably be handle on the session directly
       
    53     # this is something to consider once we have proper server side Connection.
       
    54     return Connection(session)
       
    55 
       
    56 def anonymous_cnx(repo):
       
    57     """return a Connection for Anonymous user.
       
    58 
       
    59     raises an AuthenticationError if anonymous usage is not allowed
       
    60     """
       
    61     anoninfo = getattr(repo.config, 'anonymous_user', lambda: None)()
       
    62     if anoninfo is None: # no anonymous user
       
    63         raise AuthenticationError('anonymous access is not authorized')
       
    64     anon_login, anon_password = anoninfo
       
    65     # use vreg's repository cache
       
    66     return connect(repo, anon_login, password=anon_password)
       
    67 
       
    68 
       
    69 @add_metaclass(class_deprecated)
       
    70 class ClientConnection(Connection):
       
    71     __deprecation_warning__ = '[3.20] %(cls)s is deprecated, use Connection instead'