fix: move permission list validation from model level to service layer #5946
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
Whenever agencies change email domains, we remove their domain from the Agency collection. However, this means that if they had any collaborators in their form collaborator list whose emails were from the old domain, their form is forever locked. They cannot make changes to their form anymore, since the form document cannot be saved with invalid collaborators.
The reason for this occurring is validation that is applied at the model level. The validation is thus run on every document save, and the database rejects the invalid save on change.
Solution
Even though we would like for there to be model-level validation, this is not practical or informative. The model validations should specify invariants of the model over all operations throughout the system. However, the
agency
collection is always subject to change, and the validator will certainly not be invariant over time. Therefore, we should not in principle validate based on theagency
collection at the model level. All we can guarantee is that the list of collaborators are emails. What we would really like is that all new collaborators are whitelisted.Therefore, this PR removes the model-level validation based on the
agency
collection. In replacement, the validation occurs at the service layer. Any new collaborators added are extracted and checked for validity, while old collaborators are left untouched.Breaking Changes
Screenshots
Screen.Recording.2023-03-20.at.4.54.57.PM.mov
Screen.Recording.2023-03-20.at.5.00.24.PM.mov
Tests
data.gov.sg
is not in thegovtech
agency. Try to add[email protected]
email as a collaborator. This should fail.data.gov.sg
email domain to thegovtech
agency. Now add[email protected]
and[email protected]
email as collaborators. Then, remove thedata.gov.sg
email domain from thegovtech
agency. Perform the following actions in order.[email protected]
as a collaborator. This should work.[email protected]
. This should fail.[email protected]
as a collaborator. This should work.[email protected]
as a collaborator. This should work.Notes
Separately, I found an existing bug with transfer ownership. We currently allow ownership to be transferred to users whose emails are no longer valid domains belonging to any agency. You can see this in the second video clip, right at the end. I'll open a ticket for it after looking through the existing issues to see if it's duplicated.