From e4404f30c4e7bdc7edd36d0ff819408f9337bac8 Mon Sep 17 00:00:00 2001 From: Ronald Portier Date: Mon, 12 Aug 2019 13:20:41 +0200 Subject: [PATCH] [IMP] website_menu_permission. Better defaults. Easier maintenance. 1. Prevent just installing this module of making most menu's unavailable for logged in users; 2. Make menu's directly configurable from website settings, as not all menu's are linked to a page; 3. Use group_ids field in new website.menu form to edit groups. With this commit, just installing this module will not change anything in existing permissions. TODO: update tests and README.rst. --- website_menu_permission/README.rst | 74 ++- website_menu_permission/__init__.py | 3 +- website_menu_permission/__manifest__.py | 6 +- website_menu_permission/models/__init__.py | 3 +- .../models/website_menu.py | 48 +- .../readme/CONTRIBUTORS.rst | 2 + .../readme/DESCRIPTION.rst | 1 + website_menu_permission/readme/USAGE.rst | 5 + .../security/record_rules.xml | 3 +- .../static/description/index.html | 430 ++++++++++++++++++ .../tests/test_menu_perm.py | 156 +++---- .../views/website_views.xml | 48 +- 12 files changed, 603 insertions(+), 176 deletions(-) create mode 100644 website_menu_permission/readme/CONTRIBUTORS.rst create mode 100644 website_menu_permission/readme/DESCRIPTION.rst create mode 100644 website_menu_permission/readme/USAGE.rst create mode 100644 website_menu_permission/static/description/index.html diff --git a/website_menu_permission/README.rst b/website_menu_permission/README.rst index 456803df32..d275635e58 100644 --- a/website_menu_permission/README.rst +++ b/website_menu_permission/README.rst @@ -1,51 +1,83 @@ -.. image:: https://img.shields.io/badge/licence-lgpl--3-blue.svg - :target: http://www.gnu.org/licenses/LGPL-3.0-standalone.html - :alt: License: LGPL-3 - - ======================= Website Menu Permission ======================= +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png + :target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html + :alt: License: LGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fwebsite-lightgray.png?logo=github + :target: https://github.com/OCA/website/tree/11.0/website_menu_permission + :alt: OCA/website +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/website-11-0/website-11-0-website_menu_permission + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/186/11.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + Show/hide website menu items by user group. -TODO +**Table of contents** +.. contents:: + :local: Usage ===== +To use this module, you need to: -TODO - +#. Go to Website ==> Configuration ==> Website menu's. +#. Edit the menu that you want to limit to certain groups, and add + the names of the groups that should have access. Bug Tracker =========== -Bugs are tracked on `GitHub Issues `_. In -case of trouble, please check there if your issue has already been -reported. If you spotted it first, help us smashing it by providing a -detailed and welcomed feedback. +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. Credits ======= +Authors +~~~~~~~ + +* Camptocamp + Contributors ------------- +~~~~~~~~~~~~ -- Simone Orsi simone.orsi@camptocamp.com +* Simone Orsi simone.orsi@camptocamp.com +* Ronald Portier ronald@therp.nl -Maintainer ----------- +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. .. image:: https://odoo-community.org/logo.png :alt: Odoo Community Association :target: https://odoo-community.org -This module is maintained by the OCA. +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. -OCA, or the Odoo Community Association, is a nonprofit organization -whose mission is to support the collaborative development of Odoo -features and promote its widespread use. +This module is part of the `OCA/website `_ project on GitHub. -To contribute to this module, please visit https://odoo-community.org. +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/website_menu_permission/__init__.py b/website_menu_permission/__init__.py index cde864bae2..29276a93dd 100644 --- a/website_menu_permission/__init__.py +++ b/website_menu_permission/__init__.py @@ -1,3 +1,2 @@ -# -*- coding: utf-8 -*- - +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). from . import models diff --git a/website_menu_permission/__manifest__.py b/website_menu_permission/__manifest__.py index 0887aac5ab..db8f42e4d7 100644 --- a/website_menu_permission/__manifest__.py +++ b/website_menu_permission/__manifest__.py @@ -1,7 +1,5 @@ -# -*- coding: utf-8 -*- -# Copyright 2017 Simone Orsi -# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl) - +# Copyright 2017 Simone Orsi. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). { 'name': 'Website Menu Permission', 'version': '11.0.1.0.0', diff --git a/website_menu_permission/models/__init__.py b/website_menu_permission/models/__init__.py index 2b81276d57..e9aa8b5f46 100644 --- a/website_menu_permission/models/__init__.py +++ b/website_menu_permission/models/__init__.py @@ -1,3 +1,2 @@ -# -*- coding: utf-8 -*- - +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). from . import website_menu diff --git a/website_menu_permission/models/website_menu.py b/website_menu_permission/models/website_menu.py index 76aae593f2..24379d1755 100644 --- a/website_menu_permission/models/website_menu.py +++ b/website_menu_permission/models/website_menu.py @@ -1,55 +1,11 @@ -# -*- coding: utf-8 -*- -# Copyright 2017 Simone Orsi -# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) +# Copyright 2017 Simone Orsi. +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). from odoo import fields, models class WebsiteMenu(models.Model): _inherit = 'website.menu' - # TODO: when we'll have an advanced form for editing groups - # we should add an handler to update flags (like `_update_menu_groups`) group_ids = fields.Many2many( comodel_name='res.groups', ) - user_logged = fields.Boolean( - string="User Logged-in", - default=True, - help="If checked, " - "this item will be available for logged-in users.", - inverse='_update_menu_groups', - ) - user_not_logged = fields.Boolean( - string="User Not Logged-in", - default=True, - help="If checked, " - "this item will be available for not logged-in users.", - inverse='_update_menu_groups', - ) - - @property - def menu_group_public(self): - return self.env.ref('base.group_public') - - @property - def menu_group_logged(self): - return self.env.ref('base.group_portal') - - def _get_updated_groups(self): - """Get update groups recordset for current menu item.""" - groups = self.group_ids - if self.user_logged: - groups |= self.menu_group_logged - else: - groups -= self.menu_group_logged - if self.user_not_logged: - groups |= self.menu_group_public - else: - groups -= self.menu_group_public - return groups - - def _update_menu_groups(self): - if self.env.context.get('ws_menu_skip_group_update'): - return - for item in self: - item.group_ids = item._get_updated_groups() diff --git a/website_menu_permission/readme/CONTRIBUTORS.rst b/website_menu_permission/readme/CONTRIBUTORS.rst new file mode 100644 index 0000000000..eb8c589fd9 --- /dev/null +++ b/website_menu_permission/readme/CONTRIBUTORS.rst @@ -0,0 +1,2 @@ +* Simone Orsi simone.orsi@camptocamp.com +* Ronald Portier ronald@therp.nl diff --git a/website_menu_permission/readme/DESCRIPTION.rst b/website_menu_permission/readme/DESCRIPTION.rst new file mode 100644 index 0000000000..501307423b --- /dev/null +++ b/website_menu_permission/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +Show/hide website menu items by user group. diff --git a/website_menu_permission/readme/USAGE.rst b/website_menu_permission/readme/USAGE.rst new file mode 100644 index 0000000000..8c00f6e6d0 --- /dev/null +++ b/website_menu_permission/readme/USAGE.rst @@ -0,0 +1,5 @@ +To use this module, you need to: + +#. Go to Website ==> Configuration ==> Website menu's. +#. Edit the menu that you want to limit to certain groups, and add + the names of the groups that should have access. diff --git a/website_menu_permission/security/record_rules.xml b/website_menu_permission/security/record_rules.xml index 38616a9ea9..2dd5393f8c 100644 --- a/website_menu_permission/security/record_rules.xml +++ b/website_menu_permission/security/record_rules.xml @@ -4,12 +4,11 @@ website_menu_permission group access - ['|',('group_ids','in', [g.id for g in user.groups_id]), ('group_ids','=',False)] + ['|',('group_ids','in', user.groups_id.ids), ('group_ids','=',False)] - diff --git a/website_menu_permission/static/description/index.html b/website_menu_permission/static/description/index.html new file mode 100644 index 0000000000..78c88d7814 --- /dev/null +++ b/website_menu_permission/static/description/index.html @@ -0,0 +1,430 @@ + + + + + + +Website Menu Permission + + + +
+

