2023-08-25 08:57:24 -07:00
|
|
|
from flask import abort, flash, redirect, render_template, request, session, url_for
|
2019-07-01 15:22:08 +01:00
|
|
|
from flask_login import current_user
|
2016-04-15 11:04:35 +01:00
|
|
|
from notifications_python_client.errors import HTTPError
|
2018-02-20 11:22:17 +00:00
|
|
|
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
from app import current_service, service_api_client
|
2019-04-03 11:47:46 +01:00
|
|
|
from app.event_handlers import (
|
2023-04-25 19:36:18 -04:00
|
|
|
create_cancel_user_invite_to_service_event,
|
2019-04-03 11:47:46 +01:00
|
|
|
create_email_change_event,
|
2023-04-25 19:36:18 -04:00
|
|
|
create_invite_user_to_service_event,
|
2019-04-03 11:47:46 +01:00
|
|
|
create_mobile_number_change_event,
|
2020-03-10 13:18:32 +00:00
|
|
|
create_remove_user_from_service_event,
|
2019-04-03 11:47:46 +01:00
|
|
|
)
|
2021-01-06 12:12:01 +00:00
|
|
|
from app.formatters import redact_mobile_number
|
2018-02-20 11:22:17 +00:00
|
|
|
from app.main import main
|
2019-02-20 11:50:34 +00:00
|
|
|
from app.main.forms import (
|
|
|
|
|
ChangeEmailForm,
|
2019-02-22 16:01:04 +00:00
|
|
|
ChangeMobileNumberForm,
|
2019-04-18 16:03:13 +01:00
|
|
|
ChangeNonGovEmailForm,
|
2019-02-20 11:50:34 +00:00
|
|
|
InviteUserForm,
|
|
|
|
|
PermissionsForm,
|
|
|
|
|
SearchUsersForm,
|
|
|
|
|
)
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
from app.models.user import InvitedUser, User
|
2023-12-11 10:47:21 -05:00
|
|
|
from app.utils.user import is_gov_user, user_has_permissions
|
2022-10-04 03:04:13 +00:00
|
|
|
from app.utils.user_permissions import permission_options
|
2016-02-19 15:02:13 +00:00
|
|
|
|
|
|
|
|
|
2019-11-04 11:07:55 +00:00
|
|
|
@main.route("/services/<uuid:service_id>/users")
|
2019-06-19 16:39:13 +01:00
|
|
|
@user_has_permissions(allow_org_user=True)
|
2016-02-19 15:02:13 +00:00
|
|
|
def manage_users(service_id):
|
2016-03-22 13:18:06 +00:00
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
"views/manage-users.html",
|
2018-12-03 10:59:36 +00:00
|
|
|
users=current_service.team_members,
|
2016-03-22 13:18:06 +00:00
|
|
|
current_user=current_user,
|
2018-12-03 10:59:36 +00:00
|
|
|
show_search_box=(len(current_service.team_members) > 7),
|
2018-01-26 17:21:06 +00:00
|
|
|
form=SearchUsersForm(),
|
2022-10-04 03:04:13 +00:00
|
|
|
permissions=permission_options,
|
2016-03-22 13:18:06 +00:00
|
|
|
)
|
2016-02-19 15:02:13 +00:00
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route("/services/<uuid:service_id>/users/invite", methods=["GET", "POST"])
|
|
|
|
|
@main.route(
|
|
|
|
|
"/services/<uuid:service_id>/users/invite/<uuid:user_id>", methods=["GET", "POST"]
|
|
|
|
|
)
|
2023-12-11 10:47:21 -05:00
|
|
|
@user_has_permissions("manage_service")
|
2020-06-08 10:58:15 +01:00
|
|
|
def invite_user(service_id, user_id=None):
|
2022-10-04 03:04:13 +00:00
|
|
|
form_class = InviteUserForm
|
2020-07-13 11:10:34 +01:00
|
|
|
form = form_class(
|
2021-07-15 14:21:58 +01:00
|
|
|
inviter_email_address=current_user.email_address,
|
2019-03-15 14:57:39 +00:00
|
|
|
all_template_folders=current_service.all_template_folders,
|
2023-08-25 09:12:23 -07:00
|
|
|
folder_permissions=[f["id"] for f in current_service.all_template_folders],
|
2019-03-15 14:57:39 +00:00
|
|
|
)
|
2017-11-01 15:36:27 +00:00
|
|
|
|
2020-06-08 10:58:15 +01:00
|
|
|
if user_id:
|
|
|
|
|
user_to_invite = User.from_id(user_id)
|
2021-01-08 10:56:59 +00:00
|
|
|
if user_to_invite.belongs_to_service(current_service.id):
|
|
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
"views/user-already-team-member.html",
|
2021-01-08 10:56:59 +00:00
|
|
|
user_to_invite=user_to_invite,
|
|
|
|
|
)
|
|
|
|
|
if current_service.invite_pending_for(user_to_invite.email_address):
|
2020-06-08 11:19:58 +01:00
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
"views/user-already-invited.html",
|
2020-06-08 11:19:58 +01:00
|
|
|
user_to_invite=user_to_invite,
|
|
|
|
|
)
|
2023-07-12 12:09:44 -04:00
|
|
|
if not user_to_invite.default_organization:
|
2020-06-08 14:39:36 +01:00
|
|
|
abort(403)
|
2023-07-12 12:09:44 -04:00
|
|
|
if user_to_invite.default_organization.id != current_service.organization_id:
|
2020-06-08 14:39:36 +01:00
|
|
|
abort(403)
|
2020-06-08 10:58:15 +01:00
|
|
|
form.email_address.data = user_to_invite.email_address
|
|
|
|
|
else:
|
|
|
|
|
user_to_invite = None
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
service_has_email_auth = current_service.has_permission("email_auth")
|
2017-11-01 15:36:27 +00:00
|
|
|
if not service_has_email_auth:
|
2023-08-25 09:12:23 -07:00
|
|
|
form.login_authentication.data = "sms_auth"
|
2016-03-03 13:00:12 +00:00
|
|
|
|
2016-02-19 15:02:13 +00:00
|
|
|
if form.validate_on_submit():
|
2016-02-26 13:07:35 +00:00
|
|
|
email_address = form.email_address.data
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
invited_user = InvitedUser.create(
|
2016-03-22 13:18:06 +00:00
|
|
|
current_user.id,
|
|
|
|
|
service_id,
|
|
|
|
|
email_address,
|
2020-05-14 16:59:34 +01:00
|
|
|
form.permissions,
|
2019-03-15 14:57:39 +00:00
|
|
|
form.login_authentication.data,
|
2019-04-02 15:23:46 +01:00
|
|
|
form.folder_permissions.data,
|
2016-03-22 13:18:06 +00:00
|
|
|
)
|
2023-04-25 19:36:18 -04:00
|
|
|
create_invite_user_to_service_event(
|
|
|
|
|
email_address=email_address,
|
|
|
|
|
invited_by_id=current_user.id,
|
|
|
|
|
service_id=service_id,
|
|
|
|
|
ui_permissions=form.permissions,
|
|
|
|
|
)
|
2016-03-18 10:49:22 +00:00
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
flash(
|
|
|
|
|
"Invite sent to {}".format(invited_user.email_address), "default_with_tick"
|
|
|
|
|
)
|
|
|
|
|
return redirect(url_for(".manage_users", service_id=service_id))
|
2016-02-19 15:02:13 +00:00
|
|
|
|
|
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
"views/invite-user.html",
|
2017-11-01 15:36:27 +00:00
|
|
|
form=form,
|
2019-03-12 11:41:52 +00:00
|
|
|
service_has_email_auth=service_has_email_auth,
|
|
|
|
|
mobile_number=True,
|
2020-06-08 10:58:15 +01:00
|
|
|
user_to_invite=user_to_invite,
|
2016-02-19 15:02:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route("/services/<uuid:service_id>/users/<uuid:user_id>", methods=["GET", "POST"])
|
|
|
|
|
@user_has_permissions("manage_service")
|
2016-03-01 16:12:26 +00:00
|
|
|
def edit_user_permissions(service_id, user_id):
|
2023-08-25 09:12:23 -07:00
|
|
|
service_has_email_auth = current_service.has_permission("email_auth")
|
2019-02-25 16:51:37 +00:00
|
|
|
user = current_service.get_team_member(user_id)
|
2019-02-21 13:03:06 +00:00
|
|
|
|
|
|
|
|
mobile_number = None
|
|
|
|
|
if user.mobile_number:
|
2019-02-25 14:27:37 +00:00
|
|
|
mobile_number = redact_mobile_number(user.mobile_number, " ")
|
2017-11-01 15:36:27 +00:00
|
|
|
|
2022-10-04 03:04:13 +00:00
|
|
|
form_class = PermissionsForm
|
2020-07-13 11:10:34 +01:00
|
|
|
|
|
|
|
|
form = form_class.from_user(
|
2019-02-25 16:23:28 +00:00
|
|
|
user,
|
|
|
|
|
service_id,
|
2023-08-25 09:12:23 -07:00
|
|
|
folder_permissions=None
|
|
|
|
|
if user.platform_admin
|
|
|
|
|
else [
|
|
|
|
|
f["id"]
|
|
|
|
|
for f in current_service.all_template_folders
|
2019-03-11 14:13:56 +00:00
|
|
|
if user.has_template_folder_permission(f)
|
2019-02-27 15:45:18 +00:00
|
|
|
],
|
2023-08-25 09:12:23 -07:00
|
|
|
all_template_folders=None
|
|
|
|
|
if user.platform_admin
|
|
|
|
|
else current_service.all_template_folders,
|
2019-02-25 16:23:28 +00:00
|
|
|
)
|
2018-06-12 14:29:47 +01:00
|
|
|
|
2016-03-03 13:00:12 +00:00
|
|
|
if form.validate_on_submit():
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
user.set_permissions(
|
|
|
|
|
service_id,
|
2020-05-14 16:59:34 +01:00
|
|
|
permissions=form.permissions,
|
2019-04-02 15:23:46 +01:00
|
|
|
folder_permissions=form.folder_permissions.data,
|
2021-07-15 11:57:33 +01:00
|
|
|
set_by_id=current_user.id,
|
2016-03-22 13:18:06 +00:00
|
|
|
)
|
2023-08-14 16:59:38 -04:00
|
|
|
# Only change the auth type if this is supported for a service.
|
|
|
|
|
if service_has_email_auth:
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
user.update(auth_type=form.login_authentication.data)
|
2023-08-25 09:12:23 -07:00
|
|
|
return redirect(url_for(".manage_users", service_id=service_id))
|
2016-02-19 15:02:13 +00:00
|
|
|
|
|
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
"views/edit-user-permissions.html",
|
2016-03-03 13:00:12 +00:00
|
|
|
user=user,
|
2017-11-01 15:36:27 +00:00
|
|
|
form=form,
|
|
|
|
|
service_has_email_auth=service_has_email_auth,
|
2019-03-14 17:31:51 +00:00
|
|
|
mobile_number=mobile_number,
|
2023-08-25 09:12:23 -07:00
|
|
|
delete=request.args.get("delete"),
|
2016-02-19 15:02:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route("/services/<uuid:service_id>/users/<uuid:user_id>/delete", methods=["POST"])
|
|
|
|
|
@user_has_permissions("manage_service")
|
2019-03-26 15:51:44 +00:00
|
|
|
def remove_user_from_service(service_id, user_id):
|
2019-03-14 17:31:51 +00:00
|
|
|
try:
|
|
|
|
|
service_api_client.remove_user_from_service(service_id, user_id)
|
|
|
|
|
except HTTPError as e:
|
|
|
|
|
msg = "You cannot remove the only user for a service"
|
|
|
|
|
if e.status_code == 400 and msg in e.message:
|
2023-08-25 09:12:23 -07:00
|
|
|
flash(msg, "info")
|
|
|
|
|
return redirect(url_for(".manage_users", service_id=service_id))
|
2019-03-14 17:31:51 +00:00
|
|
|
else:
|
|
|
|
|
abort(500, e)
|
2020-03-10 13:18:32 +00:00
|
|
|
else:
|
2021-07-12 15:29:53 +01:00
|
|
|
create_remove_user_from_service_event(
|
2023-08-25 09:12:23 -07:00
|
|
|
user_id=user_id, removed_by_id=current_user.id, service_id=service_id
|
2021-07-12 15:29:53 +01:00
|
|
|
)
|
2019-03-14 17:31:51 +00:00
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
return redirect(url_for(".manage_users", service_id=service_id))
|
2016-03-23 10:46:31 +00:00
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route(
|
|
|
|
|
"/services/<uuid:service_id>/users/<uuid:user_id>/edit-email",
|
|
|
|
|
methods=["GET", "POST"],
|
|
|
|
|
)
|
|
|
|
|
@user_has_permissions("manage_service")
|
2019-02-19 15:35:51 +00:00
|
|
|
def edit_user_email(service_id, user_id):
|
2019-02-25 16:51:37 +00:00
|
|
|
user = current_service.get_team_member(user_id)
|
2019-02-19 15:35:51 +00:00
|
|
|
user_email = user.email_address
|
2023-08-25 09:12:23 -07:00
|
|
|
session_key = "team_member_email_change-{}".format(user_id)
|
2019-02-19 18:01:01 +00:00
|
|
|
|
2019-04-18 16:03:13 +01:00
|
|
|
if is_gov_user(user_email):
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
form = ChangeEmailForm(User.already_registered, email_address=user_email)
|
2019-04-18 16:03:13 +01:00
|
|
|
else:
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
form = ChangeNonGovEmailForm(User.already_registered, email_address=user_email)
|
2019-02-25 12:04:07 +00:00
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
if request.form.get("email_address", "").strip() == user_email:
|
|
|
|
|
return redirect(url_for(".manage_users", service_id=current_service.id))
|
2019-02-25 12:04:07 +00:00
|
|
|
|
2019-02-19 18:01:01 +00:00
|
|
|
if form.validate_on_submit():
|
2020-01-13 12:03:39 +00:00
|
|
|
session[session_key] = form.email_address.data
|
2019-02-19 18:01:01 +00:00
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
return redirect(
|
|
|
|
|
url_for(".confirm_edit_user_email", user_id=user.id, service_id=service_id)
|
|
|
|
|
)
|
2019-02-19 15:35:51 +00:00
|
|
|
|
|
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
"views/manage-users/edit-user-email.html",
|
2019-02-19 15:35:51 +00:00
|
|
|
user=user,
|
|
|
|
|
form=form,
|
2023-08-25 09:12:23 -07:00
|
|
|
service_id=service_id,
|
2019-02-19 15:35:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route(
|
|
|
|
|
"/services/<uuid:service_id>/users/<uuid:user_id>/edit-email/confirm",
|
|
|
|
|
methods=["GET", "POST"],
|
|
|
|
|
)
|
|
|
|
|
@user_has_permissions("manage_service")
|
2019-02-19 18:01:01 +00:00
|
|
|
def confirm_edit_user_email(service_id, user_id):
|
2019-02-25 16:51:37 +00:00
|
|
|
user = current_service.get_team_member(user_id)
|
2023-08-25 09:12:23 -07:00
|
|
|
session_key = "team_member_email_change-{}".format(user_id)
|
2020-01-13 12:03:39 +00:00
|
|
|
if session_key in session:
|
|
|
|
|
new_email = session[session_key]
|
2019-02-22 16:13:46 +00:00
|
|
|
else:
|
2023-08-25 09:12:23 -07:00
|
|
|
return redirect(
|
|
|
|
|
url_for(".edit_user_email", service_id=service_id, user_id=user_id)
|
|
|
|
|
)
|
|
|
|
|
if request.method == "POST":
|
2019-02-19 18:01:01 +00:00
|
|
|
try:
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
user.update(email_address=new_email, updated_by=current_user.id)
|
2019-02-19 18:01:01 +00:00
|
|
|
except HTTPError as e:
|
2019-02-25 14:27:37 +00:00
|
|
|
abort(500, e)
|
2019-04-03 11:47:46 +01:00
|
|
|
else:
|
2021-07-12 15:29:53 +01:00
|
|
|
create_email_change_event(
|
|
|
|
|
user_id=user.id,
|
|
|
|
|
updated_by_id=current_user.id,
|
|
|
|
|
original_email_address=user.email_address,
|
2023-08-25 09:12:23 -07:00
|
|
|
new_email_address=new_email,
|
2021-07-12 15:29:53 +01:00
|
|
|
)
|
2019-02-22 16:20:54 +00:00
|
|
|
finally:
|
2020-01-13 12:03:39 +00:00
|
|
|
session.pop(session_key, None)
|
2019-02-19 18:01:01 +00:00
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
return redirect(url_for(".manage_users", service_id=service_id))
|
2019-02-19 18:01:01 +00:00
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
"views/manage-users/confirm-edit-user-email.html",
|
2019-02-19 18:01:01 +00:00
|
|
|
user=user,
|
2019-02-20 11:50:34 +00:00
|
|
|
service_id=service_id,
|
2023-08-25 09:12:23 -07:00
|
|
|
new_email=new_email,
|
2019-02-19 18:01:01 +00:00
|
|
|
)
|
|
|
|
|
|
2019-02-20 11:50:34 +00:00
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route(
|
|
|
|
|
"/services/<uuid:service_id>/users/<uuid:user_id>/edit-mobile-number",
|
|
|
|
|
methods=["GET", "POST"],
|
|
|
|
|
)
|
|
|
|
|
@user_has_permissions("manage_service")
|
2019-02-22 11:31:35 +00:00
|
|
|
def edit_user_mobile_number(service_id, user_id):
|
2019-02-26 11:47:15 +00:00
|
|
|
user = current_service.get_team_member(user_id)
|
2019-02-22 11:31:35 +00:00
|
|
|
user_mobile_number = redact_mobile_number(user.mobile_number)
|
|
|
|
|
|
|
|
|
|
form = ChangeMobileNumberForm(mobile_number=user_mobile_number)
|
2023-08-25 09:12:23 -07:00
|
|
|
if form.mobile_number.data == user_mobile_number and request.method == "POST":
|
|
|
|
|
return redirect(url_for(".manage_users", service_id=service_id))
|
2019-02-22 12:28:18 +00:00
|
|
|
if form.validate_on_submit():
|
2023-08-25 09:12:23 -07:00
|
|
|
session["team_member_mobile_change"] = form.mobile_number.data
|
2019-02-22 12:28:18 +00:00
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
return redirect(
|
|
|
|
|
url_for(
|
|
|
|
|
".confirm_edit_user_mobile_number",
|
|
|
|
|
user_id=user.id,
|
|
|
|
|
service_id=service_id,
|
|
|
|
|
)
|
|
|
|
|
)
|
2019-02-22 11:31:35 +00:00
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
"views/manage-users/edit-user-mobile.html",
|
2019-02-22 11:31:35 +00:00
|
|
|
user=user,
|
|
|
|
|
form=form,
|
2023-08-25 09:12:23 -07:00
|
|
|
service_id=service_id,
|
2019-02-22 11:31:35 +00:00
|
|
|
)
|
2019-02-21 13:03:06 +00:00
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route(
|
|
|
|
|
"/services/<uuid:service_id>/users/<uuid:user_id>/edit-mobile-number/confirm",
|
|
|
|
|
methods=["GET", "POST"],
|
|
|
|
|
)
|
|
|
|
|
@user_has_permissions("manage_service")
|
2019-02-22 12:28:18 +00:00
|
|
|
def confirm_edit_user_mobile_number(service_id, user_id):
|
2019-02-26 11:47:15 +00:00
|
|
|
user = current_service.get_team_member(user_id)
|
2023-08-25 09:12:23 -07:00
|
|
|
if "team_member_mobile_change" in session:
|
|
|
|
|
new_number = session["team_member_mobile_change"]
|
2019-02-25 14:27:37 +00:00
|
|
|
else:
|
2023-08-25 09:12:23 -07:00
|
|
|
return redirect(
|
|
|
|
|
url_for(".edit_user_mobile_number", service_id=service_id, user_id=user_id)
|
|
|
|
|
)
|
|
|
|
|
if request.method == "POST":
|
2019-02-22 16:01:04 +00:00
|
|
|
try:
|
Make user API client return JSON, not a model
The data flow of other bits of our application looks like this:
```
API (returns JSON)
⬇
API client (returns a built in type, usually `dict`)
⬇
Model (returns an instance, eg of type `Service`)
⬇
View (returns HTML)
```
The user API client was architected weirdly, in that it returned a model
directly, like this:
```
API (returns JSON)
⬇
API client (returns a model, of type `User`, `InvitedUser`, etc)
⬇
View (returns HTML)
```
This mixing of different layers of the application is bad because it
makes it hard to write model code that doesn’t have circular
dependencies. As our application gets more complicated we will be
relying more on models to manage this complexity, so we should make it
easy, not hard to write them.
It also means that most of our mocking was of the User model, not just
the underlying JSON. So it would have been easy to introduce subtle bugs
to the user model, because it wasn’t being comprehensively tested. A lot
of the changed lines of code in this commit mean changing the tests to
mock only the JSON, which means that the model layer gets implicitly
tested.
For those reasons this commit changes the user API client to return
JSON, not an instance of `User` or other models.
2019-05-23 15:27:35 +01:00
|
|
|
user.update(mobile_number=new_number, updated_by=current_user.id)
|
2019-02-22 16:01:04 +00:00
|
|
|
except HTTPError as e:
|
2019-02-25 14:27:37 +00:00
|
|
|
abort(500, e)
|
2019-04-03 11:47:46 +01:00
|
|
|
else:
|
2021-07-12 15:29:53 +01:00
|
|
|
create_mobile_number_change_event(
|
|
|
|
|
user_id=user.id,
|
|
|
|
|
updated_by_id=current_user.id,
|
|
|
|
|
original_mobile_number=user.mobile_number,
|
2023-08-25 09:12:23 -07:00
|
|
|
new_mobile_number=new_number,
|
2021-07-12 15:29:53 +01:00
|
|
|
)
|
2019-02-25 14:27:37 +00:00
|
|
|
finally:
|
2023-08-25 09:12:23 -07:00
|
|
|
session.pop("team_member_mobile_change", None)
|
2019-02-22 16:01:04 +00:00
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
return redirect(url_for(".manage_users", service_id=service_id))
|
2019-02-22 15:06:42 +00:00
|
|
|
|
|
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
"views/manage-users/confirm-edit-user-mobile-number.html",
|
2019-02-22 15:06:42 +00:00
|
|
|
user=user,
|
|
|
|
|
service_id=service_id,
|
2023-08-25 09:12:23 -07:00
|
|
|
new_mobile_number=new_number,
|
2019-02-22 15:06:42 +00:00
|
|
|
)
|
2019-02-22 12:28:18 +00:00
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
@main.route(
|
|
|
|
|
"/services/<uuid:service_id>/cancel-invited-user/<uuid:invited_user_id>",
|
|
|
|
|
methods=["GET"],
|
|
|
|
|
)
|
|
|
|
|
@user_has_permissions("manage_service")
|
2016-03-01 16:12:26 +00:00
|
|
|
def cancel_invited_user(service_id, invited_user_id):
|
2019-02-25 16:51:37 +00:00
|
|
|
current_service.cancel_invite(invited_user_id)
|
2016-03-01 16:12:26 +00:00
|
|
|
|
2020-08-17 14:30:09 +01:00
|
|
|
invited_user = InvitedUser.by_id_and_service_id(service_id, invited_user_id)
|
2023-04-25 19:36:18 -04:00
|
|
|
create_cancel_user_invite_to_service_event(
|
|
|
|
|
email_address=invited_user.email_address,
|
|
|
|
|
canceled_by_id=current_user.id,
|
|
|
|
|
service_id=service_id,
|
|
|
|
|
)
|
2020-08-17 14:30:09 +01:00
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
flash(f"Invitation cancelled for {invited_user.email_address}", "default_with_tick")
|
|
|
|
|
return redirect(url_for("main.manage_users", service_id=service_id))
|