~toolpart/openobject-server/toolpart

« back to all changes in this revision

Viewing changes to bin/service/security.py

  • Committer: pinky
  • Date: 2006-12-07 13:41:40 UTC
  • Revision ID: pinky-3f10ee12cea3c4c75cef44ab04ad33ef47432907
New trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
#
 
3
# Copyright (c) 2004 TINY SPRL. (http://tiny.be) All Rights Reserved.
 
4
#                    Fabien Pinckaers <fp@tiny.Be>
 
5
#
 
6
# WARNING: This program as such is intended to be used by professional
 
7
# programmers who take the whole responsability of assessing all potential
 
8
# consequences resulting from its eventual inadequacies and bugs
 
9
# End users who are looking for a ready-to-use solution with commercial
 
10
# garantees and support are strongly adviced to contract a Free Software
 
11
# Service Company
 
12
#
 
13
# This program is Free Software; you can redistribute it and/or
 
14
# modify it under the terms of the GNU General Public License
 
15
# as published by the Free Software Foundation; either version 2
 
16
# of the License, or (at your option) any later version.
 
17
#
 
18
# This program is distributed in the hope that it will be useful,
 
19
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
20
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
21
# GNU General Public License for more details.
 
22
#
 
23
# You should have received a copy of the GNU General Public License
 
24
# along with this program; if not, write to the Free Software
 
25
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
26
#
 
27
##############################################################################
 
28
 
 
29
 
 
30
import pooler
 
31
import tools
 
32
 
 
33
_uid_cache = {}
 
34
 
 
35
def login(db, login, password):
 
36
        cr = pooler.get_db(db).cursor()
 
37
        cr.execute('select id from res_users where login=%s and password=%s and active', (login, password))
 
38
        res = cr.fetchone()
 
39
        cr.close()
 
40
        if res:
 
41
                return res[0]
 
42
        else:
 
43
                raise _('Bad username or password')
 
44
 
 
45
def check_super(passwd):
 
46
        if passwd == tools.config['admin_passwd']:
 
47
                return True
 
48
        else:
 
49
                raise 'AccessDenied'
 
50
        
 
51
def check(db, uid, passwd):
 
52
        #FIXME: this should be db dependent
 
53
        if _uid_cache.has_key(uid) and (_uid_cache[uid]==passwd):
 
54
                return True
 
55
        cr = pooler.get_db(db).cursor()
 
56
        cr.execute('select count(*) from res_users where id=%d and password=%s', (int(uid), passwd))
 
57
        res = cr.fetchone()[0]
 
58
        cr.close()
 
59
        if not bool(res):
 
60
                raise 'AccessDenied'
 
61
        if res:
 
62
                _uid_cache[uid] = passwd
 
63
        return bool(res)
 
64
 
 
65
def access(db, uid, passwd, sec_level, ids):
 
66
        cr = pooler.get_db(db).cursor()
 
67
        cr.execute('select id from res_users where id=%s and password=%s', (uid, passwd))
 
68
        res = cr.fetchone()
 
69
        cr.close()
 
70
        if not res:
 
71
                raise _('Bad username or password')
 
72
        return res[0]