diff --git a/crc/scripts/ldap.py b/crc/scripts/ldap.py index 577d4a75..2e7d1e6c 100644 --- a/crc/scripts/ldap.py +++ b/crc/scripts/ldap.py @@ -1,19 +1,24 @@ import copy +from flask import g + from crc import app from crc.api.common import ApiError from crc.scripts.script import Script from crc.services.ldap_service import LdapService +from crc.services.user_service import UserService class Ldap(Script): """This Script allows to be introduced as part of a workflow and called from there, taking - a UID (or several) as input and looking it up through LDAP to return the person's details """ + a UID (or several) as input and looking it up through LDAP to return the person's details. + If no user id is specified, returns information about the current user.""" def get_description(self): return """ Attempts to create a dictionary with person details, using the -provided argument (a UID) and look it up through LDAP. +provided argument (a UID) and look it up through LDAP. If no UID is +provided, then returns information about the current user. Examples: supervisor_info = ldap(supervisor_uid) // Sets the supervisor information to ldap details for the given uid. @@ -26,13 +31,15 @@ supervisor_info = ldap(supervisor_uid) // Sets the supervisor information to l return self.set_users_info_in_task(task, args) def set_users_info_in_task(self, task, args): - if len(args) != 1: - raise ApiError(code="missing_argument", - message="Ldap takes a single argument, the " - "UID for the person we want to look up") - uid = args[0] - user_info_dict = {} - + if len(args) > 1: + raise ApiError(code="invalid_argument", + message="Ldap takes at most one argument, the " + "UID for the person we want to look up.") + if len(args) < 1: + if UserService.has_user(): + uid = UserService.current_user().uid + else: + uid = args[0] user_info = LdapService.user_info(uid) user_info_dict = { "display_name": user_info.display_name, diff --git a/tests/ldap/test_ldap_lookup_script.py b/tests/ldap/test_ldap_lookup_script.py index 0a88a899..0f8f48b1 100644 --- a/tests/ldap/test_ldap_lookup_script.py +++ b/tests/ldap/test_ldap_lookup_script.py @@ -1,9 +1,12 @@ +from flask import g + from tests.base_test import BaseTest +from crc import db +from crc.models.user import UserModel from crc.services.workflow_processor import WorkflowProcessor from crc.scripts.ldap import Ldap from crc.api.common import ApiError -from crc import db, mail class TestLdapLookupScript(BaseTest): @@ -44,6 +47,19 @@ class TestLdapLookupScript(BaseTest): with(self.assertRaises(ApiError)): user_details = script.do_task(task, workflow.study_id, workflow.id, "PIComputingID") + def test_get_current_user_details(self): + self.load_example_data() + self.create_reference_document() + workflow = self.create_workflow('empty_workflow') + processor = WorkflowProcessor(workflow) + task = processor.next_task() + + script = Ldap() + g.user = db.session.query(UserModel).filter(UserModel.uid=='dhf8r').first() + user_details = script.do_task(task, workflow.study_id, workflow.id) + self.assertEqual(user_details['display_name'], 'Dan Funk') + + def test_bpmn_task_receives_user_details(self): workflow = self.create_workflow('ldap_replace')