diff --git a/setup/website_recaptcha_v2_form/odoo/addons/website_recaptcha_v2_form b/setup/website_recaptcha_v2_form/odoo/addons/website_recaptcha_v2_form new file mode 120000 index 0000000000..c5585f7f8b --- /dev/null +++ b/setup/website_recaptcha_v2_form/odoo/addons/website_recaptcha_v2_form @@ -0,0 +1 @@ +../../../../website_recaptcha_v2_form \ No newline at end of file diff --git a/setup/website_recaptcha_v2_form/setup.py b/setup/website_recaptcha_v2_form/setup.py new file mode 100644 index 0000000000..28c57bb640 --- /dev/null +++ b/setup/website_recaptcha_v2_form/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/website_recaptcha_v2_form/README.rst b/website_recaptcha_v2_form/README.rst new file mode 100644 index 0000000000..d15095a54f --- /dev/null +++ b/website_recaptcha_v2_form/README.rst @@ -0,0 +1,108 @@ +========================= +Website reCAPTCHA v2 form +========================= + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:82c3625280a3850676447d821ae41a770088a52928f19dc3d67a5aa695b2e677 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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%2Fhttps://github.com/BinhexTeam/website.git-lightgray.png?logo=github + :target: https://github.com/OCA/https://github.com/BinhexTeam/website.git/tree/16.0-add-website_recaptcha_v2_form/website_recaptcha_v2_form + :alt: OCA/https://github.com/BinhexTeam/website.git +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/https://github.com/BinhexTeam/website.git-16-0-add-website_recaptcha_v2_form/https://github.com/BinhexTeam/website.git-16-0-add-website_recaptcha_v2_form-website_recaptcha_v2_form + :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/https://github.com/BinhexTeam/website.git&target_branch=16.0-add-website_recaptcha_v2_form + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module allows you to use the recaptcha in its version 2 for the +login, password reset and signup views; it is also configured as needed +in the Form type snippets that appear on the Website. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +To configure this module, you need to: + +#. Go to **Website > Configuration > Settings**. + +#. Search 'reCAPTCHA v2' option. + +|reCaptcha v2| + +#. Click the link `Get reCAPTCHA v2 +keys `__ to generate the keys +needed to use the recaptcha. + +|Get reCAPTCHA v2 keys| + +#. Fill in the generated **Site Key** and **Secret key**. + +|reCAPTCHA v2 keys| + +#. Enable recaptcha in form website. |reCAPTCHA v2 enable website| + +.. |reCaptcha v2| image:: https://raw.githubusercontent.com/OCA/https:/github.com/BinhexTeam/website.git/16.0-add-website_recaptcha_v2_form/website_recaptcha_v2_form/static/src/img/readme/img.png +.. |Get reCAPTCHA v2 keys| image:: https://raw.githubusercontent.com/OCA/https:/github.com/BinhexTeam/website.git/16.0-add-website_recaptcha_v2_form/website_recaptcha_v2_form/static/src/img/readme/img_1.png +.. |reCAPTCHA v2 keys| image:: https://raw.githubusercontent.com/OCA/https:/github.com/BinhexTeam/website.git/16.0-add-website_recaptcha_v2_form/website_recaptcha_v2_form/static/src/img/readme/img_2.png +.. |reCAPTCHA v2 enable website| image:: https://raw.githubusercontent.com/OCA/https:/github.com/BinhexTeam/website.git/16.0-add-website_recaptcha_v2_form/website_recaptcha_v2_form/static/src/img/readme/img_3.png + +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 +------- + +* Binhex + +Contributors +------------ + +-``BINHEX ``\ \_: + +- Edilio Escalona Almira + +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/https://github.com/BinhexTeam/website.git `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/website_recaptcha_v2_form/__init__.py b/website_recaptcha_v2_form/__init__.py new file mode 100644 index 0000000000..f7209b1710 --- /dev/null +++ b/website_recaptcha_v2_form/__init__.py @@ -0,0 +1,2 @@ +from . import models +from . import controllers diff --git a/website_recaptcha_v2_form/__manifest__.py b/website_recaptcha_v2_form/__manifest__.py new file mode 100644 index 0000000000..fd648ea266 --- /dev/null +++ b/website_recaptcha_v2_form/__manifest__.py @@ -0,0 +1,30 @@ +{ + "name": "Website reCAPTCHA v2 form", + "version": "16.0.1.0.0", + "category": "Website", + "depends": ["web", "auth_signup", "website", "website_recaptcha_v2"], + "author": """ + Binhex, + Odoo Community Association (OCA) + """, + "license": "AGPL-3", + "website": "https://github.com/OCA/website", + "summary": """ Module that allows you to use recaptcha v2 for login, password reset, + signup and snippet form on the website. + """, + "data": [ + "views/webclient_templates.xml", + "views/auth_signup_login_templates.xml", + "views/s_website_form.xml", + ], + "assets": { + "website.assets_wysiwyg": [ + "website_recaptcha_v2_form/static/src/xml/website_form_editor.xml", + "website_recaptcha_v2_form/static/src/snippets/s_website_form/options.js", + ], + "web.assets_frontend": [ + "website_recaptcha_v2_form/static/src/css/recaptcha.css", + ], + }, + "installable": True, +} diff --git a/website_recaptcha_v2_form/controllers/__init__.py b/website_recaptcha_v2_form/controllers/__init__.py new file mode 100644 index 0000000000..b26463d6cb --- /dev/null +++ b/website_recaptcha_v2_form/controllers/__init__.py @@ -0,0 +1,2 @@ +from . import main +from . import form diff --git a/website_recaptcha_v2_form/controllers/form.py b/website_recaptcha_v2_form/controllers/form.py new file mode 100644 index 0000000000..590efff291 --- /dev/null +++ b/website_recaptcha_v2_form/controllers/form.py @@ -0,0 +1,30 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +import json + +from odoo import http + +from odoo.addons.website.controllers.form import WebsiteForm + +from .main import RecaptchaHome + + +class WebsiteRecaptchaForm(WebsiteForm): + @http.route( + "/website/form/", + type="http", + auth="public", + methods=["POST"], + website=True, + csrf=False, + ) + def website_form(self, model_name, **kwargs): + if kwargs.get("recaptcha_enabled", False): + valid = RecaptchaHome.verify_recaptcha_v2(self, values=kwargs) + if not isinstance(valid, bool): + return json.dumps( + { + "error": valid, + } + ) + return super().website_form(model_name, **kwargs) diff --git a/website_recaptcha_v2_form/controllers/main.py b/website_recaptcha_v2_form/controllers/main.py new file mode 100644 index 0000000000..bd9026ee25 --- /dev/null +++ b/website_recaptcha_v2_form/controllers/main.py @@ -0,0 +1,85 @@ +import logging + +from odoo import _, http +from odoo.exceptions import AccessDenied +from odoo.http import request + +from odoo.addons.auth_signup.controllers.main import AuthSignupHome +from odoo.addons.web.controllers.home import SIGN_UP_REQUEST_PARAMS, Home + +logger = logging.getLogger(__name__) + +SIGN_UP_REQUEST_PARAMS.add("g-recaptcha-response") + + +class RecaptchaHome(Home): + def verify_recaptcha_v2(self, kw=None, template="", values=None): + Website = request.env["website"].sudo() + try: + request.env["ir.http"]._auth_method_public() + valid = Website.get_current_website().valid_recaptcha(values) + if valid: + if template == "web.login": + return super().web_login(values.get("redirect", ""), **kw) + else: + return True + except AccessDenied as e: + message_error = str( + e.args[0] if len(e.args) > 0 else _("Recaptcha is not valid.") + ) + if template in ( + "web.login", + "auth_signup.reset_password", + "auth_signup.signup", + ): + values.update({"error": message_error}) + response = request.render(template, values) + response.headers["X-Frame-Options"] = "SAMEORIGIN" + response.headers["Content-Security-Policy"] = "frame-ancestors 'self'" + return response + else: + return message_error + + @http.route("/web/login", type="http", auth="none") + def web_login(self, redirect=None, **kw): + if request.httprequest.method == "POST": + values = { + k: v for k, v in request.params.items() if k in SIGN_UP_REQUEST_PARAMS + } + # Checking that if the request comes from the creation of the account, + # that the recaptcha is not checked again to avoid errors. + + if ( + values.get("confirm_password", "") == "" + and request.httprequest.url.find("web/signup") == -1 + ): + return self.verify_recaptcha_v2( + kw=kw, template="web.login", values=values + ) + return super().web_login(redirect, **kw) + + +class RecaptchaAuthSignupHome(AuthSignupHome): + @http.route( + "/web/reset_password", type="http", auth="public", website=True, sitemap=False + ) + def web_auth_reset_password(self, *args, **kw): + qcontext = self.get_auth_signup_qcontext() + if request.httprequest.method == "POST": + valid = self.verify_recaptcha_v2( + kw=kw, template="auth_signup.reset_password", values=qcontext + ) + if not isinstance(valid, bool): + return valid + return super().web_auth_reset_password(*args, **kw) + + @http.route("/web/signup", type="http", auth="public", website=True, sitemap=False) + def web_auth_signup(self, *args, **kw): + qcontext = self.get_auth_signup_qcontext() + if request.httprequest.method == "POST": + valid = self.verify_recaptcha_v2( + template="auth_signup.signup", values=qcontext + ) + if not isinstance(valid, bool): + return valid + return super().web_auth_signup(*args, **kw) diff --git a/website_recaptcha_v2_form/i18n/es.po b/website_recaptcha_v2_form/i18n/es.po new file mode 100644 index 0000000000..ad619481ec --- /dev/null +++ b/website_recaptcha_v2_form/i18n/es.po @@ -0,0 +1,28 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * binhex_website_recaptcha_v2 +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2024-12-02 16:23+0000\n" +"PO-Revision-Date: 2024-12-02 16:23+0000\n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: binhex_website_recaptcha_v2 +#. odoo-python +#: code:addons/binhex_website_recaptcha_v2/controllers/main.py:0 +#, python-format +msgid "Recaptcha is not valid." +msgstr "Recaptcha no es válido." + +#. module: binhex_website_recaptcha_v2 +#: model:ir.model,name:binhex_website_recaptcha_v2.model_website +msgid "Website" +msgstr "Sitio web" diff --git a/website_recaptcha_v2_form/models/__init__.py b/website_recaptcha_v2_form/models/__init__.py new file mode 100644 index 0000000000..74b81abfff --- /dev/null +++ b/website_recaptcha_v2_form/models/__init__.py @@ -0,0 +1,2 @@ +from . import website +from . import res_config_settings diff --git a/website_recaptcha_v2_form/models/res_config_settings.py b/website_recaptcha_v2_form/models/res_config_settings.py new file mode 100644 index 0000000000..b525dff23e --- /dev/null +++ b/website_recaptcha_v2_form/models/res_config_settings.py @@ -0,0 +1,31 @@ +from odoo import api, models + + +class ResConfigSettings(models.TransientModel): + _inherit = "res.config.settings" + + @api.onchange("recaptcha_v2_site_key") + def onchange_recaptcha_v2_site_key(self): + views_recaptcha = ( + self.env["ir.ui.view"] + .sudo() + .search( + [ + ("arch_db", "ilike", 'class="g-recaptcha"'), + ("website_id", "!=", False), + ] + ) + ) + if views_recaptcha: + site_key_old = views_recaptcha.arch_db.split('data-sitekey="') + if len(site_key_old) > 1: + site_key_old = site_key_old[1].split('"')[0] + if site_key_old: + div_start = '
', + f'{div_start} "{self.recaptcha_v2_site_key}" {div_end}/>', + ) + views_recaptcha.sudo().write({"arch": updated_arch}) diff --git a/website_recaptcha_v2_form/models/website.py b/website_recaptcha_v2_form/models/website.py new file mode 100644 index 0000000000..18d1cc896e --- /dev/null +++ b/website_recaptcha_v2_form/models/website.py @@ -0,0 +1,16 @@ +from odoo import api, models +from odoo.exceptions import AccessDenied + + +class Website(models.Model): + _inherit = "website" + + def valid_recaptcha(self, values): + valid, message = self.is_recaptcha_v2_valid(values) + if not valid: + raise AccessDenied(message) + return True + + @api.model + def get_recaptcha_v2_site_key(self): + return self.sudo().get_current_website().recaptcha_v2_site_key diff --git a/website_recaptcha_v2_form/readme/CONFIGURE.md b/website_recaptcha_v2_form/readme/CONFIGURE.md new file mode 100644 index 0000000000..012a2de128 --- /dev/null +++ b/website_recaptcha_v2_form/readme/CONFIGURE.md @@ -0,0 +1,19 @@ + To configure this module, you need to: + +#. Go to **Website > Configuration > Settings**. + +#. Search 'reCAPTCHA v2' option. + +![reCaptcha v2](../static/src/img/readme/img.png) + +#. Click the link [Get reCAPTCHA v2 keys](https://www.google.com/recaptcha/admin) + to generate the keys needed to use the recaptcha. + +![Get reCAPTCHA v2 keys](../static/src/img/readme/img_1.png) + +#. Fill in the generated **Site Key** and **Secret key**. + +![reCAPTCHA v2 keys](../static/src/img/readme/img_2.png) + +#. Enable recaptcha in form website. +![reCAPTCHA v2 enable website](../static/src/img/readme/img_3.png) diff --git a/website_recaptcha_v2_form/readme/CONTRIBUTORS.md b/website_recaptcha_v2_form/readme/CONTRIBUTORS.md new file mode 100644 index 0000000000..7c56c94d4c --- /dev/null +++ b/website_recaptcha_v2_form/readme/CONTRIBUTORS.md @@ -0,0 +1,3 @@ +-`BINHEX `_: + + - Edilio Escalona Almira diff --git a/website_recaptcha_v2_form/readme/DESCRIPTION.md b/website_recaptcha_v2_form/readme/DESCRIPTION.md new file mode 100644 index 0000000000..2c2e102a86 --- /dev/null +++ b/website_recaptcha_v2_form/readme/DESCRIPTION.md @@ -0,0 +1,3 @@ +This module allows you to use the recaptcha in its version 2 for the login, +password reset and signup views; it is also configured as needed in the +Form type snippets that appear on the Website. diff --git a/website_recaptcha_v2_form/static/description/index.html b/website_recaptcha_v2_form/static/description/index.html new file mode 100644 index 0000000000..ab87f33978 --- /dev/null +++ b/website_recaptcha_v2_form/static/description/index.html @@ -0,0 +1,447 @@ + + + + + +Website reCAPTCHA v2 form + + + +
+

