feat: added a policy to prevent sending ace messages to disabled users (#36584)
This commit is contained in:
committed by
GitHub
parent
725234815f
commit
47a920d5b9
29
openedx/core/djangoapps/ace_common/policies.py
Normal file
29
openedx/core/djangoapps/ace_common/policies.py
Normal file
@@ -0,0 +1,29 @@
|
||||
"""Disable User Email OptOut Policy"""
|
||||
|
||||
import logging
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from edx_ace.channel import ChannelType
|
||||
from edx_ace.policy import Policy, PolicyResult
|
||||
|
||||
|
||||
User = get_user_model()
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class DisableUserOptout(Policy):
|
||||
"""
|
||||
Skips sending ace messages to disabled users
|
||||
"""
|
||||
def check(self, message):
|
||||
"""
|
||||
Checks if the user is disabled and if so, skips sending the message
|
||||
"""
|
||||
skip_disable_user_policy = message.options.get('skip_disable_user_policy', False)
|
||||
if skip_disable_user_policy:
|
||||
return PolicyResult(deny=set())
|
||||
user = User.objects.get(id=message.recipient.lms_user_id)
|
||||
if user.has_usable_password():
|
||||
return PolicyResult(deny=set())
|
||||
log.info(f"===> User is disabled - {user.email} - {message.name}")
|
||||
return PolicyResult(deny=set(ChannelType))
|
||||
@@ -16,3 +16,4 @@ class EmailNotificationMessageType(MessageType):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.options['transactional'] = True
|
||||
self.options['from_address'] = settings.NOTIFICATIONS_DEFAULT_FROM_EMAIL
|
||||
self.options['skip_disable_user_policy'] = True
|
||||
|
||||
@@ -274,6 +274,7 @@ def _schedule_send(msg_str, site_id, delivery_config_var, log_prefix): # lint-a
|
||||
site = Site.objects.select_related('configuration').get(pk=site_id)
|
||||
if _is_delivery_enabled(site, delivery_config_var, log_prefix):
|
||||
msg = Message.from_string(msg_str)
|
||||
msg.options['skip_disable_user_policy'] = True
|
||||
|
||||
user = User.objects.get(id=msg.recipient.lms_user_id)
|
||||
if not user.has_usable_password():
|
||||
|
||||
@@ -14,6 +14,7 @@ class PasswordReset(BaseMessageType):
|
||||
|
||||
# pylint: disable=unsupported-assignment-operation
|
||||
self.options['transactional'] = True
|
||||
self.options['skip_disable_user_policy'] = True
|
||||
|
||||
|
||||
class PasswordResetSuccess(BaseMessageType):
|
||||
|
||||
Reference in New Issue
Block a user