Website Menu Permission

+ + +

Beta License: LGPL-3 OCA/website Translate me on Weblate Try me on Runbot

+

Show/hide website menu items by user group.

+

Table of contents

+ +
+

Usage

+

To use this module, you need to:

+
    +
  1. Go to Website ==> Configuration ==> Website menu’s.
  2. +
  3. Edit the menu that you want to limit to certain groups, and add +the names of the groups that should have access.
  4. +
+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Camptocamp
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/website project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/website_menu_permission/tests/test_menu_perm.py b/website_menu_permission/tests/test_menu_perm.py index 175fe5654d..9791d37f14 100644 --- a/website_menu_permission/tests/test_menu_perm.py +++ b/website_menu_permission/tests/test_menu_perm.py @@ -1,98 +1,80 @@ -# -*- coding: utf-8 -*- -# Copyright 2017 Simone Orsi -# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl) +# Copyright 2017 Simone Orsi. +# Copyright 2019 Therp BV . +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). import odoo.tests.common as test_common -class TestMenuPerm(test_common.TransactionCase): +class TestMenuPerm(test_common.SavepointCase): - def setUp(self): - super(TestMenuPerm, self).setUp() - self.group_logged = self.env.ref('base.group_portal') - self.group_public = self.env.ref('base.group_public') - user_model = self.env['res.users'].with_context(**{ + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.env = cls.env(context=dict( + cls.env.context, tracking_disable=True, no_reset_password=True)) + cls.group_portal = cls.env.ref('base.group_portal') + cls.group_user = cls.env.ref('base.group_user') + cls.group_public = cls.env.ref('base.group_public') + user_model = cls.env['res.users'].with_context(**{ 'no_reset_password': True, 'mail_create_nosubscribe': True}) - self.user_public = user_model.create({ - 'name': 'User 1 (test ref)', - 'login': 'testref_user_public', - 'email': 'testref_user_public@email.com', - # make sure to have only portal group - 'groups_id': [(6, 0, [self.group_public.id])] - }) - self.user_logged = user_model.create({ - 'name': 'Public User', - 'login': 'publicuser', - 'email': 'publicuser@example.com', - 'groups_id': [(6, 0, [self.group_logged.id])]} - ) - self.menu_model = self.env['website.menu'] - self.menu_item = self.menu_model.create({'name': 'Foo'}) + cls.employee_user = user_model.create({ + 'name': 'Employee user (test ref)', + 'login': 'testref_employee_user', + 'email': 'testref_employee_user@email.com', + 'groups_id': [(6, 0, [cls.group_user.id])]}) + cls.portal_user = user_model.create({ + 'name': 'Portal user', + 'login': 'portaluser', + 'email': 'portaluser@example.com', + 'groups_id': [(6, 0, [cls.group_portal.id])]}) + cls.public_user = cls.env.ref('base.public_user') + cls.menu_model = cls.env['website.menu'] + cls.menu_item = cls.menu_model.create({'name': 'Foo'}) - def test_menu_groups_default(self): - self.assertIn(self.group_logged, self.menu_item.group_ids) - self.assertIn(self.group_public, self.menu_item.group_ids) + def test_menu_for_all(self): + self.menu_item.write({'group_ids': False}) + self.assertTrue(self._can_see(self.employee_user)) + self.assertTrue(self._can_see(self.public_user)) + self.assertTrue(self._can_see(self.portal_user)) - def test_menu_groups_update(self): - # wipe groups and flags - self.menu_item.with_context(ws_menu_skip_group_update=1).write({ - 'group_ids': False, - 'user_logged': False, - 'user_not_logged': False, - }) - self.menu_item.user_logged = True - self.assertIn(self.group_logged, self.menu_item.group_ids) - self.assertNotIn(self.group_public, self.menu_item.group_ids) - self.menu_item.user_not_logged = True - self.assertIn(self.group_logged, self.menu_item.group_ids) - self.assertIn(self.group_public, self.menu_item.group_ids) - self.menu_item.user_logged = False - self.assertNotIn(self.group_logged, self.menu_item.group_ids) - self.assertIn(self.group_public, self.menu_item.group_ids) - self.menu_item.user_not_logged = False - self.assertNotIn(self.group_logged, self.menu_item.group_ids) - self.assertNotIn(self.group_public, self.menu_item.group_ids) + def test_menu_for_employee(self): + self.menu_item.write({'group_ids': [(6, 0, [self.group_user.id])]}) + self.assertTrue(self._can_see(self.employee_user)) + self.assertFalse(self._can_see(self.public_user)) + self.assertFalse(self._can_see(self.portal_user)) - def test_perm_default_all_can_view(self): - model = self.menu_model.sudo(self.user_public.id) - self.assertIn( - self.menu_item, - model.search([]) - ) - model = self.menu_model.sudo(self.user_logged.id) - self.assertIn( - self.menu_item, - model.search([]) - ) + def test_menu_for_external(self): + self.menu_item.write({'group_ids': [(6, 0, [self.group_portal.id])]}) + self.assertFalse(self._can_see(self.employee_user)) + self.assertFalse(self._can_see(self.public_user)) + self.assertTrue(self._can_see(self.portal_user)) - def test_perm_public_only_can_view(self): - self.menu_item.write({ - 'user_logged': False, - 'user_not_logged': True, - }) - model = self.menu_model.sudo(self.user_public.id) - self.assertIn( - self.menu_item, - model.search([]) - ) - model = self.menu_model.sudo(self.user_logged.id) - self.assertNotIn( - self.menu_item, - model.with_context(foo=1).search([]) - ) + def test_menu_for_logged_in_users(self): + self.menu_item.write( + {'group_ids': [(6, 0, [ + self.group_portal.id, self.group_user.id])]}) + self.assertTrue(self._can_see(self.employee_user)) + self.assertFalse(self._can_see(self.public_user)) + self.assertTrue(self._can_see(self.portal_user)) - def test_perm_logged_only_can_view(self): - self.menu_item.write({ - 'user_logged': True, - 'user_not_logged': False, - }) - model = self.menu_model.sudo(self.user_public.id) - self.assertNotIn( - self.menu_item, - model.search([]) - ) - model = self.menu_model.sudo(self.user_logged.id) - self.assertIn( - self.menu_item, - model.with_context(foo=1).search([]) - ) + def test_menu_for_not_logged_in_users(self): + self.menu_item.write({'group_ids': [(6, 0, [self.group_public.id])]}) + self.assertFalse(self._can_see(self.employee_user)) + self.assertTrue(self._can_see(self.public_user)) + self.assertFalse(self._can_see(self.portal_user)) + + def test_menu_for_all_explicit(self): + self.menu_item.write( + {'group_ids': [(6, 0, [ + self.group_portal.id, + self.group_user.id, + self.group_public.id])]}) + self.assertTrue(self._can_see(self.employee_user)) + self.assertTrue(self._can_see(self.public_user)) + self.assertTrue(self._can_see(self.portal_user)) + + def _can_see(self, user): + """Check wether user can see the test menu.""" + menu_model_sudo = self.menu_model.sudo(user.id) + can_see = self.menu_item in menu_model_sudo.search([]) + return can_see diff --git a/website_menu_permission/views/website_views.xml b/website_menu_permission/views/website_views.xml index 7797a7329b..5eb7b5c968 100644 --- a/website_menu_permission/views/website_views.xml +++ b/website_menu_permission/views/website_views.xml @@ -1,16 +1,40 @@ - - website_menu_permission: show flags - website.menu - - - - - - - - - + + + website.menu.form.permission + website.menu + child_id + +
+ + + + + + + + + +
+
+
+ + + Website Menu + website.menu + tree,form + {'search_default_my_websites':1} + + +