I am trying to alter the authenticate method in res.users model to follow user behavior if he/she enters wrong password for several times to lock their account.
from odoo import api, fields, models, _
from odoo.exceptions import AccessDenied
class ResUsers(models.Model):
_inherit = 'res.users'
failed_login_attempts = fields.Integer(default=0)
is_locked = fields.Boolean(default=False)
@api.model
def _check_credentials(self, password, env):
if self.is_locked:
raise AccessDenied(_("Account is locked. Contact your Administrator to unlock your account."))
return super(ResUsers, self)._check_credentials(password, env)
@api.model
def authenticate(self, db, login, password, user_agent_env): # Add 'user_agent_env' argument
user = self.search([('login', '=', login)], limit=1)
if user.is_locked:
raise AccessDenied(_("Your account is locked due to too many failed login attempts."))
user_id = super(ResUsers, self).authenticate(db, login, password, user_agent_env)
if user_id:
user.sudo().write({'failed_login_attempts': 0}) # Reset on successful login
else:
user.sudo()._increase_failed_attempts()
return user_id
def _increase_failed_attempts(self):
limit = int(self.env['ir.config_parameter'].sudo().get_param('auth_failure.login_lock_limit', 5))
if self.failed_login_attempts + 1 >= limit:
self.sudo().write({'is_locked': True, 'failed_login_attempts': self.failed_login_attempts + 1})
else:
self.sudo().write({'failed_login_attempts': self.failed_login_attempts + 1})
when trying to login I face internal server error with a conclusion:
uid = odoo.registry(db)['res.users'].authenticate(db, login, password, env) TypeError: ResUsers.authenticate() missing 1 required positional argument: 'user_agent_env'
is there any way to solve the error? or is there any other way to achieve this task?
I am using odoo 15.