diff --git a/auth_user_case_insensitive/__init__.py b/auth_user_case_insensitive/__init__.py index f16bb110a5..b26a8b2689 100644 --- a/auth_user_case_insensitive/__init__.py +++ b/auth_user_case_insensitive/__init__.py @@ -2,6 +2,36 @@ # Copyright 2021 Open Source Integrators # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). +from odoo import _ +from odoo.exceptions import ValidationError from . import models -from .hooks import pre_init_hook_login_check -from .hooks import post_init_hook_login_convert + + +def pre_init_hook_login_check(env): + """This hook will look to see if any conflicting logins exist before + the module is installed + :param openerp.sql_db.Cursor cr: + Database cursor. + """ + with env.cr.savepoint(): + users = [] + env.cr.execute("SELECT login FROM res_users") + for user in env.cr.fetchall(): + login = user[0].lower() + if login not in users: + users.append(login) + else: + raise ValidationError( + _("Conflicting user logins exist for `%s`", login) + ) + + +def post_init_hook_login_convert(env): + """After the module is installed, set all logins to lowercase + :param openerp.sql_db.Cursor cr: + Database cursor. + :param openerp.modules.registry.RegistryManager registry: + Database registry, using v7 api. + """ + with env.cr.savepoint(): + env.cr.execute("UPDATE res_users SET login=lower(login)") diff --git a/auth_user_case_insensitive/__manifest__.py b/auth_user_case_insensitive/__manifest__.py index 3bb85ee311..2b16fba3b5 100644 --- a/auth_user_case_insensitive/__manifest__.py +++ b/auth_user_case_insensitive/__manifest__.py @@ -4,7 +4,7 @@ { "name": "Case Insensitive Logins", "summary": "Makes the user login field case insensitive", - "version": "18.0.0.0.0", + "version": "18.0.1.0.0", "category": "Authentication", "website": "https://github.com/OCA/server-auth", "author": "LasLabs, Odoo Community Association (OCA)", diff --git a/auth_user_case_insensitive/hooks.py b/auth_user_case_insensitive/hooks.py deleted file mode 100644 index dda15e8057..0000000000 --- a/auth_user_case_insensitive/hooks.py +++ /dev/null @@ -1,34 +0,0 @@ -# Copyright 2017 LasLabs Inc. -# Copyright 2021 Open Source Integrators -# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). - -from odoo import _ -from odoo.exceptions import ValidationError - - -def pre_init_hook_login_check(env): - """This hook will look to see if any conflicting logins exist before - the module is installed - :param env: - Environment. - """ - with env.cr.savepoint(): - users = [] - env.cr.execute("SELECT login FROM res_users") - for user in env.cr.fetchall(): - login = user[0].lower() - if login not in users: - users.append(login) - else: - raise ValidationError( - _("Conflicting user logins exist for `%s`", login) - ) - - -def post_init_hook_login_convert(env): - """After the module is installed, set all logins to lowercase - :param env: - Environment. - """ - with env.cr.savepoint(): - env.cr.execute("UPDATE res_users SET login=lower(login)") diff --git a/auth_user_case_insensitive/models/res_users.py b/auth_user_case_insensitive/models/res_users.py index e6cd365af7..270882b8ff 100644 --- a/auth_user_case_insensitive/models/res_users.py +++ b/auth_user_case_insensitive/models/res_users.py @@ -11,11 +11,13 @@ class ResUsers(models.Model): login = fields.Char(help="Used to log into the system. Case insensitive.") @classmethod - def _login(cls, db, login, password, user_agent_env): + def _login(cls, db, credential, user_agent_env): """Overload _login to lowercase the `login` before passing to the super.""" - login = login.lower() - return super()._login(db, login, password, user_agent_env=user_agent_env) + if credential.get("type") and credential["type"] == "password": + credential["login"] = credential["login"].lower() + + return super()._login(db, credential, user_agent_env) @api.model_create_multi def create(self, vals_list): diff --git a/auth_user_case_insensitive/tests/test_res_users.py b/auth_user_case_insensitive/tests/test_res_users.py index c6b7645ddd..5dafcbcffe 100644 --- a/auth_user_case_insensitive/tests/test_res_users.py +++ b/auth_user_case_insensitive/tests/test_res_users.py @@ -45,15 +45,15 @@ def test_login_is_lowercased_on_write(self): def test_login_login_is_lowercased(self): """verify the login is set to lowercase on login.""" rec_id = self.model_obj.search([("login", "=", "admin")]) - res_id = self.model_obj._login( + credential = {"login": "AdMiN", "password": "admin", "type": "password"} + auth_info = self.model_obj._login( self.env.registry.db_name, - "AdMiN", - "admin", + credential, {"interactive": True}, ) self.assertEqual( rec_id.id, - res_id, + auth_info["uid"], "Login with with uppercase chars was not \ successful", )