Website reCAPTCHA v2 form

+ + +

Beta License: AGPL-3 OCA/https://github.com/BinhexTeam/website.git Translate me on Weblate Try me on Runboat

+

This module allows you to use the recaptcha in its version 2 for the +login, password reset and signup views; it is also configured as needed +in the Form type snippets that appear on the Website.

+

Table of contents

+ +
+

Configuration

+

To configure this module, you need to:

+
    +
  1. Go to Website > Configuration > Settings.
  2. +
  3. Search ‘reCAPTCHA v2’ option.
  4. +
+

reCaptcha v2

+

#. Click the link Get reCAPTCHA v2 +keys to generate the keys +needed to use the recaptcha.

+

Get reCAPTCHA v2 keys

+
    +
  1. Fill in the generated Site Key and Secret key.
  2. +
+

reCAPTCHA v2 keys

+
    +
  1. Enable recaptcha in form website. reCAPTCHA v2 enable website
  2. +
+
+
+

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

+
    +
  • Binhex
  • +
+
+
+

Contributors

+

-BINHEX <https://binhex.cloud>_:

+
    +
  • Edilio Escalona Almira
  • +
+
+
+

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/https://github.com/BinhexTeam/website.git project on GitHub.

+

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

+
+
+
+ + diff --git a/website_recaptcha_v2_form/static/description/recaptcha_ico.png b/website_recaptcha_v2_form/static/description/recaptcha_ico.png new file mode 100644 index 0000000000..65f4e0147f Binary files /dev/null and b/website_recaptcha_v2_form/static/description/recaptcha_ico.png differ diff --git a/website_recaptcha_v2_form/static/src/css/recaptcha.css b/website_recaptcha_v2_form/static/src/css/recaptcha.css new file mode 100644 index 0000000000..2a4de6cb60 --- /dev/null +++ b/website_recaptcha_v2_form/static/src/css/recaptcha.css @@ -0,0 +1,5 @@ +div.s_website_form_recaptcha_v2 > div.g-recaptcha { + margin-left: 18% !important; +} + +/*# sourceMappingURL=recaptcha.css.map */ diff --git a/website_recaptcha_v2_form/static/src/css/recaptcha.css.map b/website_recaptcha_v2_form/static/src/css/recaptcha.css.map new file mode 100644 index 0000000000..5b27d9f714 --- /dev/null +++ b/website_recaptcha_v2_form/static/src/css/recaptcha.css.map @@ -0,0 +1 @@ +{"version":3,"sourceRoot":"","sources":["../scss/recaptcha.scss"],"names":[],"mappings":"AACI;EACI","file":"recaptcha.css"} diff --git a/website_recaptcha_v2_form/static/src/img/readme/img.png b/website_recaptcha_v2_form/static/src/img/readme/img.png new file mode 100644 index 0000000000..8e9875d7cd Binary files /dev/null and b/website_recaptcha_v2_form/static/src/img/readme/img.png differ diff --git a/website_recaptcha_v2_form/static/src/img/readme/img_1.png b/website_recaptcha_v2_form/static/src/img/readme/img_1.png new file mode 100644 index 0000000000..92fbe95fbe Binary files /dev/null and b/website_recaptcha_v2_form/static/src/img/readme/img_1.png differ diff --git a/website_recaptcha_v2_form/static/src/img/readme/img_2.png b/website_recaptcha_v2_form/static/src/img/readme/img_2.png new file mode 100644 index 0000000000..8a9d51d438 Binary files /dev/null and b/website_recaptcha_v2_form/static/src/img/readme/img_2.png differ diff --git a/website_recaptcha_v2_form/static/src/img/readme/img_3.png b/website_recaptcha_v2_form/static/src/img/readme/img_3.png new file mode 100644 index 0000000000..1882ca3aa4 Binary files /dev/null and b/website_recaptcha_v2_form/static/src/img/readme/img_3.png differ diff --git a/website_recaptcha_v2_form/static/src/scss/recaptcha.scss b/website_recaptcha_v2_form/static/src/scss/recaptcha.scss new file mode 100644 index 0000000000..918be7fe04 --- /dev/null +++ b/website_recaptcha_v2_form/static/src/scss/recaptcha.scss @@ -0,0 +1,5 @@ +div.s_website_form_recaptcha_v2 { + > div.g-recaptcha { + margin-left: 18% !important; + } +} diff --git a/website_recaptcha_v2_form/static/src/snippets/s_website_form/options.js b/website_recaptcha_v2_form/static/src/snippets/s_website_form/options.js new file mode 100644 index 0000000000..e9cffdefaa --- /dev/null +++ b/website_recaptcha_v2_form/static/src/snippets/s_website_form/options.js @@ -0,0 +1,44 @@ +odoo.define("website_recaptcha_v2_form.form_editor", function (require) { + "use strict"; + + var options = require("web_editor.snippets.options"); + const core = require("web.core"); + const rpc = require("web.rpc"); + const qweb = core.qweb; + require("website.form_editor"); + + options.registry.WebsiteFormEditor.include({ + willStart: async function () { + var res = this._super(...arguments); + this.recaptcha_site_key = await rpc.query({ + model: "website", + method: "get_recaptcha_v2_site_key", + }); + return res; + }, + toggleRecaptchaV2: async function () { + const recaptchaV2 = this.$target[0].querySelector( + ".s_website_form_recaptcha_v2" + ); + if (recaptchaV2) { + recaptchaV2.remove(); + } else { + const legal = qweb.render("website_recaptcha_v2_form.recaptcha_v2", { + recaptcha_site_key: this.recaptcha_site_key, + }); + this.$target.find(".s_website_form_submit").before(legal); + } + }, + _computeWidgetState: function (methodName, params) { + switch (methodName) { + case "toggleRecaptchaV2": + return ( + !this.$target[0].querySelector( + ".s_website_form_recaptcha_v2" + ) || "" + ); + } + return this._super(methodName, params); + }, + }); +}); diff --git a/website_recaptcha_v2_form/static/src/xml/website_form_editor.xml b/website_recaptcha_v2_form/static/src/xml/website_form_editor.xml new file mode 100644 index 0000000000..7ccfa89e67 --- /dev/null +++ b/website_recaptcha_v2_form/static/src/xml/website_form_editor.xml @@ -0,0 +1,25 @@ + + + + +
+ + +