Files
tryton/trytond/security.py
T

76 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.
try:
import crypt
except ImportError:
pass
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
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
2008-03-11 17:14:52 +01:00
def login(dbname, loginname, password, cache=True):
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
User = pool.get('res.user')
user_id = User.get_login(loginname, password)
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
2007-12-10 01:04:13 +01:00
def check_super(passwd):
cryptedpasswd = config.get('session', 'super_pwd')
if cryptedpasswd and crypt.crypt(passwd, cryptedpasswd) == cryptedpasswd:
2007-12-10 01:04:13 +01:00
return True
raise Exception('AccessDenied')
2007-12-10 01:04:13 +01:00
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()