Skip to content

Commit 13f74eb

Browse files
committed
[MIG] auth_user_case_insensitive: Migration to 18.0
1 parent 55e9917 commit 13f74eb

File tree

5 files changed

+42
-44
lines changed

5 files changed

+42
-44
lines changed

auth_user_case_insensitive/__init__.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,36 @@
22
# Copyright 2021 Open Source Integrators
33
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
44

5+
from odoo import _
6+
from odoo.exceptions import ValidationError
57
from . import models
6-
from .hooks import pre_init_hook_login_check
7-
from .hooks import post_init_hook_login_convert
8+
9+
10+
def pre_init_hook_login_check(env):
11+
"""This hook will look to see if any conflicting logins exist before
12+
the module is installed
13+
:param openerp.sql_db.Cursor cr:
14+
Database cursor.
15+
"""
16+
with env.cr.savepoint():
17+
users = []
18+
env.cr.execute("SELECT login FROM res_users")
19+
for user in env.cr.fetchall():
20+
login = user[0].lower()
21+
if login not in users:
22+
users.append(login)
23+
else:
24+
raise ValidationError(
25+
_("Conflicting user logins exist for `%s`", login)
26+
)
27+
28+
29+
def post_init_hook_login_convert(env):
30+
"""After the module is installed, set all logins to lowercase
31+
:param openerp.sql_db.Cursor cr:
32+
Database cursor.
33+
:param openerp.modules.registry.RegistryManager registry:
34+
Database registry, using v7 api.
35+
"""
36+
with env.cr.savepoint():
37+
env.cr.execute("UPDATE res_users SET login=lower(login)")

auth_user_case_insensitive/__manifest__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
{
55
"name": "Case Insensitive Logins",
66
"summary": "Makes the user login field case insensitive",
7-
"version": "18.0.0.0.0",
7+
"version": "18.0.1.0.0",
88
"category": "Authentication",
99
"website": "https://github.com/OCA/server-auth",
1010
"author": "LasLabs, Odoo Community Association (OCA)",

auth_user_case_insensitive/hooks.py

Lines changed: 0 additions & 34 deletions
This file was deleted.

auth_user_case_insensitive/models/res_users.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ class ResUsers(models.Model):
1111
login = fields.Char(help="Used to log into the system. Case insensitive.")
1212

1313
@classmethod
14-
def _login(cls, db, login, password, user_agent_env):
14+
def _login(cls, db, credential, user_agent_env):
1515
"""Overload _login to lowercase the `login` before passing to the
1616
super."""
17-
login = login.lower()
18-
return super()._login(db, login, password, user_agent_env=user_agent_env)
17+
if credential.get("type") and credential["type"] == "password":
18+
credential["login"] = credential["login"].lower()
19+
20+
return super()._login(db, credential, user_agent_env)
1921

2022
@api.model_create_multi
2123
def create(self, vals_list):

auth_user_case_insensitive/tests/test_res_users.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ def test_login_is_lowercased_on_write(self):
4545
def test_login_login_is_lowercased(self):
4646
"""verify the login is set to lowercase on login."""
4747
rec_id = self.model_obj.search([("login", "=", "admin")])
48-
res_id = self.model_obj._login(
48+
credential = {"login": "AdMiN", "password": "admin", "type": "password"}
49+
auth_info = self.model_obj._login(
4950
self.env.registry.db_name,
50-
"AdMiN",
51-
"admin",
51+
credential,
5252
{"interactive": True},
5353
)
5454
self.assertEqual(
5555
rec_id.id,
56-
res_id,
56+
auth_info["uid"],
5757
"Login with with uppercase chars was not \
5858
successful",
5959
)

0 commit comments

Comments
 (0)