[IMP] add a method to rename a database (and the associated filestore directory)
[odoo/odoo.git] / bin / service / security.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 import pooler
24 import tools
25
26 _uid_cache = {}
27
28 def login(db, login, password):
29     cr = pooler.get_db(db).cursor()
30     cr.execute('select id from res_users where login=%s and password=%s and active', (login.encode('utf-8'), password.encode('utf-8')))
31     res = cr.fetchone()
32     cr.close()
33     if res:
34         return res[0]
35     else:
36         return False
37
38 def check_super(passwd):
39     if passwd == tools.config['admin_passwd']:
40         return True
41     else:
42         raise Exception('AccessDenied')
43
44 def check(db, uid, passwd):
45     if _uid_cache.get(db, {}).get(uid) == passwd:
46         return True
47     cr = pooler.get_db(db).cursor()
48     cr.execute('select count(*) from res_users where id=%s and password=%s', (int(uid), passwd))
49     res = cr.fetchone()[0]
50     cr.close()
51     if not bool(res):
52         raise Exception('AccessDenied')
53     if res:
54         if _uid_cache.has_key(db):
55             ulist = _uid_cache[db]
56             ulist[uid] = passwd
57         else:
58             _uid_cache[db] = {uid:passwd}
59     return bool(res)
60
61 def access(db, uid, passwd, sec_level, ids):
62     cr = pooler.get_db(db).cursor()
63     cr.execute('select id from res_users where id=%s and password=%s', (uid, passwd))
64     res = cr.fetchone()
65     cr.close()
66     if not res:
67         raise Exception('Bad username or password')
68     return res[0]
69
70 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
71