2014-12-02 12:55:12 +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.
|
2021-03-18 23:04:17 +01:00
|
|
|
from sql import Null
|
|
|
|
|
|
2023-09-24 13:56:20 +02:00
|
|
|
from trytond.cache import Cache
|
2019-12-05 22:37:16 +01:00
|
|
|
from trytond.model import ModelSQL, fields
|
2021-12-11 17:58:42 +01:00
|
|
|
from trytond.pool import Pool, PoolMeta
|
2019-12-05 22:37:16 +01:00
|
|
|
from trytond.pyson import Eval
|
2010-08-10 11:58:47 -04:00
|
|
|
from trytond.transaction import Transaction
|
2017-09-27 18:37:28 +02:00
|
|
|
|
2008-05-28 16:27:39 +02:00
|
|
|
|
2021-03-18 23:04:17 +01:00
|
|
|
class UserCompany(ModelSQL):
|
|
|
|
|
"User - Company"
|
|
|
|
|
__name__ = 'res.user-company.company'
|
|
|
|
|
|
|
|
|
|
user = fields.Many2One(
|
2022-10-11 00:44:49 +02:00
|
|
|
'res.user', "User", ondelete='CASCADE', required=True)
|
2021-03-18 23:04:17 +01:00
|
|
|
company = fields.Many2One(
|
|
|
|
|
'company.company', "Company",
|
2022-10-11 00:44:49 +02:00
|
|
|
ondelete='CASCADE', required=True)
|
2021-03-18 23:04:17 +01:00
|
|
|
|
2023-09-24 13:56:20 +02:00
|
|
|
@classmethod
|
|
|
|
|
def create(cls, vlist):
|
|
|
|
|
pool = Pool()
|
|
|
|
|
User = pool.get('res.user')
|
|
|
|
|
records = super().create(vlist)
|
|
|
|
|
User._get_companies_cache.clear()
|
|
|
|
|
return records
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def write(cls, *args):
|
|
|
|
|
pool = Pool()
|
|
|
|
|
User = pool.get('res.user')
|
|
|
|
|
super().write(*args)
|
|
|
|
|
User._get_companies_cache.clear()
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def delete(cls, records):
|
|
|
|
|
pool = Pool()
|
|
|
|
|
User = pool.get('res.user')
|
|
|
|
|
super().delete(records)
|
|
|
|
|
User._get_companies_cache.clear()
|
|
|
|
|
|
2021-03-18 23:04:17 +01:00
|
|
|
|
2012-01-26 12:41:06 +01:00
|
|
|
class UserEmployee(ModelSQL):
|
|
|
|
|
'User - Employee'
|
2012-09-24 22:17:50 +02:00
|
|
|
__name__ = 'res.user-company.employee'
|
2022-10-11 00:44:49 +02:00
|
|
|
user = fields.Many2One(
|
|
|
|
|
'res.user', "User", ondelete='CASCADE', required=True)
|
|
|
|
|
employee = fields.Many2One(
|
|
|
|
|
'company.employee', "Employee", ondelete='CASCADE', required=True)
|
2012-01-26 12:41:06 +01:00
|
|
|
|
2023-09-24 13:56:20 +02:00
|
|
|
@classmethod
|
|
|
|
|
def create(cls, vlist):
|
|
|
|
|
pool = Pool()
|
|
|
|
|
User = pool.get('res.user')
|
|
|
|
|
records = super().create(vlist)
|
|
|
|
|
User._get_employees_cache.clear()
|
|
|
|
|
return records
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def write(cls, *args):
|
|
|
|
|
pool = Pool()
|
|
|
|
|
User = pool.get('res.user')
|
|
|
|
|
super().write(*args)
|
|
|
|
|
User._get_employees_cache.clear()
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def delete(cls, records):
|
|
|
|
|
pool = Pool()
|
|
|
|
|
User = pool.get('res.user')
|
|
|
|
|
super().delete(records)
|
|
|
|
|
User._get_employees_cache.clear()
|
|
|
|
|
|
2012-01-26 12:41:06 +01:00
|
|
|
|
2018-06-26 19:11:46 +02:00
|
|
|
class User(metaclass=PoolMeta):
|
2012-09-24 22:17:50 +02:00
|
|
|
__name__ = 'res.user'
|
2021-03-18 23:04:17 +01:00
|
|
|
|
|
|
|
|
companies = fields.Many2Many(
|
|
|
|
|
'res.user-company.company', 'user', 'company', "Companies",
|
|
|
|
|
help="The companies that the user has access to.")
|
|
|
|
|
company = fields.Many2One(
|
|
|
|
|
'company.company', "Current Company",
|
|
|
|
|
domain=[
|
|
|
|
|
('id', 'in', Eval('companies', [])),
|
|
|
|
|
],
|
2017-04-03 20:59:03 +02:00
|
|
|
help="Select the company to work for.")
|
2012-01-26 12:41:06 +01:00
|
|
|
employees = fields.Many2Many('res.user-company.employee', 'user',
|
2017-04-03 20:59:03 +02:00
|
|
|
'employee', 'Employees',
|
2021-03-18 23:04:17 +01:00
|
|
|
domain=[
|
|
|
|
|
('company', 'in', Eval('companies', [])),
|
|
|
|
|
],
|
2017-04-03 20:59:03 +02:00
|
|
|
help="Add employees to grant the user access to them.")
|
2012-08-11 12:22:12 +02:00
|
|
|
employee = fields.Many2One('company.employee', 'Current Employee',
|
2012-01-26 12:41:06 +01:00
|
|
|
domain=[
|
2014-04-05 00:35:22 +02:00
|
|
|
('company', '=', Eval('company', -1)),
|
2012-01-26 12:41:06 +01:00
|
|
|
('id', 'in', Eval('employees', [])),
|
|
|
|
|
],
|
2017-04-03 20:59:03 +02:00
|
|
|
help="Select the employee to make the user behave as such.")
|
2021-04-10 23:46:23 +02:00
|
|
|
company_filter = fields.Selection([
|
|
|
|
|
('one', "Current"),
|
|
|
|
|
('all', "All"),
|
|
|
|
|
], "Company Filter",
|
|
|
|
|
help="Define records of which companies are shown.")
|
2023-09-24 13:56:20 +02:00
|
|
|
_get_companies_cache = Cache(__name__ + '.get_companies', context=False)
|
|
|
|
|
_get_employees_cache = Cache(__name__ + '.get_employees', context=False)
|
2008-02-19 14:58:52 +01:00
|
|
|
|
2012-09-24 22:17:50 +02:00
|
|
|
@classmethod
|
|
|
|
|
def __setup__(cls):
|
|
|
|
|
super(User, cls).__setup__()
|
|
|
|
|
cls._context_fields.insert(0, 'company')
|
|
|
|
|
cls._context_fields.insert(0, 'employee')
|
2021-04-10 23:46:23 +02:00
|
|
|
cls._context_fields.insert(0, 'company_filter')
|
2008-02-26 17:53:04 +01:00
|
|
|
|
2021-03-18 23:04:17 +01:00
|
|
|
@classmethod
|
|
|
|
|
def __register__(cls, module):
|
|
|
|
|
pool = Pool()
|
|
|
|
|
UserCompany = pool.get('res.user-company.company')
|
|
|
|
|
transaction = Transaction()
|
|
|
|
|
table = cls.__table__()
|
|
|
|
|
user_company = UserCompany.__table__()
|
|
|
|
|
|
|
|
|
|
super().__register__(module)
|
|
|
|
|
|
|
|
|
|
table_h = cls.__table_handler__(module)
|
|
|
|
|
cursor = transaction.connection.cursor()
|
|
|
|
|
|
|
|
|
|
# Migration from 5.8: remove main_company
|
|
|
|
|
if table_h.column_exist('main_company'):
|
|
|
|
|
cursor.execute(*user_company.insert(
|
|
|
|
|
[user_company.user, user_company.company],
|
|
|
|
|
table.select(
|
|
|
|
|
table.id, table.main_company,
|
|
|
|
|
where=table.main_company != Null)))
|
|
|
|
|
cursor.execute(*user_company.insert(
|
|
|
|
|
[user_company.user, user_company.company],
|
|
|
|
|
table.select(
|
|
|
|
|
table.id, table.company,
|
|
|
|
|
where=(table.company != Null)
|
|
|
|
|
& (table.company != table.main_company))))
|
|
|
|
|
table_h.drop_column('main_company')
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def default_companies(cls):
|
|
|
|
|
company = Transaction().context.get('company')
|
2021-03-24 13:48:25 +01:00
|
|
|
return [company] if company else []
|
2008-12-31 00:22:01 +01:00
|
|
|
|
2012-09-24 22:17:50 +02:00
|
|
|
@classmethod
|
|
|
|
|
def default_company(cls):
|
2021-03-18 23:04:17 +01:00
|
|
|
return Transaction().context.get('company')
|
2009-04-11 16:01:01 +02:00
|
|
|
|
2021-04-10 23:46:23 +02:00
|
|
|
@classmethod
|
|
|
|
|
def default_company_filter(cls):
|
|
|
|
|
return 'one'
|
|
|
|
|
|
2012-09-24 22:17:50 +02:00
|
|
|
def get_status_bar(self, name):
|
2020-02-12 00:23:04 +01:00
|
|
|
def same_company(record):
|
|
|
|
|
return record.company == self.company
|
2012-09-24 22:17:50 +02:00
|
|
|
status = super(User, self).get_status_bar(name)
|
2020-02-12 00:23:04 +01:00
|
|
|
if (self.employee
|
|
|
|
|
and len(list(filter(same_company, self.employees))) > 1):
|
|
|
|
|
status += ' - %s' % self.employee.rec_name
|
2012-09-24 22:17:50 +02:00
|
|
|
if self.company:
|
2021-03-18 23:04:17 +01:00
|
|
|
if len(self.companies) > 1:
|
2020-02-12 00:23:04 +01:00
|
|
|
status += ' - %s' % self.company.rec_name
|
|
|
|
|
status += ' [%s]' % self.company.currency.code
|
2012-09-24 22:17:50 +02:00
|
|
|
return status
|
2008-06-12 19:41:40 +02:00
|
|
|
|
2014-01-26 14:29:09 +01:00
|
|
|
@fields.depends('company', 'employees')
|
2012-09-24 22:17:50 +02:00
|
|
|
def on_change_company(self):
|
|
|
|
|
Employee = Pool().get('company.employee')
|
2014-10-29 20:39:42 +01:00
|
|
|
self.employee = None
|
2012-09-24 22:17:50 +02:00
|
|
|
if self.company and self.employees:
|
|
|
|
|
employees = Employee.search([
|
|
|
|
|
('id', 'in', [e.id for e in self.employees]),
|
|
|
|
|
('company', '=', self.company.id),
|
2008-02-20 18:01:34 +01:00
|
|
|
])
|
2012-09-24 22:17:50 +02:00
|
|
|
if employees:
|
2014-10-29 20:39:42 +01:00
|
|
|
self.employee = employees[0]
|
2008-02-19 14:58:52 +01:00
|
|
|
|
2012-09-24 22:17:50 +02:00
|
|
|
@classmethod
|
|
|
|
|
def _get_preferences(cls, user, context_only=False):
|
|
|
|
|
res = super(User, cls)._get_preferences(user,
|
|
|
|
|
context_only=context_only)
|
2008-02-22 14:01:38 +01:00
|
|
|
if not context_only:
|
2021-03-18 23:04:17 +01:00
|
|
|
res['companies'] = [c.id for c in user.companies]
|
2012-01-26 12:41:06 +01:00
|
|
|
res['employees'] = [e.id for e in user.employees]
|
2008-02-22 14:01:38 +01:00
|
|
|
return res
|
|
|
|
|
|
2023-09-24 13:56:20 +02:00
|
|
|
@classmethod
|
|
|
|
|
def get_companies(cls):
|
|
|
|
|
'''
|
|
|
|
|
Return an ordered tuple of company ids for the user
|
|
|
|
|
'''
|
|
|
|
|
transaction = Transaction()
|
|
|
|
|
user_id = transaction.user
|
|
|
|
|
companies = cls._get_companies_cache.get(user_id)
|
|
|
|
|
if companies is not None:
|
|
|
|
|
return companies
|
|
|
|
|
with transaction.set_user(0):
|
|
|
|
|
user = cls(user_id)
|
|
|
|
|
if user.company_filter == 'one':
|
|
|
|
|
companies = [user.company.id] if user.company else []
|
|
|
|
|
elif user.company_filter == 'all':
|
|
|
|
|
companies = [c.id for c in user.companies]
|
|
|
|
|
else:
|
|
|
|
|
companies = []
|
|
|
|
|
companies = tuple(companies)
|
|
|
|
|
cls._get_companies_cache.set(user_id, companies)
|
|
|
|
|
return companies
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def get_employees(cls):
|
|
|
|
|
'''
|
|
|
|
|
Return an ordered tuple of employee ids for the user
|
|
|
|
|
'''
|
|
|
|
|
transaction = Transaction()
|
|
|
|
|
user_id = transaction.user
|
|
|
|
|
employees = cls._get_employees_cache.get(user_id)
|
|
|
|
|
if employees is not None:
|
|
|
|
|
return employees
|
|
|
|
|
with transaction.set_user(0):
|
|
|
|
|
user = cls(user_id)
|
|
|
|
|
if user.company_filter == 'one':
|
|
|
|
|
employees = [user.employee.id] if user.employee else []
|
|
|
|
|
elif user.company_filter == 'all':
|
|
|
|
|
employees = [e.id for e in user.employees]
|
|
|
|
|
else:
|
|
|
|
|
employees = []
|
|
|
|
|
employees = tuple(employees)
|
|
|
|
|
cls._get_employees_cache.set(user_id, employees)
|
|
|
|
|
return employees
|
|
|
|
|
|
2012-09-24 22:17:50 +02:00
|
|
|
@classmethod
|
2020-05-12 17:35:43 +02:00
|
|
|
def read(cls, ids, fields_names):
|
2011-06-07 11:33:08 +02:00
|
|
|
user_id = Transaction().user
|
|
|
|
|
if user_id == 0 and 'user' in Transaction().context:
|
|
|
|
|
user_id = Transaction().context['user']
|
2020-05-12 17:35:43 +02:00
|
|
|
result = super(User, cls).read(ids, fields_names)
|
2011-06-07 11:33:08 +02:00
|
|
|
if (fields_names
|
2020-03-01 12:33:52 +01:00
|
|
|
and ((
|
|
|
|
|
'company' in fields_names
|
2013-12-10 12:10:40 +01:00
|
|
|
and 'company' in Transaction().context)
|
|
|
|
|
or ('employee' in fields_names
|
|
|
|
|
and 'employee' in Transaction().context))):
|
2011-06-07 11:33:08 +02:00
|
|
|
values = None
|
2012-09-24 22:17:50 +02:00
|
|
|
if int(user_id) in ids:
|
|
|
|
|
for vals in result:
|
|
|
|
|
if vals['id'] == int(user_id):
|
|
|
|
|
values = vals
|
|
|
|
|
break
|
2011-06-07 11:33:08 +02:00
|
|
|
if values:
|
2013-12-12 12:12:51 +01:00
|
|
|
if ('company' in fields_names
|
|
|
|
|
and 'company' in Transaction().context):
|
2021-03-18 23:04:17 +01:00
|
|
|
companies = values.get('companies')
|
|
|
|
|
if not companies:
|
|
|
|
|
companies = cls.read([user_id],
|
|
|
|
|
['companies'])[0]['companies']
|
2013-12-10 12:10:40 +01:00
|
|
|
company_id = Transaction().context['company']
|
2021-03-18 23:04:17 +01:00
|
|
|
if ((company_id and company_id in companies)
|
2017-08-02 19:32:08 +02:00
|
|
|
or not company_id
|
|
|
|
|
or Transaction().user == 0):
|
2013-12-10 12:10:40 +01:00
|
|
|
values['company'] = company_id
|
2023-05-30 16:36:25 +02:00
|
|
|
else:
|
|
|
|
|
values['company'] = None
|
2013-12-12 12:12:51 +01:00
|
|
|
if ('employee' in fields_names
|
|
|
|
|
and 'employee' in Transaction().context):
|
2013-12-10 12:10:40 +01:00
|
|
|
employees = values.get('employees')
|
|
|
|
|
if not employees:
|
|
|
|
|
employees = cls.read([user_id],
|
|
|
|
|
['employees'])[0]['employees']
|
|
|
|
|
employee_id = Transaction().context['employee']
|
2017-08-02 19:32:08 +02:00
|
|
|
if ((employee_id and employee_id in employees)
|
|
|
|
|
or not employee_id
|
|
|
|
|
or Transaction().user == 0):
|
2013-12-10 12:10:40 +01:00
|
|
|
values['employee'] = employee_id
|
2023-05-30 16:36:25 +02:00
|
|
|
else:
|
|
|
|
|
values['employee'] = None
|
2011-06-07 11:33:08 +02:00
|
|
|
return result
|
|
|
|
|
|
2015-10-02 08:56:51 +02:00
|
|
|
@classmethod
|
|
|
|
|
def write(cls, *args):
|
2023-09-24 13:56:20 +02:00
|
|
|
super().write(*args)
|
|
|
|
|
cls._get_companies_cache.clear()
|
|
|
|
|
cls._get_employees_cache.clear()
|