[FIX] website_slides: Raise error if not responsible on invite

Steps to reproduce:

- Install `E-learning` module
- Create a user X with only "Officer" as rights for `E-learning`
- Login with user X
- Go to `E-learning` and open any course
- Ensure that the user X is not the responsible and that the course
  enroll policy is set to "On invitation"
- Invite any user not already a member

Issue:

  No user was invited (not added as member) and no warning message
  raised.

Cause:

  Due to the ir.rule "Channel: officer: create/write own only", the
  `Officer` user can only edit (and therefore invite members) on
  courses where he is responsible.

Solution:

  Add a param `raise_on_access` (set to False by default) to the
  `_action_add_members` & `_filter_add_members` methods.

  In the wizard, call the `_action_add_members` method with the
  `raise_on_access` param set to True so that the error is only raised
  in this flow.

opw-3133733

closes odoo/odoo#120114

X-original-commit: 515912108aef86376349df5eee51b4a5d664d2a4
Signed-off-by: Warnon Aurélien (awa) <awa@odoo.com>
Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
This commit is contained in:
Nasreddin Boulif (bon)
2023-05-04 08:56:07 +02:00
parent 620c901847
commit 38aaaeb27e
6 changed files with 36 additions and 15 deletions
@@ -60,8 +60,8 @@ class SlideChannelPartner(models.Model):
class Channel(models.Model):
_inherit = 'slide.channel'
def _action_add_members(self, target_partners):
res = super()._action_add_members(target_partners)
def _action_add_members(self, target_partners, raise_on_access=False):
res = super()._action_add_members(target_partners, raise_on_access=raise_on_access)
for channel in self:
channel._message_employee_chatter(
Markup(_('The employee subscribed to the course <a href="%(link)s">%(course)s</a>')) % {
@@ -351,6 +351,15 @@ msgid ""
" used in your description (heading 1, heading 2, ...)"
msgstr ""
#. module: website_sale_slides
#. odoo-python
#: code:addons/website_sale_slides/models/slide_channel.py:0
#, python-format
msgid ""
"You are not allowed to add members to this course. Please contact the course"
" responsible or an administrator."
msgstr ""
#. module: website_sale_slides
#: code:addons/website_sale_slides/models/sale_order.py:0
#, python-format
@@ -1,7 +1,8 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models
from odoo import api, fields, models, _
from odoo.exceptions import AccessError
class Channel(models.Model):
@@ -75,17 +76,18 @@ class Channel(models.Model):
action['domain'] = [('product_id', 'in', self.product_id.ids)]
return action
def _filter_add_members(self, target_partners):
def _filter_add_members(self, target_partners, raise_on_access=False):
""" Overridden to add 'payment' channels to the filtered channels. People
that can write on payment-based channels can add members. """
result = super(Channel, self)._filter_add_members(target_partners)
result = super(Channel, self)._filter_add_members(target_partners, raise_on_access=raise_on_access)
on_payment = self.filtered(lambda channel: channel.enroll == 'payment')
if on_payment:
try:
on_payment.check_access_rights('write')
on_payment.check_access_rule('write')
except:
pass
except AccessError:
if raise_on_access:
raise AccessError(_('You are not allowed to add members to this course. Please contact the course responsible or an administrator.'))
else:
result |= on_payment
return result
@@ -5352,6 +5352,15 @@ msgstr ""
msgid "Yes"
msgstr ""
#. module: website_slides
#. odoo-python
#: code:addons/website_slides/models/slide_channel.py:0
#, python-format
msgid ""
"You are not allowed to add members to this course. Please contact the course"
" responsible or an administrator."
msgstr ""
#. module: website_slides
#. odoo-javascript
#: code:addons/website_slides/static/src/js/tours/slides_tour.js:0
@@ -731,20 +731,20 @@ class Channel(models.Model):
'context': local_context,
}
def _action_add_member(self):
def _action_add_member(self, raise_on_access=False):
""" Adds the logged in user in the channel members.
(see '_action_add_members' for more info)
Returns True if added successfully, False otherwise."""
return bool(self._action_add_members(self.env.user.partner_id))
return bool(self._action_add_members(self.env.user.partner_id, raise_on_access=raise_on_access))
def _action_add_members(self, target_partners):
def _action_add_members(self, target_partners, raise_on_access=False):
""" Add the target_partner as a member of the channel (to its slide.channel.partner).
This will make the content (slides) of the channel available to that partner.
Returns the added 'slide.channel.partner's (! as sudo !)
"""
to_join = self._filter_add_members(target_partners)
to_join = self._filter_add_members(target_partners, raise_on_access=raise_on_access)
if to_join:
existing_channel_partners = self.env['slide.channel.partner'].with_context(active_test=False).sudo().search([
('channel_id', 'in', self.ids),
@@ -769,15 +769,16 @@ class Channel(models.Model):
return slide_partners_sudo | archived_members
return self.env['slide.channel.partner'].sudo()
def _filter_add_members(self, target_partners):
def _filter_add_members(self, target_partners, raise_on_access=False):
allowed = self.filtered(lambda channel: channel.enroll == 'public')
on_invite = self.filtered(lambda channel: channel.enroll == 'invite')
if on_invite:
try:
on_invite.check_access_rights('write')
on_invite.check_access_rule('write')
except:
pass
except AccessError:
if raise_on_access:
raise AccessError(_('You are not allowed to add members to this course. Please contact the course responsible or an administrator.'))
else:
allowed |= on_invite
return allowed
@@ -56,7 +56,7 @@ class SlideChannelInvite(models.TransientModel):
mail_values = []
for partner_id in self.partner_ids:
slide_channel_partner = self.channel_id._action_add_members(partner_id)
slide_channel_partner = self.channel_id._action_add_members(partner_id, raise_on_access=True)
if slide_channel_partner:
mail_values.append(self._prepare_mail_values(slide_channel_partner))