2021-03-10 13:55:06 +00:00
|
|
|
|
from flask import Blueprint, current_app, jsonify, request
|
2021-03-11 20:26:44 +00:00
|
|
|
|
from itsdangerous import BadData, SignatureExpired
|
|
|
|
|
|
from notifications_utils.url_safe_token import check_token, generate_token
|
2016-02-24 14:01:19 +00:00
|
|
|
|
|
2017-07-19 13:50:29 +01:00
|
|
|
|
from app.config import QueueNames
|
2016-02-25 11:22:36 +00:00
|
|
|
|
from app.dao.invited_user_dao import (
|
2021-03-11 20:26:44 +00:00
|
|
|
|
get_invited_user_by_id,
|
2021-03-12 12:50:07 +00:00
|
|
|
|
get_invited_user_by_service_and_id,
|
2021-03-10 13:55:06 +00:00
|
|
|
|
get_invited_users_for_service,
|
|
|
|
|
|
save_invited_user,
|
2016-02-25 11:22:36 +00:00
|
|
|
|
)
|
2016-06-16 17:34:33 +01:00
|
|
|
|
from app.dao.templates_dao import dao_get_template_by_id
|
2021-03-11 20:26:44 +00:00
|
|
|
|
from app.errors import InvalidRequest, register_errors
|
2022-10-04 15:28:27 +00:00
|
|
|
|
from app.models import EMAIL_TYPE, KEY_TYPE_NORMAL, Service
|
2021-03-10 13:55:06 +00:00
|
|
|
|
from app.notifications.process_notifications import (
|
|
|
|
|
|
persist_notification,
|
|
|
|
|
|
send_notification_to_queue,
|
|
|
|
|
|
)
|
2016-02-26 12:00:16 +00:00
|
|
|
|
from app.schemas import invited_user_schema
|
2016-02-24 14:01:19 +00:00
|
|
|
|
|
2021-03-11 20:26:44 +00:00
|
|
|
|
service_invite = Blueprint('service_invite', __name__)
|
2016-02-24 14:01:19 +00:00
|
|
|
|
|
2021-03-05 20:13:56 +00:00
|
|
|
|
register_errors(service_invite)
|
2016-02-24 14:01:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
2021-03-11 20:26:44 +00:00
|
|
|
|
@service_invite.route('/service/<service_id>/invite', methods=['POST'])
|
2016-02-25 11:22:36 +00:00
|
|
|
|
def create_invited_user(service_id):
|
2017-12-20 14:38:49 +00:00
|
|
|
|
request_json = request.get_json()
|
2022-05-06 15:25:14 +01:00
|
|
|
|
invited_user = invited_user_schema.load(request_json)
|
2016-02-24 14:01:19 +00:00
|
|
|
|
save_invited_user(invited_user)
|
2016-06-16 17:34:33 +01:00
|
|
|
|
|
2022-10-04 15:28:27 +00:00
|
|
|
|
template_id = current_app.config['INVITATION_EMAIL_TEMPLATE_ID']
|
2020-09-15 16:45:24 +01:00
|
|
|
|
|
|
|
|
|
|
template = dao_get_template_by_id(template_id)
|
2017-01-10 13:41:16 +00:00
|
|
|
|
service = Service.query.get(current_app.config['NOTIFY_SERVICE_ID'])
|
2016-12-20 15:59:31 +00:00
|
|
|
|
|
|
|
|
|
|
saved_notification = persist_notification(
|
|
|
|
|
|
template_id=template.id,
|
|
|
|
|
|
template_version=template.version,
|
|
|
|
|
|
recipient=invited_user.email_address,
|
2017-01-10 13:41:16 +00:00
|
|
|
|
service=service,
|
2016-12-20 15:59:31 +00:00
|
|
|
|
personalisation={
|
2016-06-16 17:34:33 +01:00
|
|
|
|
'user_name': invited_user.from_user.name,
|
|
|
|
|
|
'service_name': invited_user.service.name,
|
2017-12-20 14:38:49 +00:00
|
|
|
|
'url': invited_user_url(
|
|
|
|
|
|
invited_user.id,
|
|
|
|
|
|
request_json.get('invite_link_host'),
|
|
|
|
|
|
),
|
2016-12-20 15:59:31 +00:00
|
|
|
|
},
|
|
|
|
|
|
notification_type=EMAIL_TYPE,
|
|
|
|
|
|
api_key_id=None,
|
2017-11-27 12:30:50 +00:00
|
|
|
|
key_type=KEY_TYPE_NORMAL,
|
2017-12-18 11:39:21 +00:00
|
|
|
|
reply_to_text=invited_user.from_user.email_address
|
2016-12-20 15:59:31 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
2017-05-25 10:51:49 +01:00
|
|
|
|
send_notification_to_queue(saved_notification, False, queue=QueueNames.NOTIFY)
|
2016-06-16 17:34:33 +01:00
|
|
|
|
|
2022-05-06 15:52:44 +01:00
|
|
|
|
return jsonify(data=invited_user_schema.dump(invited_user)), 201
|
2016-02-25 11:22:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
2021-03-11 20:26:44 +00:00
|
|
|
|
@service_invite.route('/service/<service_id>/invite', methods=['GET'])
|
2016-02-25 11:22:36 +00:00
|
|
|
|
def get_invited_users_by_service(service_id):
|
|
|
|
|
|
invited_users = get_invited_users_for_service(service_id)
|
2022-05-06 15:52:44 +01:00
|
|
|
|
return jsonify(data=invited_user_schema.dump(invited_users, many=True)), 200
|
2016-02-25 11:22:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
2021-03-11 20:26:44 +00:00
|
|
|
|
@service_invite.route('/service/<service_id>/invite/<invited_user_id>', methods=['GET'])
|
2020-08-17 11:59:55 +01:00
|
|
|
|
def get_invited_user_by_service(service_id, invited_user_id):
|
2021-03-12 12:50:07 +00:00
|
|
|
|
invited_user = get_invited_user_by_service_and_id(service_id, invited_user_id)
|
2022-05-06 15:52:44 +01:00
|
|
|
|
return jsonify(data=invited_user_schema.dump(invited_user)), 200
|
2020-08-17 11:59:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
2021-03-11 20:26:44 +00:00
|
|
|
|
@service_invite.route('/service/<service_id>/invite/<invited_user_id>', methods=['POST'])
|
2016-03-01 13:33:20 +00:00
|
|
|
|
def update_invited_user(service_id, invited_user_id):
|
2021-03-12 12:50:07 +00:00
|
|
|
|
fetched = get_invited_user_by_service_and_id(service_id=service_id, invited_user_id=invited_user_id)
|
2016-03-01 13:33:20 +00:00
|
|
|
|
|
2022-05-06 15:52:44 +01:00
|
|
|
|
current_data = dict(invited_user_schema.dump(fetched).items())
|
2016-03-01 13:33:20 +00:00
|
|
|
|
current_data.update(request.get_json())
|
2022-05-06 15:25:14 +01:00
|
|
|
|
update_dict = invited_user_schema.load(current_data)
|
2016-03-01 13:33:20 +00:00
|
|
|
|
save_invited_user(update_dict)
|
2022-05-06 15:52:44 +01:00
|
|
|
|
return jsonify(data=invited_user_schema.dump(fetched)), 200
|
2016-03-01 13:33:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
2017-12-20 14:38:49 +00:00
|
|
|
|
def invited_user_url(invited_user_id, invite_link_host=None):
|
2016-06-16 17:34:33 +01:00
|
|
|
|
token = generate_token(str(invited_user_id), current_app.config['SECRET_KEY'], current_app.config['DANGEROUS_SALT'])
|
|
|
|
|
|
|
2017-12-20 14:38:49 +00:00
|
|
|
|
if invite_link_host is None:
|
|
|
|
|
|
invite_link_host = current_app.config['ADMIN_BASE_URL']
|
|
|
|
|
|
|
|
|
|
|
|
return '{0}/invitation/{1}'.format(invite_link_host, token)
|
2021-03-11 20:26:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@service_invite.route('/invite/service/<uuid:invited_user_id>', methods=['GET'])
|
|
|
|
|
|
def get_invited_user(invited_user_id):
|
|
|
|
|
|
invited_user = get_invited_user_by_id(invited_user_id)
|
2022-05-06 15:52:44 +01:00
|
|
|
|
return jsonify(data=invited_user_schema.dump(invited_user)), 200
|
2021-03-11 20:26:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@service_invite.route('/invite/service/<token>', methods=['GET'])
|
2021-03-11 20:47:24 +00:00
|
|
|
|
@service_invite.route('/invite/service/check/<token>', methods=['GET'])
|
2021-03-11 20:26:44 +00:00
|
|
|
|
def validate_service_invitation_token(token):
|
|
|
|
|
|
|
|
|
|
|
|
max_age_seconds = 60 * 60 * 24 * current_app.config['INVITATION_EXPIRATION_DAYS']
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
invited_user_id = check_token(token,
|
|
|
|
|
|
current_app.config['SECRET_KEY'],
|
|
|
|
|
|
current_app.config['DANGEROUS_SALT'],
|
|
|
|
|
|
max_age_seconds)
|
|
|
|
|
|
except SignatureExpired:
|
|
|
|
|
|
errors = {'invitation':
|
|
|
|
|
|
'Your invitation to GOV.UK Notify has expired. '
|
|
|
|
|
|
'Please ask the person that invited you to send you another one'}
|
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
|
|
|
|
|
except BadData:
|
|
|
|
|
|
errors = {'invitation': 'Something’s wrong with this link. Make sure you’ve copied the whole thing.'}
|
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
|
|
|
|
|
|
|
|
|
|
|
invited_user = get_invited_user_by_id(invited_user_id)
|
2022-05-06 15:52:44 +01:00
|
|
|
|
return jsonify(data=invited_user_schema.dump(invited_user)), 200
|