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