Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support GC Notify Mailer #6

Merged
merged 4 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/6.canada.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added support for the GC Notify mailer overrides.
84 changes: 47 additions & 37 deletions ckanext/security/mailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from ckan.common import config, is_flask_request
from ckan.lib.base import render_jinja2, render
from ckan.lib.mailer import get_reset_link_body, mail_user
import ckan.lib.mailer as mailer # (canada fork only): GC Notify support
from ckan.plugins import toolkit as tk
from ckan import model

Expand All @@ -25,6 +26,27 @@ def create_reset_key(user):
model.repo.commit_and_remove()


def _build_template(file_path, replacements={}):
# (canada fork only): fixes app context
# TODO: upstream contrib??
template = ''
with open(file_path, 'r') as f:
template = f.read()
for replacement, value in replacements.items():
template = template.replace("{{ %s }}" % replacement, str(value))\
.replace("{{%s}}" % replacement, str(value))
return template


def _get_template(template_name):
# (canada fork only): fixes app context
# TODO: upstream contrib??
# FIXME: this prevents users from being able to extend/override
# the email txt files in the templates directories.
return os.path.join(os.path.dirname(os.path.abspath(__file__)),
'templates/security/emails/%s' % template_name)


def send_reset_link(user):
create_reset_key(user)
body = get_reset_link_body(user)
Expand All @@ -45,50 +67,38 @@ def send_reset_link(user):


def _build_footer_content(extra_vars):
# (canada fork only): fixes app context
# TODO: upstream contrib??
custom_path = config.get('ckanext.security.brute_force_footer_path')
if (custom_path and os.path.exists(custom_path)):
log.warning('Overriding brute force lockout email footer with %s',
custom_path)
with open(custom_path, 'r') as footer_file:
footer_content = footer_file.read()
if is_flask_request():
env = flask.current_app.jinja_env
else:
env = config['pylons.app_globals'].jinja_env
template = env.from_string(footer_content)
return '\n\n' + template.render(**extra_vars)
return '\n\n' + _build_template(custom_path, extra_vars)
else:
footer_path = 'security/emails/lockout_footer.txt'
if is_flask_request():
return '\n\n' + render(footer_path, extra_vars)
else:
return '\n\n' + render_jinja2(footer_path, extra_vars)
return '\n\n' + _build_template(_get_template('lockout_footer.txt'), extra_vars)


def notify_lockout(user, lockout_timeout):
extra_vars = {
'site_title': config.get('ckan.site_title'),
'site_url': config.get('ckan.site_url'),
'user_name': user.name,
'password_reset_url':
config.get('ckan.site_url').rstrip('/') + '/user/login',
'lockout_mins': lockout_timeout // 60,
}

if is_flask_request():
subject = render(
'security/emails/lockout_subject.txt', extra_vars)
else:
subject = render_jinja2(
'security/emails/lockout_subject.txt', extra_vars)

subject = subject.split('\n')[0] # Make sure we only use the first line

if is_flask_request():
body = render('security/emails/lockout_mail.txt', extra_vars)\
+ _build_footer_content(extra_vars)
else:
body = render_jinja2('security/emails/lockout_mail.txt', extra_vars)\
# (canada fork only): GC Notify support
try:
# see: ckanext.gcnotify.mailer.notify_lockout
mailer.notify_lockout(user, lockout_timeout)
except (mailer.MailerException, AttributeError, TypeError):
extra_vars = {
'site_title': config.get('ckan.site_title'),
'site_url': config.get('ckan.site_url'),
'user_name': user.name,
'password_reset_url':
config.get('ckan.site_url').rstrip('/') + '/user/login',
'lockout_mins': int(lockout_timeout / 60),
}

# (canada fork only): fixes app context
# TODO: upstream contrib??
subject = _build_template(_get_template('lockout_subject.txt'), extra_vars)
subject = subject.split('\n')[0] # Make sure we only use the first line

body = _build_template(_get_template('lockout_mail.txt'), extra_vars)\
+ _build_footer_content(extra_vars)

mail_user(user, subject, body)
mail_user(user, subject, body)
Loading