From 99f4550e682d36aaf5688f12c8e1aea57c032fde Mon Sep 17 00:00:00 2001 From: David Date: Mon, 8 Jul 2024 16:58:02 +0200 Subject: [PATCH] [ADD] website_conditional_visibility_user_group: New module TT50008 --- .../website_conditional_visibility_user_group | 1 + .../setup.py | 6 + .../README.rst | 94 ++++ .../__init__.py | 1 + .../__manifest__.py | 28 ++ .../models/__init__.py | 1 + .../models/ir_http.py | 20 + .../readme/CONFIGURE.md | 9 + .../readme/CONTRIBUTORS.md | 2 + .../readme/DESCRIPTION.md | 2 + .../static/description/index.html | 441 ++++++++++++++++++ .../static/src/js/inject_dom.esm.js | 9 + .../src/tests/snippet_visibility_tests.esm.js | 91 ++++ .../tests/__init__.py | 1 + .../test_conditional_visibility_user_group.py | 11 + .../views/snippet_options_template.xml | 48 ++ 16 files changed, 765 insertions(+) create mode 120000 setup/website_conditional_visibility_user_group/odoo/addons/website_conditional_visibility_user_group create mode 100644 setup/website_conditional_visibility_user_group/setup.py create mode 100644 website_conditional_visibility_user_group/README.rst create mode 100644 website_conditional_visibility_user_group/__init__.py create mode 100644 website_conditional_visibility_user_group/__manifest__.py create mode 100644 website_conditional_visibility_user_group/models/__init__.py create mode 100644 website_conditional_visibility_user_group/models/ir_http.py create mode 100644 website_conditional_visibility_user_group/readme/CONFIGURE.md create mode 100644 website_conditional_visibility_user_group/readme/CONTRIBUTORS.md create mode 100644 website_conditional_visibility_user_group/readme/DESCRIPTION.md create mode 100644 website_conditional_visibility_user_group/static/description/index.html create mode 100644 website_conditional_visibility_user_group/static/src/js/inject_dom.esm.js create mode 100644 website_conditional_visibility_user_group/static/src/tests/snippet_visibility_tests.esm.js create mode 100644 website_conditional_visibility_user_group/tests/__init__.py create mode 100644 website_conditional_visibility_user_group/tests/test_conditional_visibility_user_group.py create mode 100644 website_conditional_visibility_user_group/views/snippet_options_template.xml diff --git a/setup/website_conditional_visibility_user_group/odoo/addons/website_conditional_visibility_user_group b/setup/website_conditional_visibility_user_group/odoo/addons/website_conditional_visibility_user_group new file mode 120000 index 0000000000..71de1c5d0c --- /dev/null +++ b/setup/website_conditional_visibility_user_group/odoo/addons/website_conditional_visibility_user_group @@ -0,0 +1 @@ +../../../../website_conditional_visibility_user_group \ No newline at end of file diff --git a/setup/website_conditional_visibility_user_group/setup.py b/setup/website_conditional_visibility_user_group/setup.py new file mode 100644 index 0000000000..28c57bb640 --- /dev/null +++ b/setup/website_conditional_visibility_user_group/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/website_conditional_visibility_user_group/README.rst b/website_conditional_visibility_user_group/README.rst new file mode 100644 index 0000000000..ee0a86b996 --- /dev/null +++ b/website_conditional_visibility_user_group/README.rst @@ -0,0 +1,94 @@ +==================================================== +Conditional visibility for internal users in Website +==================================================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:886fcbb9151837c653c43993188f47b0cb30031f5c7e184aa2b5c83c62649da2 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fwebsite-lightgray.png?logo=github + :target: https://github.com/OCA/website/tree/15.0/website_conditional_visibility_user_group + :alt: OCA/website +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/website-15-0/website-15-0-website_conditional_visibility_user_group + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/website&target_branch=15.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +Thsi module adds extra granularity in the visibility of the website +snippets allowing to set if the users that can see a block are internal +or portal ones. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +To configure the user group visibility: + +- With the frontend editor opened select the block you want to set the + visibility for. +- In *Style* tab of the editor panel set the *Visibility* option to + *Conditional* and unfold it. +- Set the *Users* option to *Visible for logged in*. +- Now you'll see the new *Groups* option. +- You can choose between *Only internal users*, *Only portal users* or + *No restriction*, which is the default value. + +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 to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* Tecnativa + +Contributors +------------ + +- `Tecnativa `__ + + - David Vidal + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +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_conditional_visibility_user_group/__init__.py b/website_conditional_visibility_user_group/__init__.py new file mode 100644 index 0000000000..0650744f6b --- /dev/null +++ b/website_conditional_visibility_user_group/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/website_conditional_visibility_user_group/__manifest__.py b/website_conditional_visibility_user_group/__manifest__.py new file mode 100644 index 0000000000..d7fe90cb76 --- /dev/null +++ b/website_conditional_visibility_user_group/__manifest__.py @@ -0,0 +1,28 @@ +# Copyright 2024 Tecnativa - David Vidal +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Conditional visibility for internal users in Website", + "summary": "Only internal users will see the blocks you add this condition to", + "version": "15.0.1.0.0", + "category": "Product", + "website": "https://github.com/OCA/website", + "author": "Tecnativa, Odoo Community Association (OCA)", + "license": "AGPL-3", + "depends": ["website"], + "data": ["views/snippet_options_template.xml"], + "assets": { + "web.assets_frontend_minimal": [ + "website_conditional_visibility_user_group/static/src/js/*.js", + ], + "web.assets_frontend_lazy": [ + # Remove assets_frontend_minimal + ( + "remove", + "website_conditional_visibility_user_group/static/src/js/*.js", + ), + ], + "web.assets_tests": [ + "website_conditional_visibility_user_group/static/src/tests/*.js" + ], + }, +} diff --git a/website_conditional_visibility_user_group/models/__init__.py b/website_conditional_visibility_user_group/models/__init__.py new file mode 100644 index 0000000000..9a5eb71871 --- /dev/null +++ b/website_conditional_visibility_user_group/models/__init__.py @@ -0,0 +1 @@ +from . import ir_http diff --git a/website_conditional_visibility_user_group/models/ir_http.py b/website_conditional_visibility_user_group/models/ir_http.py new file mode 100644 index 0000000000..5bbd10afbc --- /dev/null +++ b/website_conditional_visibility_user_group/models/ir_http.py @@ -0,0 +1,20 @@ +# Copyright 2024 Tecnativa - David Vidal +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo import api, models +from odoo.http import request + + +class IrHttp(models.AbstractModel): + _inherit = "ir.http" + + @api.model + def get_frontend_session_info(self): + # Used for the visibility of the website snippets + session_info = super().get_frontend_session_info() + if request.env.user.has_group("base.group_user"): + session_info["user_group"] = "internal" + elif request.env.user.has_group("base.group_portal"): + session_info["user_group"] = "portal" + else: + session_info["user_group"] = "public" + return session_info diff --git a/website_conditional_visibility_user_group/readme/CONFIGURE.md b/website_conditional_visibility_user_group/readme/CONFIGURE.md new file mode 100644 index 0000000000..b518af04f9 --- /dev/null +++ b/website_conditional_visibility_user_group/readme/CONFIGURE.md @@ -0,0 +1,9 @@ +To configure the user group visibility: + +- With the frontend editor opened select the block you want to set the visibility for. +- In *Style* tab of the editor panel set the *Visibility* option to *Conditional* and + unfold it. +- Set the *Users* option to *Visible for logged in*. +- Now you'll see the new *Groups* option. +- You can choose between *Only internal users*, *Only portal users* or *No restriction*, + which is the default value. diff --git a/website_conditional_visibility_user_group/readme/CONTRIBUTORS.md b/website_conditional_visibility_user_group/readme/CONTRIBUTORS.md new file mode 100644 index 0000000000..96d0c4c7ff --- /dev/null +++ b/website_conditional_visibility_user_group/readme/CONTRIBUTORS.md @@ -0,0 +1,2 @@ +- [Tecnativa](https://tecnativa.com) + - David Vidal diff --git a/website_conditional_visibility_user_group/readme/DESCRIPTION.md b/website_conditional_visibility_user_group/readme/DESCRIPTION.md new file mode 100644 index 0000000000..13d7c9c394 --- /dev/null +++ b/website_conditional_visibility_user_group/readme/DESCRIPTION.md @@ -0,0 +1,2 @@ +Thsi module adds extra granularity in the visibility of the website snippets allowing +to set if the users that can see a block are internal or portal ones. diff --git a/website_conditional_visibility_user_group/static/description/index.html b/website_conditional_visibility_user_group/static/description/index.html new file mode 100644 index 0000000000..9463b0adab --- /dev/null +++ b/website_conditional_visibility_user_group/static/description/index.html @@ -0,0 +1,441 @@ + + + + + + +Conditional visibility for internal users in Website + + + +
+

