Files
notifications-api/app/service/utils.py
Chris Hill-Scott 01cf175cb2 Don’t respect the whitelist for one off sending
The whitelist was built to help developers and designers making
prototypes to do realistic usability testing of them, without having to
go through the whole go live process.

These users are sending messages using the API. The whitelist wasn’t
made available to users uploading spreadsheets. The users sending one
off messages are similar to those uploading spreadsheets, not those
using the API. Therefore they shouldn’t be able to use the whitelist to
expand the range of recipients they can send to.

Passing the argument through three methods doesn’t feel that great, but
can’t think of a better way without major refactoring…
2018-01-22 11:42:37 +00:00

55 lines
1.6 KiB
Python

import itertools
from notifications_utils.recipients import allowed_to_send_to
from app.models import (
ServiceWhitelist,
MOBILE_TYPE, EMAIL_TYPE,
KEY_TYPE_TEST, KEY_TYPE_TEAM, KEY_TYPE_NORMAL)
def get_recipients_from_request(request_json, key, type):
return [(type, recipient) for recipient in request_json.get(key)]
def get_whitelist_objects(service_id, request_json):
return [
ServiceWhitelist.from_string(service_id, type, recipient)
for type, recipient in (
get_recipients_from_request(request_json,
'phone_numbers',
MOBILE_TYPE) +
get_recipients_from_request(request_json,
'email_addresses',
EMAIL_TYPE)
)
]
def service_allowed_to_send_to(recipient, service, key_type, allow_whitelisted_recipients=True):
if key_type == KEY_TYPE_TEST:
return True
if key_type == KEY_TYPE_NORMAL and not service.restricted:
return True
team_members = itertools.chain.from_iterable(
[user.mobile_number, user.email_address] for user in service.users
)
whitelist_members = [
member.recipient for member in service.whitelist
if allow_whitelisted_recipients
]
if (
(key_type == KEY_TYPE_NORMAL and service.restricted) or
(key_type == KEY_TYPE_TEAM)
):
return allowed_to_send_to(
recipient,
itertools.chain(
team_members,
whitelist_members
)
)