Files
odoo_source/addons/auth_totp_mail/models/auth_totp_device.py
T
Aurélien Warnon 9e7ff38fa8 [IMP] mail, auth_totp: notify user about important security modifications
This commit aims to notify the user (currently through an email) that some
important security parameter of his account has changed.

Here is a list of what is currently notified:
- password change
- login change
- email change
- 2FA enabled/disabled
- trusted device (for 2FA) added/removed

This email is rather simple and only invites the end-user to take actions if
that change was not done by him (reset password, contact administrator, ...).

Task-2639168

closes odoo/odoo#94922

Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com>
2023-01-24 17:34:48 +01:00

48 lines
1.6 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import _, models
from collections import defaultdict
class AuthTotpDevice(models.Model):
_inherit = "auth_totp.device"
def unlink(self):
""" Notify users when trusted devices are removed from their account. """
removed_devices_by_user = self._classify_by_user()
for user, removed_devices in removed_devices_by_user.items():
user._notify_security_setting_update(
_("Security Update: Device Removed"),
_(
"A trusted device has just been removed from your account: %(device_names)s",
device_names=', '.join([device.name for device in removed_devices])
),
)
return super().unlink()
def _generate(self, scope, name):
""" Notify users when trusted devices are added onto their account.
We override this method instead of 'create' as those records are inserted directly into the
database using raw SQL. """
res = super()._generate(scope, name)
self.env.user._notify_security_setting_update(
_("Security Update: Device Added"),
_(
"A trusted device has just been added to your account: %(device_name)s",
device_name=name
),
)
return res
def _classify_by_user(self):
devices_by_user = defaultdict(lambda: self.env['auth_totp.device'])
for device in self:
devices_by_user[device.user_id] |= device
return devices_by_user