1 # copyright 2003-2010 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 """ |
|
19 |
|
20 """ |
|
21 from AccessControl import getSecurityManager |
|
22 |
|
23 from cubicweb.dbapi import connect, Connection, Cursor |
|
24 from cubicweb.common.utils import ResultSet, ResultSetIterator, ResultSetRow, Entity |
|
25 |
|
26 Connection.__allow_access_to_unprotected_subobjects__ = 1 |
|
27 Cursor.__allow_access_to_unprotected_subobjects__ = 1 |
|
28 ResultSet.__allow_access_to_unprotected_subobjects__ = 1 |
|
29 ResultSetIterator.__allow_access_to_unprotected_subobjects__ = 1 |
|
30 ResultSetRow.__allow_access_to_unprotected_subobjects__ = 1 |
|
31 Entity.__allow_access_to_unprotected_subobjects__ = 1 |
|
32 |
|
33 CNX_CACHE = {} |
|
34 |
|
35 def get_connection(context, user=None, password=None, |
|
36 host=None, database=None, group='cubicweb'): |
|
37 """get a connection on an cubicweb server""" |
|
38 request = context.REQUEST |
|
39 zope_user = getSecurityManager().getUser() |
|
40 if user is None: |
|
41 user = zope_user.getId() |
|
42 key = (user, host, database) |
|
43 try: |
|
44 return CNX_CACHE[key] |
|
45 except KeyError: |
|
46 if password is None: |
|
47 password = zope_user._getPassword() |
|
48 cnx = connect(user, password, host, database, group) |
|
49 CNX_CACHE[key] = cnx |
|
50 return cnx |
|