Connect LDAP Requests to the StudyInfo service so we get back additional details.
This commit is contained in:
parent
2d3402a719
commit
edbd75bb75
|
@ -43,7 +43,7 @@ PB_REQUIRED_DOCS_URL = environ.get('PB_REQUIRED_DOCS_URL', default=PB_BASE_URL +
|
|||
PB_STUDY_DETAILS_URL = environ.get('PB_STUDY_DETAILS_URL', default=PB_BASE_URL + "study?studyid=%i")
|
||||
|
||||
LDAP_URL = environ.get('LDAP_URL', default="ldap.virginia.edu")
|
||||
|
||||
LDAP_TIMEOUT_SEC = environ.get('LDAP_TIMEOUT_SEC', default=3)
|
||||
print('=== USING DEFAULT CONFIG: ===')
|
||||
print('DB_HOST = ', DB_HOST)
|
||||
print('DEVELOPMENT = ', DEVELOPMENT)
|
||||
|
|
|
@ -1,12 +1,16 @@
|
|||
from crc import session
|
||||
from ldap3.core.exceptions import LDAPSocketOpenError
|
||||
|
||||
from crc import session, app
|
||||
from crc.api.common import ApiError
|
||||
from crc.models.study import StudyModel, StudySchema
|
||||
from crc.scripts.script import Script
|
||||
from crc.services.ldap_service import LdapService
|
||||
from crc.services.protocol_builder import ProtocolBuilderService
|
||||
from crc.services.workflow_processor import WorkflowProcessor
|
||||
|
||||
|
||||
class StudyInfo(Script):
|
||||
|
||||
"""Just your basic class that can pull in data from a few api endpoints and do a basic task."""
|
||||
pb = ProtocolBuilderService()
|
||||
type_options = ['info', 'investigators', 'details']
|
||||
|
@ -78,5 +82,18 @@ class StudyInfo(Script):
|
|||
"""Convert array of investigators from protocol builder into a dictionary keyed on the type"""
|
||||
output = {}
|
||||
for i in pb_investigators:
|
||||
output[i["INVESTIGATORTYPE"]] = {"user_id": i["NETBADGEID"], "type_full": i["INVESTIGATORTYPEFULL"]}
|
||||
dict = {"user_id": i["NETBADGEID"], "type_full": i["INVESTIGATORTYPEFULL"]}
|
||||
dict.update(self.get_ldap_dict_if_available(i["NETBADGEID"]))
|
||||
output[i["INVESTIGATORTYPE"]] = dict
|
||||
return output
|
||||
|
||||
def get_ldap_dict_if_available(self, user_id):
|
||||
try:
|
||||
ldap_service = LdapService()
|
||||
return ldap_service.user_info(user_id).__dict__
|
||||
except ApiError:
|
||||
app.logger.info(ApiError.message)
|
||||
return {}
|
||||
except LDAPSocketOpenError:
|
||||
app.logger.info("Failed to connect to LDAP Server.")
|
||||
return {}
|
|
@ -1,13 +1,15 @@
|
|||
from crc import app
|
||||
from ldap3 import Connection
|
||||
from ldap3 import Connection, Server
|
||||
|
||||
from crc.api.common import ApiError
|
||||
|
||||
|
||||
class LdapUserInfo(object):
|
||||
|
||||
def __init__(self, entry):
|
||||
self.display_name = entry.displayName
|
||||
self.display_name = entry.displayName.value
|
||||
self.given_name = ", ".join(entry.givenName)
|
||||
self.email = entry.mail
|
||||
self.email = entry.mail.value
|
||||
self.telephone_number= ", ".join(entry.telephoneNumber)
|
||||
self.title = ", ".join(entry.title)
|
||||
self.department = ", ".join(entry.uvaDisplayDepartment)
|
||||
|
@ -15,6 +17,8 @@ class LdapUserInfo(object):
|
|||
self.sponsor_type = ", ".join(entry.uvaPersonSponsoredType)
|
||||
|
||||
|
||||
|
||||
|
||||
class LdapService(object):
|
||||
search_base = "ou=People,o=University of Virginia,c=US"
|
||||
attributes = ['cn', 'displayName', 'givenName', 'mail', 'objectClass', 'UvaDisplayDepartment',
|
||||
|
@ -22,17 +26,24 @@ class LdapService(object):
|
|||
search_string = "(&(objectclass=person)(uid=%s))"
|
||||
|
||||
def __init__(self, connection=None):
|
||||
self.conn = None
|
||||
if connection is None:
|
||||
self.LDAP_URL = app.config['LDAP_URL']
|
||||
self.conn = Connection(self.LDAP_URL, auto_bind=True, client_strategy='SYNC')
|
||||
server = Server(app.config['LDAP_URL'], connect_timeout=app.config['LDAP_TIMEOUT_SEC'])
|
||||
self.conn = Connection(server,
|
||||
auto_bind=True,
|
||||
receive_timeout=app.config['LDAP_TIMEOUT_SEC'],
|
||||
)
|
||||
else:
|
||||
self.conn = connection
|
||||
|
||||
def __del__(self):
|
||||
self.conn.unbind()
|
||||
if self.conn:
|
||||
self.conn.unbind()
|
||||
|
||||
def user_info(self, uva_uid):
|
||||
search_string = LdapService.search_string % uva_uid
|
||||
self.conn.search(LdapService.search_base, search_string, attributes=LdapService.attributes)
|
||||
if len(self.conn.entries) < 1:
|
||||
raise ApiError("missing_ldap_record", "Unable to locate a user with id %s in LDAP" % uva_uid)
|
||||
entry = self.conn.entries[0]
|
||||
return(LdapUserInfo(entry))
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import os
|
||||
|
||||
from crc import app
|
||||
from crc.api.common import ApiError
|
||||
from crc.services.ldap_service import LdapService
|
||||
from tests.base_test import BaseTest
|
||||
from ldap3 import Server, Connection, ALL, MOCK_SYNC
|
||||
|
@ -31,3 +32,9 @@ class TestLdapService(BaseTest):
|
|||
self.assertEqual("faculty", user_info.affiliation)
|
||||
self.assertEqual("Staff", user_info.sponsor_type)
|
||||
|
||||
def test_find_missing_user(self):
|
||||
try:
|
||||
user_info = self.ldap_service.user_info("nosuch")
|
||||
self.assertFalse(True, "An API error should be raised.")
|
||||
except ApiError as ae:
|
||||
self.assertEquals("missing_ldap_record", ae.code)
|
Loading…
Reference in New Issue