Skip to content

Commit

Permalink
[MIG] auth_user_case_insensitive: Migration to 18.0
Browse files Browse the repository at this point in the history
  • Loading branch information
El-khamisi committed Jan 23, 2025
1 parent 55e9917 commit 13f74eb
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 44 deletions.
34 changes: 32 additions & 2 deletions auth_user_case_insensitive/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(

Check warning on line 24 in auth_user_case_insensitive/__init__.py

View check run for this annotation

Codecov / codecov/patch

auth_user_case_insensitive/__init__.py#L24

Added line #L24 was not covered by tests
_("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)")
2 changes: 1 addition & 1 deletion auth_user_case_insensitive/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down
34 changes: 0 additions & 34 deletions auth_user_case_insensitive/hooks.py

This file was deleted.

8 changes: 5 additions & 3 deletions auth_user_case_insensitive/models/res_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions auth_user_case_insensitive/tests/test_res_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)

0 comments on commit 13f74eb

Please sign in to comment.