Skip to content

Commit

Permalink
Merge pull request #2417 from auslin-aot/fwf-4088-export-import-autho…
Browse files Browse the repository at this point in the history
…rization-updates-username

FWF-4088: Form Export Import authorization updates in userName field
  • Loading branch information
arun-s-aot authored Dec 12, 2024
2 parents c3b06c1 + afa8432 commit 658cfda
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 54 deletions.
129 changes: 81 additions & 48 deletions forms-flow-api/src/formsflow_api/schemas/import_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,67 +8,100 @@
"properties": {
"forms": {
"type": "array",
"properties": {
"formTitle": {"type": "string"},
"formDescription": {"type": "string"},
"anonymous": {"type": "boolean"},
"type": {"type": "string"},
"content": {"type": "object"},
"minItems": 1,
"items": {
"type": "object",
"properties": {
"formTitle": {"type": "string"},
"formDescription": {"type": "string"},
"anonymous": {"type": "boolean"},
"type": {"type": "string"},
"content": {"type": "object"},
},
"required": [
"formTitle",
"formDescription",
"content",
"anonymous",
"type",
],
},
"required": [
"formTitle",
"formDescription",
"content",
"anonymous",
"type",
],
},
"workflows": {
"type": "array",
"properties": {
"processKey": {"type": "string"},
"processName": {"type": "string"},
"processType": {"type": "string"},
"type": {"type": "string"},
"content": {"type": "string"},
"minItems": 1,
"items": {
"type": "object",
"properties": {
"processKey": {"type": "string"},
"processName": {"type": "string"},
"processType": {"type": "string"},
"type": {"type": "string"},
"content": {"type": "string"},
},
"required": [
"content",
"processKey",
"processName",
"processType",
"type",
],
},
"required": ["content", "processKey", "processName", "processType", "type"],
},
"authorizations": {
"type": "array",
"properties": {
"APPLICATION": {
"type": "array",
"properties": {
"resourceId": {"type": "string"},
"resourceDetails": {"type": "object"},
"roles": {"type": "array"},
"userName": {"type": "string"},
"minItems": 1,
"items": {
"type": "object",
"properties": {
"APPLICATION": {
"type": "object",
"properties": {
"resourceId": {"type": "string"},
"resourceDetails": {"type": "object"},
"roles": {"type": "array", "items": {"type": "string"}},
"userName": {"type": ["string", "null"]},
},
"required": [
"resourceId",
"resourceDetails",
"roles",
"userName",
],
},
"required": ["resourceId", "resourceDetails", "roles", "userName"],
},
"FORM": {
"type": "array",
"properties": {
"resourceId": {"type": "string"},
"resourceDetails": {"type": "object"},
"roles": {"type": "array"},
"userName": {"type": "string"},
"FORM": {
"type": "object",
"properties": {
"resourceId": {"type": "string"},
"resourceDetails": {"type": "object"},
"roles": {"type": "array", "items": {"type": "string"}},
"userName": {"type": ["string", "null"]},
},
"required": [
"resourceId",
"resourceDetails",
"roles",
"userName",
],
},
"required": ["resourceId", "resourceDetails", "roles", "userName"],
},
"DESIGNER": {
"type": "array",
"properties": {
"resourceId": {"type": "string"},
"resourceDetails": {"type": "object"},
"roles": {"type": "array"},
"userName": {"type": "string"},
"DESIGNER": {
"type": "object",
"properties": {
"resourceId": {"type": "string"},
"resourceDetails": {"type": "object"},
"roles": {"type": "array", "items": {"type": "string"}},
"userName": {"type": ["string", "null"]},
},
"required": [
"resourceId",
"resourceDetails",
"roles",
"userName",
],
},
"required": ["resourceId", "resourceDetails", "roles", "userName"],
},
"required": ["APPLICATION", "FORM", "DESIGNER"],
},
"required": ["APPLICATION", "FORM", "DESIGNER"],
},
},
"required": ["forms", "workflows", "rules", "authorizations"],
Expand Down
13 changes: 11 additions & 2 deletions forms-flow-api/src/formsflow_api/services/authorization.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ def is_dashboard_authorized(self, resource_id: str, **kwargs) -> bool:

@user_context
def create_authorization(
self, auth_type: str, resource: Dict[str, str], is_designer: bool, **kwargs
self,
auth_type: str,
resource: Dict[str, str],
is_designer: bool,
edit_import_designer=False,
**kwargs
) -> Dict[str, any]:
"""Create authorization record."""
user: UserContext = kwargs["user"]
Expand All @@ -86,9 +91,13 @@ def create_authorization(
)
roles = resource.get("roles")
if auth:
# Incase of edit import, user_name default to the user who created it
user_name = (
auth.created_by if edit_import_designer else resource.get("userName")
)
auth.roles = roles
auth.resource_details = resource.get("resourceDetails")
auth.user_name = resource.get("userName")
auth.user_name = user_name
auth.modified = datetime.datetime.now()
auth.modified_by = user.user_name
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ def _sanitize_form_json(self, form_json, tenant_key):
"submissionAccess",
"parentFormId",
"owner",
"tenantKey",
]
for key in keys_to_remove:
form_json.pop(key, None)
Expand Down Expand Up @@ -602,7 +603,7 @@ def _get_authorizations(self, resource_id: str, user) -> dict:
"resourceId": auth.resource_id,
"resourceDetails": auth.resource_details,
"roles": auth.roles,
"userName": auth.user_name,
"userName": None,
}
return auth_detail

Expand Down
17 changes: 14 additions & 3 deletions forms-flow-api/src/formsflow_api/services/import_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def set_form_and_submission_access(self, form_data, anonymous, **kwargs):
current_app.logger.debug(f"Admin url: {url}")
response = AdminService.get_request(url, user.bearer_token)
role_ids = response["form"]
form_data["tenantKey"] = user.tenant_key
else:
role_ids = Cache.get("formio_role_ids")
if not role_ids:
Expand Down Expand Up @@ -134,18 +135,28 @@ def set_form_and_submission_access(self, form_data, anonymous, **kwargs):
]
return form_data

def create_authorization(self, data):
@user_context
def create_authorization(self, data, new_import=False, **kwargs):
"""Create authorization."""
for auth_type in AuthType:
if auth_type.value in [
AuthType.APPLICATION.value,
AuthType.FORM.value,
AuthType.DESIGNER.value,
]:
auth_data = data.get(auth_type.value.upper())
is_designer = auth_type.value == AuthType.DESIGNER.value
# If edit import, add created_by user as username in case of designer
edit_import_designer = not new_import and is_designer
# Update designer's username if new_import
if new_import is True and is_designer:
user: UserContext = kwargs["user"]
auth_data["userName"] = user.user_name
self.auth_service.create_authorization(
auth_type.value.upper(),
data.get(auth_type.value.upper()),
auth_data,
is_designer=True,
edit_import_designer=edit_import_designer,
)

def get_latest_version_workflow(self, process_name):
Expand Down Expand Up @@ -425,7 +436,7 @@ def import_new_form_workflow(
for auth in file_data["authorizations"][0]:
file_data["authorizations"][0][auth]["resourceId"] = form_id
# Create authorizations for the form
self.create_authorization(file_data["authorizations"][0])
self.create_authorization(file_data["authorizations"][0], new_import=True)
# validate process key already exists, if exists append mapper id to process_key.
updated_process_name = (
FormProcessMapperService.validate_process_and_update_mapper(
Expand Down

0 comments on commit 658cfda

Please sign in to comment.