Merge pull request #185 from sartography/feature/107_current_user_info

calling ldap with no argument will return information about the curre…
This commit is contained in:
Dan Funk 2020-08-14 09:18:10 -04:00 committed by GitHub
commit c57ac6d302
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 10 deletions

View File

@ -1,19 +1,24 @@
import copy import copy
from flask import g
from crc import app from crc import app
from crc.api.common import ApiError from crc.api.common import ApiError
from crc.scripts.script import Script from crc.scripts.script import Script
from crc.services.ldap_service import LdapService from crc.services.ldap_service import LdapService
from crc.services.user_service import UserService
class Ldap(Script): class Ldap(Script):
"""This Script allows to be introduced as part of a workflow and called from there, taking """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): def get_description(self):
return """ return """
Attempts to create a dictionary with person details, using the 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: Examples:
supervisor_info = ldap(supervisor_uid) // Sets the supervisor information to ldap details for the given uid. 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) return self.set_users_info_in_task(task, args)
def set_users_info_in_task(self, task, args): def set_users_info_in_task(self, task, args):
if len(args) != 1: if len(args) > 1:
raise ApiError(code="missing_argument", raise ApiError(code="invalid_argument",
message="Ldap takes a single argument, the " message="Ldap takes at most one argument, the "
"UID for the person we want to look up") "UID for the person we want to look up.")
uid = args[0] if len(args) < 1:
user_info_dict = {} if UserService.has_user():
uid = UserService.current_user().uid
else:
uid = args[0]
user_info = LdapService.user_info(uid) user_info = LdapService.user_info(uid)
user_info_dict = { user_info_dict = {
"display_name": user_info.display_name, "display_name": user_info.display_name,

View File

@ -1,9 +1,12 @@
from flask import g
from tests.base_test import BaseTest 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.services.workflow_processor import WorkflowProcessor
from crc.scripts.ldap import Ldap from crc.scripts.ldap import Ldap
from crc.api.common import ApiError from crc.api.common import ApiError
from crc import db, mail
class TestLdapLookupScript(BaseTest): class TestLdapLookupScript(BaseTest):
@ -44,6 +47,19 @@ class TestLdapLookupScript(BaseTest):
with(self.assertRaises(ApiError)): with(self.assertRaises(ApiError)):
user_details = script.do_task(task, workflow.study_id, workflow.id, "PIComputingID") 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): def test_bpmn_task_receives_user_details(self):
workflow = self.create_workflow('ldap_replace') workflow = self.create_workflow('ldap_replace')