Přejít na obsah
Menu
You need to be registered to interact with the community.
This question has been flagged
2 Odpovědi
736 Zobrazení

Hello,

I would like to only allow users from my own company to signup to my ecommerce website. They all have an email address ending with MyCompany domain name like this "XXXX@MyCompany.com" so I would like to restrict new signups based on the email address.

How to best implement this?

Any help would be much appreciated 🙏

Avatar
Zrušit

You can restrict the creation of new users in Odoo.



With this setting, only users with your invitation can create an account in Odoo.

1. Override the Signup Controller

Modify Odoo’s auth_signup controller to check the email domain before allowing registration.

📌 Steps:

  1. Create a new module or update your existing custom module.
  2. Extend the AuthSignupHome controller to restrict email domains.

📌 Example Code (Python - Controller Override):

python

CopyEdit

from odoo import http from odoo.http import request from odoo.addons.auth_signup.controllers.main import AuthSignupHome ALLOWED_DOMAIN = "mycompany.com" class CustomAuthSignup(AuthSignupHome): def do_signup(self, qcontext): email = qcontext.get('email', '').strip().lower() if not email.endswith(f"@{ALLOWED_DOMAIN}"): raise ValueError("Signup is restricted to MyCompany employees only.") return super(CustomAuthSignup, self).do_signup(qcontext)

2. Modify Signup Form to Show Error Message

You can customize the signup page template to display a warning message if users enter an invalid email.

📌 Steps:

  1. Edit the views/auth_signup_login.xml file in your custom module.
  2. Add a validation script to check the email before submission.

📌 Example (JavaScript - Client-Side Validation):

javascript

CopyEdit

document.addEventListener("DOMContentLoaded", function () { document.querySelector("#signup_form").addEventListener("submit", function (event) { var email = document.querySelector("input[name='email']").value; if (!email.endsWith("@mycompany.com")) { alert("Signup is restricted to MyCompany employees only."); event.preventDefault(); } }); });

3. Hide Signup Option for Non-Company Emails

If you want to completely disable signup for external users, you can restrict the “Sign Up” button in the website settings:

📌 Steps:

  1. Go to Website > Configuration > Settings.
  2. Under Customer Accounts, select “Customers Only”.
  3. Enable Login Required to prevent public users from accessing the signup page.

Nejlepší odpověď

If you're not developer than this is the easiest solution you can implement by yourself:

  1. Go to Automated Actions:
    • In Odoo, navigate to "Settings" (or "Technical Settings" if you don't see Settings).
    • Search for "Automated Actions" and open it.
  2. Create a New Automated Action:
    • Click "Create".
  3. Configure the Automated Action:
    • Model: Select "Users" (res.users).
    • Trigger: Select "On Creation".
    • Apply on: Leave as "All Records" (or you can add a condition if needed).
    • Action To Do: Select "Execute Python Code".
  4. Python Code:
    • In the "Python Code" section, enter the following code:
# Allow only users with email ending in "@mycompany.com"
allowed_domain = "@mycompany.com"
user_email = record.email or record.login  # fallback to login if email is not set

if user_email and not user_email.lower().endswith(allowed_domain.lower()):
    raise UserError("Signup is restricted to users with @mycompany.com email addresses.")

5. Save and Close


If you know how to code then use this solution:
Add this code in some module or create your own custom module and add this code to fix it.

from odoo import models, fields, api
from odoo.exceptions import ValidationError

class ResUsers(models.Model):
    _inherit = 'res.users'

    @api.model
    def create(self, vals):
        if 'login' in vals and not vals['login'].endswith('@MyCompany.com'):
            raise ValidationError("Only users with @MyCompany.com email addresses can sign up.")
        return super(ResUsers, self).create(vals)


I hope it helps.

Thanks,
Abhay

Avatar
Zrušit
Autor Nejlepší odpověď

Hello,

Thank you for your answer, it is very helpful.

I am going the automated action way, however your code raises an error:

forbidden opcode(s) in '# Available variables:\n#  - env: environment on which the action is triggered\n#  - model: model of the record on which the action is triggered; is a void recordset\n#  - record: record on which the action is triggered; may be void\n#  - records: recordset of all records on which the action is triggered in multi-mode; may be void\n#  - time, datetime, dateutil, timezone: useful Python libraries\n#  - float_compare: utility function to compare floats based on specific precision\n#  - b64encode, b64decode: functions to encode/decode binary data\n#  - log: log(message, level=\'info\'): logging function to record debug information in ir.logging table\n#  - _logger: _logger.info(message): logger to emit messages in server logs\n#  - UserError: exception class for raising user-facing warning messages\n#  - Command: x2many commands namespace\n# To return an action, assign: action = {...}\n\nif record.login and "@MyCompany.com" not in record.login:\n    record.active = False\n    tag_id = env[\'ir.model.data\']._xmlid_to_res_id(\'your_module.invalid_email_tag\')  # Replace with your tag\'s XML ID\n    if tag_id:\n        record.write({\'category_id\': [(4, tag_id)]})\n    record.message_post(body="User deactivated and tagged: Invalid email domain. Only @MyCompany.com emails are allowed.")': STORE_ATTR

Any help with that?

Avatar
Zrušit

I updated my code in the original comment, try now and let me know if it works for you

Related Posts Odpovědi Zobrazení Aktivita
2
bře 25
882
2
čvc 25
939
0
bře 25
712
0
čvc 25
2
1
čvc 25
1315