Use a Session object for user instead of just an integer

This commit is contained in:
Cédric Krier committed 2008-12-11 17:05:52 +01:00
1 parent a2fdc35245
commit 716f60afe9
6 files changed
+47 -24

No files matched your search

+1 -1
View File
@@ -678,7 +678,7 @@ class ORM(object):
def default_create_uid(self, cursor, user, context=None):
"Default value for uid field"
return user
return int(user)
def default_create_date(self, cursor, user, context=None):
"Default value for create_date field"
+32 -12
View File
@@ -1,10 +1,30 @@
#This file is part of Tryton. The COPYRIGHT file at the top level of this repository contains the full copyright notices and license terms.
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
import pooler
from config import CONFIG
import sha
import time
import random
class Session(int):
def __init__(self, x):
super(Session, self).__init__(x)
self.__data = {
'session': str(random.random()),
'timestamp': time.time(),
}
def __getattr__(self, name):
return self.__data[name]
def __getitem__(self, name):
return self.__data[name]
def reset_timestamp(self):
self.__data['timestamp'] = time.time()
_USER_CACHE = {}
def login(dbname, loginname, password, cache=True):
@@ -21,11 +41,10 @@ def login(dbname, loginname, password, cache=True):
return False
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))
return (user_id, session)
session = Session(user_id)
_USER_CACHE[dbname][user_id].append(session)
return (user_id, session.session)
else:
return user_id
else:
@@ -41,20 +60,21 @@ def check(dbname, user, session, outdate_timeout=True):
if user == 0:
raise Exception('AccessDenied')
result = False
now = time.time()
timeout = int(CONFIG['session_timeout'])
if _USER_CACHE.get(dbname, {}).has_key(user):
to_del = []
for i, (timestamp, real_session) \
for i, real_session \
in enumerate(_USER_CACHE[dbname][user]):
if abs(timestamp - time.time()) < int(CONFIG['session_timeout']):
if real_session == session:
if abs(real_session.timestamp - now) < timeout:
if real_session.session == session:
if outdate_timeout:
_USER_CACHE[dbname][user][i] = \
(time.time(), real_session)
result = True
real_session.reset_timestamp()
result = real_session
else:
to_del.insert(0, i)
for i in to_del:
del _USER_CACHE[dbname][user][i]
if result:
return True
return result
raise Exception('NotLogged')
+6 -3
View File
@@ -1,6 +1,7 @@
#This file is part of Tryton. The COPYRIGHT file at the top level of this repository contains the full copyright notices and license terms.
#This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
from psycopg2.pool import ThreadedConnectionPool
from psycopg2.extensions import ISOLATION_LEVEL_SERIALIZABLE, cursor
from psycopg2.extensions import ISOLATION_LEVEL_SERIALIZABLE, cursor, AsIs
from psycopg2 import IntegrityError
import psycopg2
import re
@@ -10,6 +11,7 @@ import zipfile
import version
from config import CONFIG
import logging
from trytond.security import Session
RE_FROM = re.compile('.* from "?([a-zA-Z_0-9]+)"?.*$')
RE_INTO = re.compile('.* into "?([a-zA-Z_0-9]+)"?.*$')
@@ -208,7 +210,8 @@ def init_db(cursor):
(module_id, dependency))
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
psycopg2.extensions.register_adapter(Session,
psycopg2.extensions.AsIs)
def table_exist(cursor, table_name):
+4 -4
View File
@@ -14,7 +14,7 @@ class Object(Service):
def exec_workflow(self, database, user, session, object_name, method,
object_id, *args):
security.check(database, user, session)
user = security.check(database, user, session)
Cache.clean(database)
service = LocalService("object_proxy")
res = service.exec_workflow(database, user, object_name, method,
@@ -23,16 +23,16 @@ class Object(Service):
def execute(self, database, user, session, object_name, method, *args):
if object_name == 'res.request' and method == 'request_get':
security.check(database, user, session, False)
user = security.check(database, user, session, False)
else:
security.check(database, user, session)
user = security.check(database, user, session)
Cache.clean(database)
service = LocalService("object_proxy")
res = service.execute(database, user, object_name, method, *args)
return res
def obj_list(self, database, user, session):
security.check(database, user, session)
user = security.check(database, user, session)
Cache.clean(database)
service = LocalService("object_proxy")
res = service.obj_list()
+1 -1
View File
@@ -13,7 +13,7 @@ class Report(Service):
def execute(self, database, user, passwd, report_name, ids, datas,
context=None):
security.check(database, user, passwd)
user = security.check(database, user, passwd)
Cache.clean(database)
report = LocalService('report_proxy')
return report.execute(database, user, report_name, ids, datas, context)
+3 -3
View File
@@ -27,7 +27,7 @@ class Wizard(Service):
self.wiz_datas[wiz_id], action, context)
def create(self, database, user, passwd, wiz_name, datas=None):
security.check(database, user, passwd)
user = security.check(database, user, passwd)
Cache.clean(database)
self.lock.acquire()
wiz_id = 0
@@ -42,7 +42,7 @@ class Wizard(Service):
return wiz_id
def execute(self, database, user, passwd, wiz_id, datas, *args):
security.check(database, user, passwd)
user = security.check(database, user, passwd)
Cache.clean(database)
if wiz_id in self.wiz_uid:
if self.wiz_uid[wiz_id] == user:
@@ -53,7 +53,7 @@ class Wizard(Service):
raise Exception, 'WizardNotFound'
def delete(self, database, user, passwd, wiz_id):
security.check(database, user, passwd)
user = security.check(database, user, passwd)
Cache.clean(database)
if wiz_id in self.wiz_uid:
if self.wiz_uid[wiz_id] == user: