Add checks for other fields

This commit is contained in:
zandercymatics 2024-02-26 10:49:38 -07:00
parent 4d4cd8257c
commit d416ea5138
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
2 changed files with 36 additions and 35 deletions

View file

@ -824,42 +824,43 @@ class DomainApplicationAdminForm(forms.ModelForm):
status = cleaned_data.get("status")
investigator = cleaned_data.get("investigator")
# TODO - need some way of determining if a change has actually occurred and to enforce this only then
if status == DomainApplication.ApplicationStatus.APPROVED:
# Checks the "investigators" field for validity.
# That field must obey certain conditions when an application is approved.
# Will call "add_error" if any issues are found.
self._check_investigators_on_approval(investigator)
checked_statuses = [
DomainApplication.ApplicationStatus.APPROVED,
DomainApplication.ApplicationStatus.IN_REVIEW,
DomainApplication.ApplicationStatus.ACTION_NEEDED,
DomainApplication.ApplicationStatus.REJECTED,
DomainApplication.ApplicationStatus.INELIGIBLE
]
# Checks the "investigators" field for validity.
# That field must obey certain conditions when an application is approved.
# Will call "add_error" if any issues are found.
#if status in checked_statuses:
#self._check_for_valid_investigator(investigator)
return cleaned_data
def _check_investigators_on_approval(self, investigator):
"""Checks the investigator field when an approval occurs"""
def _check_for_valid_investigator(self, investigator) -> bool:
"""
Checks if the investigator field is not none, and is staff.
Adds form errors on failure.
"""
# Get information about the current user making the request
current_user = self.request.user
is_superuser = current_user.has_perm("registrar.full_access_permission")
is_valid = False
error_message = None
# Check if an investigator is assigned. No approval is possible without one.
if investigator is not None:
if not investigator.is_staff:
# Investigators must be staff users.
# This is handled elsewhere, but we should check here as a precaution.
error_message = ApplicationStatusError.get_error_message(FSMErrorCodes.APPROVE_INVESTIGATOR_NOT_STAFF)
elif investigator != current_user and not is_superuser:
# If the submitting user is not the investigator, block this action.
# This is to enforce accountability. Superusers do not have this restriction.
error_message = ApplicationStatusError.get_error_message(
FSMErrorCodes.APPROVE_INVESTIGATOR_NOT_SUBMITTER
)
error_message = None
if investigator is None:
error_message = ApplicationStatusError.get_error_message(FSMErrorCodes.NO_INVESTIGATOR)
elif not investigator.is_staff:
error_message = ApplicationStatusError.get_error_message(FSMErrorCodes.INVESTIGATOR_NOT_STAFF)
else:
error_message = ApplicationStatusError.get_error_message(FSMErrorCodes.APPROVE_NO_INVESTIGATOR)
is_valid = True
# Add the error
if error_message is not None:
self.add_error("investigator", error_message)
return is_valid
class DomainApplicationAdmin(ListHeaderAdmin):
"""Custom domain applications admin class."""

View file

@ -77,15 +77,15 @@ class FSMErrorCodes(IntEnum):
"""Used when doing FSM transitions.
Overview of generic error codes:
- 1 APPROVE_DOMAIN_IN_USE The domain is already in use
- 2 APPROVE_NO_INVESTIGATOR No investigator is assigned when approving
- 3 APPROVE_INVESTIGATOR_NOT_STAFF Investigator is a non-staff user
- 4 APPROVE_INVESTIGATOR_NOT_SUBMITTER The form submitter is not the investigator
- 2 NO_INVESTIGATOR No investigator is assigned
- 3 INVESTIGATOR_NOT_STAFF Investigator is a non-staff user
- 4 INVESTIGATOR_NOT_SUBMITTER The form submitter is not the investigator
"""
APPROVE_DOMAIN_IN_USE = 1
APPROVE_NO_INVESTIGATOR = 2
APPROVE_INVESTIGATOR_NOT_STAFF = 3
APPROVE_INVESTIGATOR_NOT_SUBMITTER = 4
NO_INVESTIGATOR = 2
INVESTIGATOR_NOT_STAFF = 3
INVESTIGATOR_NOT_SUBMITTER = 4
# (Q for reviewers) What should this be called?
@ -98,10 +98,10 @@ class ApplicationStatusError(Exception):
_error_mapping = {
FSMErrorCodes.APPROVE_DOMAIN_IN_USE: ("Cannot approve. Requested domain is already in use."),
FSMErrorCodes.APPROVE_NO_INVESTIGATOR: ("Cannot approve. No investigator was assigned."),
FSMErrorCodes.APPROVE_INVESTIGATOR_NOT_STAFF: ("Cannot approve. Investigator is not a staff user."),
FSMErrorCodes.APPROVE_INVESTIGATOR_NOT_SUBMITTER: (
"Cannot approve. Only the assigned investigator can approve this application."
FSMErrorCodes.NO_INVESTIGATOR: ("No investigator was assigned."),
FSMErrorCodes.INVESTIGATOR_NOT_STAFF: ("Investigator is not a staff user."),
FSMErrorCodes.INVESTIGATOR_NOT_SUBMITTER: (
"Only the assigned investigator can make this change."
),
}