From 38aaaeb27ed095ed4eca914c52f3e49a4f087608 Mon Sep 17 00:00:00 2001 From: "Nasreddin Boulif (bon)" Date: Mon, 17 Apr 2023 21:54:10 +0000 Subject: [PATCH] [FIX] website_slides: Raise error if not responsible on invite MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) Signed-off-by: Thibault Delavallee (tde) --- addons/hr_skills_slides/models/slide_channel.py | 4 ++-- .../i18n/website_sale_slides.pot | 9 +++++++++ .../website_sale_slides/models/slide_channel.py | 12 +++++++----- addons/website_slides/i18n/website_slides.pot | 9 +++++++++ addons/website_slides/models/slide_channel.py | 15 ++++++++------- .../website_slides/wizard/slide_channel_invite.py | 2 +- 6 files changed, 36 insertions(+), 15 deletions(-) diff --git a/addons/hr_skills_slides/models/slide_channel.py b/addons/hr_skills_slides/models/slide_channel.py index ecfdb40ed93..377686c45e3 100644 --- a/addons/hr_skills_slides/models/slide_channel.py +++ b/addons/hr_skills_slides/models/slide_channel.py @@ -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 %(course)s')) % { diff --git a/addons/website_sale_slides/i18n/website_sale_slides.pot b/addons/website_sale_slides/i18n/website_sale_slides.pot index 82fb3c799ae..f5c3d64c3a2 100644 --- a/addons/website_sale_slides/i18n/website_sale_slides.pot +++ b/addons/website_sale_slides/i18n/website_sale_slides.pot @@ -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 diff --git a/addons/website_sale_slides/models/slide_channel.py b/addons/website_sale_slides/models/slide_channel.py index cbf9b37266b..7041d5cbc32 100644 --- a/addons/website_sale_slides/models/slide_channel.py +++ b/addons/website_sale_slides/models/slide_channel.py @@ -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 diff --git a/addons/website_slides/i18n/website_slides.pot b/addons/website_slides/i18n/website_slides.pot index 8d860372c65..8863c5aa930 100644 --- a/addons/website_slides/i18n/website_slides.pot +++ b/addons/website_slides/i18n/website_slides.pot @@ -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 diff --git a/addons/website_slides/models/slide_channel.py b/addons/website_slides/models/slide_channel.py index 60df5748662..6cb203d3729 100644 --- a/addons/website_slides/models/slide_channel.py +++ b/addons/website_slides/models/slide_channel.py @@ -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 diff --git a/addons/website_slides/wizard/slide_channel_invite.py b/addons/website_slides/wizard/slide_channel_invite.py index a4fbe171073..a6176bff20c 100644 --- a/addons/website_slides/wizard/slide_channel_invite.py +++ b/addons/website_slides/wizard/slide_channel_invite.py @@ -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))