Conditional visibility for internal users in Website

+ + +

Beta License: AGPL-3 OCA/website Translate me on Weblate Try me on Runboat

+

Thsi module adds extra granularity in the visibility of the website +snippets allowing to set if the users that can see a block are internal +or portal ones.

+

Table of contents

+ +
+

Configuration

+

To configure the user group visibility:

+
    +
  • With the frontend editor opened select the block you want to set the +visibility for.
  • +
  • In Style tab of the editor panel set the Visibility option to +Conditional and unfold it.
  • +
  • Set the Users option to Visible for logged in.
  • +
  • Now you’ll see the new Groups option.
  • +
  • You can choose between Only internal users, Only portal users or +No restriction, which is the default value.
  • +
+
+
+

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 to smash it by providing a detailed and welcomed +feedback.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • Tecnativa
  • +
+
+
+

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_conditional_visibility_user_group/static/src/js/inject_dom.esm.js b/website_conditional_visibility_user_group/static/src/js/inject_dom.esm.js new file mode 100644 index 0000000000..0190631516 --- /dev/null +++ b/website_conditional_visibility_user_group/static/src/js/inject_dom.esm.js @@ -0,0 +1,9 @@ +/** @odoo-module */ +/* Copyright 2024 Tecnativa - David Vidal + * License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */ +import {session} from "@web/session"; + +document.addEventListener("DOMContentLoaded", () => { + const htmlEl = document.documentElement; + htmlEl.dataset.user_group = session.user_group; +}); diff --git a/website_conditional_visibility_user_group/static/src/tests/snippet_visibility_tests.esm.js b/website_conditional_visibility_user_group/static/src/tests/snippet_visibility_tests.esm.js new file mode 100644 index 0000000000..ad8f9d94e1 --- /dev/null +++ b/website_conditional_visibility_user_group/static/src/tests/snippet_visibility_tests.esm.js @@ -0,0 +1,91 @@ +/** @odoo-module */ +/* Copyright 2024 Tecnativa - David Vidal + * License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */ +import tour from "web_tour.tour"; +import wTourUtils from "website.tour_utils"; + +const snippet = { + id: "s_text_image", + name: "Text - Image", +}; + +tour.register( + "conditional_visibility_only_internal_user", + { + test: true, + url: "/", + }, + [ + { + content: "enter edit mode", + trigger: "a[data-action=edit]", + }, + wTourUtils.dragNDrop(snippet), + wTourUtils.clickOnSnippet(snippet), + wTourUtils.changeOption("ConditionalVisibility", "we-toggler"), + { + content: "Set on conditional visibility", + trigger: '[data-name="visibility_conditional"]', + run: "click", + }, + { + content: "Click in User visibility", + trigger: '[data-save-attribute="visibilityValueLogged"]', + run: "click", + }, + { + content: "Set visibility to logged in users", + trigger: '[data-name="visibility_logged_in"]', + run: "click", + }, + { + content: "Click in group visibility", + trigger: '[data-save-attribute="visibilityUserGroup"]', + run: "click", + }, + { + content: "Set visibility to logged internal users only", + trigger: '[data-name="user_group_internal"]', + run: "click", + }, + ...wTourUtils.clickOnSave(), + { + content: "Check if the rule was applied", + trigger: "body:not(.editor_enable) #wrap", + run: () => { + const style = window.getComputedStyle( + this.$anchor[0].getElementsByClassName("s_text_image")[0] + ); + if (style.display === "none") { + console.error( + "Error: this item should be visible for internal users" + ); + } + }, + }, + ] +); + +tour.register( + "conditional_visibility_portal", + { + test: true, + url: "/", + }, + [ + { + content: "The content should be hidden for portal users", + trigger: "body #wrapwrap", + run: () => { + const style = window.getComputedStyle( + this.$anchor[0].getElementsByClassName("s_text_image")[0] + ); + if (style.display !== "none") { + console.error( + "Error: This item should be insvible for portal users" + ); + } + }, + }, + ] +); diff --git a/website_conditional_visibility_user_group/tests/__init__.py b/website_conditional_visibility_user_group/tests/__init__.py new file mode 100644 index 0000000000..d1c4bc70cb --- /dev/null +++ b/website_conditional_visibility_user_group/tests/__init__.py @@ -0,0 +1 @@ +from . import test_conditional_visibility_user_group diff --git a/website_conditional_visibility_user_group/tests/test_conditional_visibility_user_group.py b/website_conditional_visibility_user_group/tests/test_conditional_visibility_user_group.py new file mode 100644 index 0000000000..1f31f10b01 --- /dev/null +++ b/website_conditional_visibility_user_group/tests/test_conditional_visibility_user_group.py @@ -0,0 +1,11 @@ +# Copyright 2024 Tecnativa - David Vidal +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from odoo.tests import HttpCase, tagged + + +@tagged("-at_install", "post_install") +class TestConditionalVisibilityUserGroup(HttpCase): + def test_with_demo(self): + """User demo can see our snippet""" + self.start_tour("/", "conditional_visibility_only_internal_user", login="admin") + self.start_tour("/", "conditional_visibility_portal", login="portal") diff --git a/website_conditional_visibility_user_group/views/snippet_options_template.xml b/website_conditional_visibility_user_group/views/snippet_options_template.xml new file mode 100644 index 0000000000..9b387a8265 --- /dev/null +++ b/website_conditional_visibility_user_group/views/snippet_options_template.xml @@ -0,0 +1,48 @@ + + + + +