[IMP] base: prevent to archive users linked to active partners.

An active user could not see messages in its inbox if his partner is
archived. With this commit, we cannot archiving a partner linked to an
active user anymore.

In the opposite way:

 -if we deactivate a user, warning message is given saying the related
  partner will stay active. A link to the related partner is also given
  if we really need to archive it;
 -if we activate a user, the related partner will be set as active;

This commit is linked to task ID 46158. Closes #24628.
This commit is contained in:
William Henrotin
2018-05-28 14:43:06 +02:00
committed by Thibault Delavallée
parent d9c035ae42
commit c9735c75d6
4 changed files with 52 additions and 0 deletions
+11
View File
@@ -235,6 +235,13 @@ class Partner(models.Model):
('check_name', "CHECK( (type='contact' AND name IS NOT NULL) or (type!='contact') )", 'Contacts require a name.'),
]
@api.multi
def toggle_active(self):
for partner in self:
if partner.active and partner.user_ids:
raise ValidationError(_('You cannot archive a contact linked to an internal user.'))
super(Partner, self).toggle_active()
@api.depends('is_company', 'name', 'parent_id.name', 'type', 'company_name')
def _compute_display_name(self):
diff = dict(show_address=None, show_address_only=None, show_email=None)
@@ -487,6 +494,10 @@ class Partner(models.Model):
@api.multi
def write(self, vals):
if vals.get('active') is False:
for partner in self:
if partner.active and partner.user_ids:
raise ValidationError(_('You cannot archive a contact linked to an internal user.'))
# res.partner must only allow to set the company_id of a partner if it
# is the same as the company of all users that inherit from this partner
# (this is to allow the code from res_users to write to the partner!) or
+11
View File
@@ -356,6 +356,13 @@ class Users(models.Model):
if action_open_website and any(user.action_id.id == action_open_website.id for user in self):
raise ValidationError(_('The "App Switcher" action cannot be selected as home action.'))
@api.multi
def toggle_active(self):
for user in self:
if not user.active and not user.partner_id.active:
user.partner_id.toggle_active()
super(Users, self).toggle_active()
@api.multi
def read(self, fields=None, load='_classic_read'):
if fields and self == self.env.user:
@@ -412,6 +419,10 @@ class Users(models.Model):
elif user.id == self._uid:
raise UserError(_("You cannot deactivate the user you're currently logged in as."))
if values.get('active'):
for user in self:
if not user.active and not user.partner_id.active:
user.partner_id.toggle_active()
if self == self.env.user:
for key in list(values):
if not (key in self.SELF_WRITEABLE_FIELDS or key.startswith('context_')):
+21
View File
@@ -396,6 +396,27 @@ class TestBase(TransactionCase):
self.assertEqual([2, 4], [g['title_count'] for g in groups_data], 'Incorrect number of results')
self.assertEqual([-1, 10], [g['color'] for g in groups_data], 'Incorrect aggregation of int column')
def test_70_archive_internal_partners(self):
test_partner = self.env['res.partner'].create({'name':'test partner'})
test_user = self.env['res.users'].create({
'login': 'test@odoo.com',
'partner_id': test_partner.id,
})
# Cannot archive the partner
with self.assertRaises(ValidationError):
test_partner.toggle_active()
# Can archive the user but the partner stays active
test_user.toggle_active()
self.assertTrue(test_partner.active, 'Parter related to user should remain active')
# Now we can archive the partner
test_partner.toggle_active()
# Activate the user should reactivate the partner
test_user.toggle_active()
self.assertTrue(test_partner.active, 'Activating user must active related partner')
class TestPartnerRecursion(TransactionCase):
@@ -162,6 +162,15 @@
<field name="active" widget="boolean_button" options='{"terminology": "active"}'/>
</button>
</div>
<div class="alert alert-info text-center o_form_header" attrs="{'invisible': [('active', '=', True)]}">
<a class="close" data-dismiss="alert" href="#" aria-hidden="true">x</a>
<div>
<strong>The contact linked to this user is still active</strong>
</div>
<div>You can archive the contact
<field name="partner_id" required="0" readonly="1"/>
</div>
</div>
<field name="image" widget='image' class="oe_avatar" options='{"preview_image": "image_medium"}'/>
<div class="oe_title">
<label for="name" class="oe_edit_only"/>