13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
14 # details. |
14 # details. |
15 # |
15 # |
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 warnings import warn |
20 from warnings import warn |
21 |
21 |
22 from six import add_metaclass |
22 from six import add_metaclass |
23 |
23 |
24 from logilab.common.deprecation import class_deprecated |
24 from logilab.common.deprecation import class_deprecated |
25 |
25 |
26 from cubicweb import AuthenticationError |
26 from cubicweb import AuthenticationError |
27 from cubicweb.server.session import Connection |
27 from cubicweb.server.session import Connection |
28 |
28 |
29 |
|
30 ### public API ###################################################### |
|
31 |
29 |
32 def get_repository(uri=None, config=None, vreg=None): |
30 def get_repository(uri=None, config=None, vreg=None): |
33 """get a repository for the given URI or config/vregistry (in case we're |
31 """get a repository for the given URI or config/vregistry (in case we're |
34 loading the repository for a client, eg web server, configuration). |
32 loading the repository for a client, eg web server, configuration). |
35 |
33 |
39 if uri is not None: |
37 if uri is not None: |
40 warn('[3.22] get_repository only wants a config') |
38 warn('[3.22] get_repository only wants a config') |
41 |
39 |
42 assert config is not None, 'get_repository(config=config)' |
40 assert config is not None, 'get_repository(config=config)' |
43 return config.repository(vreg) |
41 return config.repository(vreg) |
|
42 |
44 |
43 |
45 def connect(repo, login, **kwargs): |
44 def connect(repo, login, **kwargs): |
46 """Take credential and return associated Connection. |
45 """Take credential and return associated Connection. |
47 |
46 |
48 raise AuthenticationError if the credential are invalid. |
47 raise AuthenticationError if the credential are invalid. |
57 """return a Connection for Anonymous user. |
56 """return a Connection for Anonymous user. |
58 |
57 |
59 raises an AuthenticationError if anonymous usage is not allowed |
58 raises an AuthenticationError if anonymous usage is not allowed |
60 """ |
59 """ |
61 anoninfo = getattr(repo.config, 'anonymous_user', lambda: None)() |
60 anoninfo = getattr(repo.config, 'anonymous_user', lambda: None)() |
62 if anoninfo is None: # no anonymous user |
61 if anoninfo is None: # no anonymous user |
63 raise AuthenticationError('anonymous access is not authorized') |
62 raise AuthenticationError('anonymous access is not authorized') |
64 anon_login, anon_password = anoninfo |
63 anon_login, anon_password = anoninfo |
65 # use vreg's repository cache |
64 # use vreg's repository cache |
66 return connect(repo, anon_login, password=anon_password) |
65 return connect(repo, anon_login, password=anon_password) |
67 |
66 |