[ADD] auth_openid module
[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-2011 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.osv import osv, fields
22 import openerp.exceptions
23 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 utils.cursor(db) as cr:
68                 cr.execute('UPDATE res_users SET date=now() WHERE login=%s AND openid_key=%s AND active=%s RETURNING id',
69                         (tools.ustr(login), tools.ustr(password), True))
70                 res = cr.fetchone()
71                 cr.commit()
72                 return res[0] if res else False
73
74
75     def check(self, db, uid, passwd):
76         try:
77             return super(res_users, self).check(db, uid, passwd)
78         except openerp.exceptions.AccessDenied:
79             if not passwd:
80                 raise
81             with utils.cursor(db) as cr:
82                 cr.execute('''SELECT COUNT(1)
83                                 FROM res_users
84                                WHERE id=%s
85                                  AND openid_key=%s
86                                  AND active=%s''',
87                             (int(uid), passwd, True))
88                 if not cr.fetchone()[0]:
89                     raise
90                 self._uid_cache.setdefault(db, {})[uid] = passwd
91
92 res_users()
93
94