2008-12-11 17:05:52 +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
|
2010-02-18 18:50:35 +01:00
|
|
|
from trytond.config import CONFIG
|
2010-08-19 22:01:12 +02:00
|
|
|
from trytond.transaction import Transaction
|
2011-09-05 20:30:26 +02:00
|
|
|
from trytond.exceptions import NotLogged
|
2008-03-05 15:10:21 +01:00
|
|
|
|
2008-12-11 17:05:52 +01:00
|
|
|
|
2012-03-16 12:11:05 +01:00
|
|
|
def _get_pool(dbname):
|
|
|
|
|
database_list = Pool.database_list()
|
|
|
|
|
pool = Pool(dbname)
|
|
|
|
|
if not dbname in database_list:
|
|
|
|
|
pool.init()
|
|
|
|
|
return pool
|
|
|
|
|
|
|
|
|
|
|
2008-03-11 17:14:52 +01:00
|
|
|
def login(dbname, loginname, password, cache=True):
|
2010-08-19 22:01:12 +02:00
|
|
|
with Transaction().start(dbname, 0) 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')
|
|
|
|
|
user_id = User.get_login(loginname, password)
|
2010-08-19 22:01:12 +02:00
|
|
|
transaction.cursor.commit()
|
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
|
2012-01-11 20:30:27 +01:00
|
|
|
with Transaction().start(dbname, user_id) as transaction:
|
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
|
|
|
transaction.cursor.commit()
|
|
|
|
|
return user_id, session.key
|
2009-01-13 13:16:51 +01:00
|
|
|
return False
|
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):
|
2012-01-11 20:30:27 +01:00
|
|
|
with Transaction().start(dbname, 0) as transaction:
|
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)
|
2012-01-11 20:30:27 +01:00
|
|
|
transaction.cursor.commit()
|
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):
|
|
|
|
|
if passwd == CONFIG['admin_passwd']:
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
raise Exception('AccessDenied')
|
|
|
|
|
|
2012-01-11 20:30:27 +01:00
|
|
|
|
2009-08-05 11:30:56 +02:00
|
|
|
def check(dbname, user, session):
|
2008-10-20 10:16:18 +02:00
|
|
|
if user == 0:
|
|
|
|
|
raise Exception('AccessDenied')
|
2012-01-11 20:30:27 +01:00
|
|
|
if not user:
|
|
|
|
|
raise NotLogged()
|
|
|
|
|
with Transaction().start(dbname, user) as transaction:
|
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')
|
2012-01-11 20:30:27 +01:00
|
|
|
try:
|
2012-09-24 22:16:53 +02:00
|
|
|
if not Session.check(user, session):
|
2012-01-11 20:30:27 +01:00
|
|
|
raise NotLogged()
|
2008-07-18 15:11:58 +02:00
|
|
|
else:
|
2012-01-11 20:30:27 +01:00
|
|
|
return user
|
|
|
|
|
finally:
|
|
|
|
|
transaction.cursor.commit()
|