Launchpad automatic translations update.
[odoo/odoo.git] / addons / auth_openid / res_users.py
1 #!/usr/bin/env python
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2010-2012 OpenERP s.a. (<http://openerp.com>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21 from openerp.modules.registry import RegistryManager
22 from openerp.osv import osv, fields
23 import openerp.exceptions
24 import tools
25
26 import utils
27
28 class res_users(osv.osv):
29     _inherit = 'res.users'
30
31     # TODO create helper fields for autofill openid_url and openid_email -> http://pad.openerp.com/web-openid
32
33     _columns = {
34         'openid_url': fields.char('OpenID URL', size=1024),
35         'openid_email': fields.char('OpenID Email', size=256,
36                                     help="Used for disambiguation in case of a shared OpenID URL"),
37         'openid_key': fields.char('OpenID Key', size=utils.KEY_LENGTH,
38                                   readonly=True),
39     }
40
41     def _check_openid_url_email(self, cr, uid, ids, context=None):
42         return all(self.search_count(cr, uid, [('active', '=', True), ('openid_url', '=', u.openid_url), ('openid_email', '=', u.openid_email)]) == 1 \
43                    for u in self.browse(cr, uid, ids, context) if u.active and u.openid_url)
44
45     def _check_openid_url_email_msg(self, cr, uid, ids, context):
46         return "There is already an active user with this OpenID Email for this OpenID URL"
47
48     _constraints = [
49         (_check_openid_url_email, lambda self, *a, **kw: self._check_openid_url_email_msg(*a, **kw), ['active', 'openid_url', 'openid_email']),
50     ]
51
52     def copy(self, cr, uid, rid, defaults=None, context=None):
53         reset_fields = 'openid_url openid_email'.split()
54         reset_values = dict.fromkeys(reset_fields, False)
55         if defaults is None:
56             defaults = reset_values
57         else:
58             defaults = dict(reset_values, **defaults)
59
60         defaults['openid_key'] = False
61         return super(res_users, self).copy(cr, uid, rid, defaults, context)
62
63     def login(self, db, login, password):
64         result = super(res_users, self).login(db, login, password)
65         if result:
66             return result
67         else:
68             with RegistryManager.get(db).cursor() as cr:
69                 cr.execute("""UPDATE res_users
70                                 SET login_date=now() AT TIME ZONE 'UTC'
71                                 WHERE login=%s AND openid_key=%s AND active=%s RETURNING id""",
72                            (tools.ustr(login), tools.ustr(password), True))
73                 res = cr.fetchone()
74                 cr.commit()
75                 return res[0] if res else False
76
77     def check(self, db, uid, passwd):
78         try:
79             return super(res_users, self).check(db, uid, passwd)
80         except openerp.exceptions.AccessDenied:
81             if not passwd:
82                 raise
83             with RegistryManager.get(db).cursor() as cr:
84                 cr.execute('''SELECT COUNT(1)
85                                 FROM res_users
86                                WHERE id=%s
87                                  AND openid_key=%s
88                                  AND active=%s''',
89                             (int(uid), passwd, True))
90                 if not cr.fetchone()[0]:
91                     raise
92                 self._uid_cache.setdefault(db, {})[uid] = passwd
93
94 res_users()
95
96
97
98 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: