82 lines
3.1 KiB
Python
82 lines
3.1 KiB
Python
# -*- coding : utf-8 -*-
|
|
# Part of BrowseInfo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from odoo import models, fields, api, _
|
|
from odoo.exceptions import UserError, ValidationError
|
|
|
|
class ResPartner(models.Model):
|
|
_inherit = "res.partner"
|
|
|
|
allowed_user_ids = fields.Many2many('res.users', 'res_partner_users_sale_rel', string="Allowed Users",default=lambda self: self.env.user)
|
|
is_company_user = fields.Boolean(string='Is Company Partner', compute="_compute_is_company_user", store=True)
|
|
company_id = fields.Many2one('res.company', 'Company', index=True, default=lambda self: self.env.company)
|
|
|
|
@api.model
|
|
def default_get(self, vals):
|
|
result = super(ResPartner, self).default_get(vals)
|
|
|
|
result.update({
|
|
'user_id' : self.env.user.id
|
|
})
|
|
|
|
return result
|
|
|
|
@api.depends('company_type','is_company')
|
|
def _compute_is_company_user(self):
|
|
for partner in self:
|
|
company = self.env['res.company'].sudo().search([('partner_id','=',partner.id)])
|
|
if company:
|
|
partner_company = True
|
|
else:
|
|
partner_company = False
|
|
|
|
partner.update({
|
|
'is_company_user' : partner_company
|
|
})
|
|
|
|
@api.model
|
|
@api.readonly
|
|
@api.returns('self')
|
|
def search(self, domain, offset=0, limit=None, order=None):
|
|
allowed_access = self.env.user.has_group('bi_sales_security.group_sales_security_allowed_customer')
|
|
own_access = self.env.user.has_group('bi_sales_security.group_sales_security_own_customer')
|
|
all_access = self.env.user.has_group('bi_sales_security.group_sales_security_manager')
|
|
|
|
|
|
if allowed_access and not all_access:
|
|
domain += [('allowed_user_ids','in',[self.env.user.id])]
|
|
|
|
elif own_access and not all_access :
|
|
domain += [('id','=',self.env.user.partner_id.id)]
|
|
|
|
return super(ResPartner, self).search(domain, offset, limit, order)
|
|
|
|
@api.model
|
|
def _name_search(self, name, domain=None, operator='ilike', limit=None, order=None):
|
|
context = dict(self.env.context)
|
|
allowed_access = self.env.user.has_group('bi_sales_security.group_sales_security_allowed_customer')
|
|
own_access = self.env.user.has_group('bi_sales_security.group_sales_security_own_customer')
|
|
all_access = self.env.user.has_group('bi_sales_security.group_sales_security_manager')
|
|
domain = domain or []
|
|
if allowed_access and not all_access:
|
|
domain += [('allowed_user_ids','in',[self.env.user.id])]
|
|
|
|
elif own_access and not all_access :
|
|
domain += [('id','=',self.env.user.partner_id.id)]
|
|
|
|
return super(ResPartner, self.sudo().with_context(context))._name_search(name=name, domain=domain, operator=operator, limit=limit, order=order)
|
|
|
|
|
|
class ResCompany(models.Model):
|
|
_inherit = 'res.company'
|
|
|
|
@api.model
|
|
def create(self, vals):
|
|
company = super(ResCompany, self).create(vals)
|
|
|
|
if company.partner_id:
|
|
company.partner_id.write({
|
|
'is_company_user' : True
|
|
})
|
|
|
|
return company |