Files
tryton/trytond/security.py
T

70 lines
2.2 KiB
Python
Raw Normal View History

2014-12-02 12:54:01 +01: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.
2009-08-25 19:15:50 +02:00
from trytond.pool import Pool
from trytond.config import config
2010-08-19 22:01:12 +02:00
from trytond.transaction import Transaction
2016-03-15 16:57:26 +01:00
from trytond import backend
2016-05-31 12:49:23 +02:00
from trytond.exceptions import LoginException
2008-03-05 15:10:21 +01:00
2012-03-16 12:11:05 +01:00
def _get_pool(dbname):
database_list = Pool.database_list()
pool = Pool(dbname)
2014-12-02 12:54:01 +01:00
if dbname not in database_list:
2012-03-16 12:11:05 +01:00
pool.init()
return pool
2016-05-31 12:49:23 +02:00
def login(dbname, loginname, parameters, cache=True, language=None):
context = {'language': language}
with Transaction().start(dbname, 0, context=context) as transaction:
2012-03-16 12:11:05 +01:00
pool = _get_pool(dbname)
2012-09-24 22:16:53 +02:00
User = pool.get('res.user')
2016-05-31 12:49:23 +02:00
try:
user_id = User.get_login(loginname, parameters)
except LoginException:
# Let's store any changes done
transaction.commit()
raise
2009-08-25 19:15:50 +02:00
if user_id:
2012-01-11 20:30:27 +01:00
if not cache:
2009-08-25 19:15:50 +02:00
return user_id
with Transaction().start(dbname, user_id):
2012-09-24 22:16:53 +02:00
Session = pool.get('ir.session')
2013-01-06 22:18:13 +01:00
session, = Session.create([{}])
2012-01-11 20:30:27 +01:00
return user_id, session.key
2016-03-15 16:57:26 +01:00
return
2007-12-10 01:04:13 +01:00
2012-01-11 20:30:27 +01:00
2009-08-14 11:38:02 +02:00
def logout(dbname, user, session):
with Transaction().start(dbname, 0):
2012-03-16 12:11:05 +01:00
pool = _get_pool(dbname)
2012-09-24 22:16:53 +02:00
Session = pool.get('ir.session')
2013-09-05 01:59:22 +05:30
sessions = Session.search([
2012-01-11 20:30:27 +01:00
('key', '=', session),
])
2013-09-05 01:59:22 +05:30
if not sessions:
return
session, = sessions
2012-01-11 20:30:27 +01:00
name = session.create_uid.login
2013-09-05 01:59:22 +05:30
Session.delete(sessions)
2009-08-14 11:38:02 +02:00
return name
2012-01-11 20:30:27 +01:00
def check(dbname, user, session):
2016-03-15 16:57:26 +01:00
DatabaseOperationalError = backend.get('DatabaseOperationalError')
for count in range(config.getint('database', 'retry'), -1, -1):
with Transaction().start(dbname, user) as transaction:
pool = _get_pool(dbname)
Session = pool.get('ir.session')
try:
if not Session.check(user, session):
return
else:
return user
except DatabaseOperationalError:
if count:
continue
raise
finally:
transaction.commit()