Skip to content

Commit

Permalink
OM-47: add validation and translations (#10)
Browse files Browse the repository at this point in the history
* OM-47: assign msystem roles in imis system

* OM-47: fix user role creation

* OM-47: delete roles if exist

* OM-47: remove exists from migration

* OM-47: fix migration

* OM-47: adjust updating roles

* OM-47: address review comments

* OM-47: update migration

* OM-47: update migration

* OM-47: update migration

* OM-47: add validation and translations

* OM-47: add validation and translations

---------

Co-authored-by: Jan <[email protected]>
  • Loading branch information
jdolkowski and Jan authored Oct 31, 2023
1 parent 5df3f2b commit 81098af
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
20 changes: 20 additions & 0 deletions locale/en/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
msgid "resident_national_id_not_valid"
msgstr "Worker national id not valid."

msgid "organization_national_id_not_valid"
msgstr "Organization national id not valid."

msgid "vehicle_national_id_not_valid"
msgstr "Vehicle national id not valid."

msgid "resident_national_id_checksum_not_valid"
msgstr "Worker national id checksum not valid."

msgid "organization_national_id_checksum_not_valid"
msgstr "Organization national id checksum not valid."

msgid "vehicle_national_id_checksum_not_valid"
msgstr "Vehicle national id checksum not valid."

msgid "role_validation.unknown_role"
msgstr "Unknown role coming from mPass."
1 change: 1 addition & 0 deletions msystems/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class MsystemsConfig(AppConfig):
ADMIN = 'Admin'
INSPECTOR = 'Inspector'
EMPLOYER = 'Employer'
IMIS_ADMIN = 'IMIS Administrator'
##### ------------------ ####

saml_config = None
Expand Down
14 changes: 13 additions & 1 deletion msystems/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from django.db import transaction
from django.db.models import Q
from secrets import token_hex
from django.core.exceptions import ValidationError
from django.utils.translation import gettext as _

from core.models import User, InteractiveUser, Role, UserRole
from core.services.userServices import create_or_update_user_districts
Expand Down Expand Up @@ -84,6 +86,9 @@ def _update_user_legal_entities(self, user: User, user_data: dict) -> None:
def _update_user_roles(self, user, user_data):
msystem_roles_list = user_data.get('Role', [MsystemsConfig.EMPLOYER])

for role in msystem_roles_list:
self._validate_incoming_roles(role)

self._delete_old_user_roles(user, msystem_roles_list)
self._add_new_user_roles(user, msystem_roles_list)

Expand Down Expand Up @@ -158,4 +163,11 @@ def _remove_previous_user_roles(self, i_user):
role.delete_history()

def _parse_msystem_role_to_imis_role(self, msystem_role):
return Role.objects.filter(name=msystem_role).first()
role_string = msystem_role
if msystem_role == MsystemsConfig.ADMIN:
role_string = MsystemsConfig.IMIS_ADMIN
return Role.objects.filter(name=role_string).first()

def _validate_incoming_roles(self, role):
if role not in [MsystemsConfig.ADMIN, MsystemsConfig.EMPLOYER, MsystemsConfig.INSPECTOR]:
raise ValidationError(_("role_validation.unknown_role"))

0 comments on commit 81098af

Please sign in to comment.