use our json encoder to dump the user for get_current_user script w/ burnettk

This commit is contained in:
jasquat 2023-02-06 14:11:15 -05:00
parent b4a3156256
commit 2a2855e0da
5 changed files with 28 additions and 21 deletions

View File

@ -1006,7 +1006,7 @@
"emailVerified" : false, "emailVerified" : false,
"email" : "kb@sartography.com", "email" : "kb@sartography.com",
"attributes" : { "attributes" : {
"bamboo_id" : [ "42" ] "spiffworkflow-employeeid" : [ "42" ]
}, },
"credentials" : [ { "credentials" : [ {
"id" : "2c0be363-038f-48f1-86d6-91fdd28657cf", "id" : "2c0be363-038f-48f1-86d6-91fdd28657cf",
@ -2120,7 +2120,7 @@
} }
}, { }, {
"id" : "a7692d41-b905-4049-9004-f6bea690051d", "id" : "a7692d41-b905-4049-9004-f6bea690051d",
"name" : "bamboo_id", "name" : "spiffworkflow-employeeid",
"protocol" : "openid-connect", "protocol" : "openid-connect",
"protocolMapper" : "oidc-usermodel-attribute-mapper", "protocolMapper" : "oidc-usermodel-attribute-mapper",
"consentRequired" : false, "consentRequired" : false,
@ -2128,10 +2128,10 @@
"aggregate.attrs" : "false", "aggregate.attrs" : "false",
"userinfo.token.claim" : "true", "userinfo.token.claim" : "true",
"multivalued" : "false", "multivalued" : "false",
"user.attribute" : "bamboo_id", "user.attribute" : "spiffworkflow-employeeid",
"id.token.claim" : "true", "id.token.claim" : "true",
"access.token.claim" : "true", "access.token.claim" : "true",
"claim.name" : "bamboo_id" "claim.name" : "spiffworkflow-employeeid"
} }
} ], } ],
"defaultClientScopes" : [ "web-origins", "acr", "profile", "roles", "email" ], "defaultClientScopes" : [ "web-origins", "acr", "profile", "roles", "email" ],
@ -3652,4 +3652,4 @@
"clientPolicies" : { "clientPolicies" : {
"policies" : [ ] "policies" : [ ]
} }
} }

View File

@ -34,9 +34,9 @@ class UserModel(SpiffworkflowBaseDBModel):
service_id = db.Column(db.String(255), nullable=False, unique=False) service_id = db.Column(db.String(255), nullable=False, unique=False)
display_name = db.Column(db.String(255)) display_name = db.Column(db.String(255))
email = db.Column(db.String(255)) email = db.Column(db.String(255))
tenant_specific_field_1 = db.Column(db.String(255)) tenant_specific_field_1: str = db.Column(db.String(255))
tenant_specific_field_2 = db.Column(db.String(255)) tenant_specific_field_2: str = db.Column(db.String(255))
tenant_specific_field_3 = db.Column(db.String(255)) tenant_specific_field_3: str = db.Column(db.String(255))
updated_at_in_seconds: int = db.Column(db.Integer) updated_at_in_seconds: int = db.Column(db.Integer)
created_at_in_seconds: int = db.Column(db.Integer) created_at_in_seconds: int = db.Column(db.Integer)

View File

@ -1,6 +1,7 @@
"""Get current user.""" """Get current user."""
from typing import Any from typing import Any
from flask import current_app
from flask import g from flask import g
from spiffworkflow_backend.models.script_attributes_context import ( from spiffworkflow_backend.models.script_attributes_context import (
@ -26,4 +27,7 @@ class GetCurrentUser(Script):
**kwargs: Any **kwargs: Any
) -> Any: ) -> Any:
"""Run.""" """Run."""
return g.user.__dict__ # dump the user using our json encoder and then load it back up as a dict
# to remove unwanted field types
user_as_json_string = current_app.json.dumps(g.user)
return current_app.json.loads(user_as_json_string)

View File

@ -82,7 +82,8 @@
<bpmn:incoming>Flow_0bgkfue</bpmn:incoming> <bpmn:incoming>Flow_0bgkfue</bpmn:incoming>
<bpmn:outgoing>Flow_1ivhu7x</bpmn:outgoing> <bpmn:outgoing>Flow_1ivhu7x</bpmn:outgoing>
<bpmn:script>approver = get_current_user() <bpmn:script>approver = get_current_user()
lane_owners["Finance Team"].remove(approver)</bpmn:script> username = approver['username']
lane_owners["Finance Team"].remove(username)</bpmn:script>
</bpmn:scriptTask> </bpmn:scriptTask>
</bpmn:process> </bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1"> <bpmndi:BPMNDiagram id="BPMNDiagram_1">

View File

@ -1,23 +1,20 @@
"""Test_get_localtime.""" """Test_get_localtime."""
from flask.app import Flask import json
from flask import g from flask import g
from flask.app import Flask
from flask.testing import FlaskClient from flask.testing import FlaskClient
from spiffworkflow_backend.models.script_attributes_context import ScriptAttributesContext
from spiffworkflow_backend.scripts.get_current_user import GetCurrentUser
from tests.spiffworkflow_backend.helpers.base_test import BaseTest from tests.spiffworkflow_backend.helpers.base_test import BaseTest
from tests.spiffworkflow_backend.helpers.test_data import load_test_spec
from spiffworkflow_backend.models.db import db from spiffworkflow_backend.models.db import db
from spiffworkflow_backend.models.group import GroupModel from spiffworkflow_backend.models.script_attributes_context import (
from spiffworkflow_backend.models.user import UserModel ScriptAttributesContext,
from spiffworkflow_backend.services.process_instance_processor import (
ProcessInstanceProcessor,
) )
from spiffworkflow_backend.services.user_service import UserService from spiffworkflow_backend.models.user import UserModel
from spiffworkflow_backend.scripts.get_current_user import GetCurrentUser
class TestGetCurrentUser(BaseTest): class TestGetCurrentUser(BaseTest):
def test_get_current_user( def test_get_current_user(
self, self,
app: Flask, app: Flask,
@ -29,6 +26,9 @@ class TestGetCurrentUser(BaseTest):
testuser1 = self.find_or_create_user("testuser1") testuser1 = self.find_or_create_user("testuser1")
testuser1.tenant_specific_field_1 = "456" testuser1.tenant_specific_field_1 = "456"
db.session.add(testuser1) db.session.add(testuser1)
db.session.commit()
testuser1 = self.find_or_create_user("testuser1")
g.user = testuser1 g.user = testuser1
process_model_identifier = "test_process_model" process_model_identifier = "test_process_model"
process_instance_id = 1 process_instance_id = 1
@ -41,4 +41,6 @@ class TestGetCurrentUser(BaseTest):
result = GetCurrentUser().run( result = GetCurrentUser().run(
script_attributes_context, script_attributes_context,
) )
assert result['username'] == "testuser1" assert result["username"] == "testuser1"
assert result["tenant_specific_field_1"] == "456"
json.dumps(result)