Files
tryton/trytond/security.py
T

60 lines
1.9 KiB
Python
Raw Normal View History

2008-06-23 20:18:54 +02:00
#This file is part of Tryton. The COPYRIGHT file at the top level of this repository contains the full copyright notices and license terms.
2007-12-10 01:04:13 +01:00
import pooler
from config import CONFIG
2008-03-02 02:31:44 +01:00
import sha
2008-03-05 15:10:21 +01:00
import time
import random
2007-12-10 01:04:13 +01:00
_USER_CACHE = {}
2008-03-11 17:14:52 +01:00
def login(dbname, loginname, password, cache=True):
2007-12-10 01:04:13 +01:00
cursor = pooler.get_db(dbname).cursor()
2008-03-02 02:31:44 +01:00
password_sha = sha.new(password).hexdigest()
2007-12-17 23:34:43 +01:00
cursor.execute('SELECT id FROM res_user '
2007-12-10 01:04:13 +01:00
'WHERE login = %s and password = %s and active',
2008-03-02 02:31:44 +01:00
(loginname, password_sha))
2007-12-10 01:04:13 +01:00
res = cursor.fetchone()
cursor.close()
if res:
2008-03-05 15:10:21 +01:00
user_id = res[0]
2008-10-20 10:16:18 +02:00
if user_id == 0:
return False
2008-03-11 17:14:52 +01:00
if cache:
_USER_CACHE.setdefault(dbname, {})
timestamp = time.time()
session = str(random.random())
_USER_CACHE[dbname].setdefault(user_id, [])
_USER_CACHE[dbname][user_id].append((timestamp, session))
2008-03-11 17:14:52 +01:00
return (user_id, session)
else:
return user_id
2007-12-10 01:04:13 +01:00
else:
return False
def check_super(passwd):
if passwd == CONFIG['admin_passwd']:
return True
else:
raise Exception('AccessDenied')
def check(dbname, user, session, outdate_timeout=True):
2008-10-20 10:16:18 +02:00
if user == 0:
raise Exception('AccessDenied')
result = False
2008-03-05 15:10:21 +01:00
if _USER_CACHE.get(dbname, {}).has_key(user):
to_del = []
for i, (timestamp, real_session) \
in enumerate(_USER_CACHE[dbname][user]):
2008-09-05 22:28:28 +02:00
if abs(timestamp - time.time()) < int(CONFIG['session_timeout']):
2008-07-26 17:09:34 +02:00
if real_session == session:
if outdate_timeout:
_USER_CACHE[dbname][user][i] = \
(time.time(), real_session)
result = True
else:
to_del.insert(0, i)
for i in to_del:
del _USER_CACHE[dbname][user][i]
if result:
return True
2008-10-20 10:16:18 +02:00
raise Exception('NotLogged')