cr-connect-workflow/crc/scripts/ldap_replace.py

61 lines
2.4 KiB
Python
Raw Normal View History

2020-07-20 03:53:18 +00:00
import copy
from crc import app
from crc.api.common import ApiError
from crc.scripts.script import Script
from crc.services.ldap_service import LdapService
2020-07-22 02:53:24 +00:00
class LdapReplace(Script):
2020-07-20 03:53:18 +00:00
"""This Script allows to be introduced as part of a workflow and called from there, taking
2020-07-22 02:53:24 +00:00
a UID (or several) as input and looking it up through LDAP to return the person's details """
2020-07-20 03:53:18 +00:00
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.
2020-07-22 02:53:24 +00:00
Examples:
#! LdapReplace supervisor
#! LdapReplace supervisor collaborator
#! LdapReplace supervisor cosupervisor collaborator
2020-07-20 03:53:18 +00:00
"""
def do_task_validate_only(self, task, *args, **kwargs):
2020-07-22 02:53:24 +00:00
self.set_users_info_in_task(task, args)
2020-07-20 03:53:18 +00:00
def do_task(self, task, *args, **kwargs):
args = [arg for arg in args if type(arg) == str]
2020-07-22 02:53:24 +00:00
self.set_users_info_in_task(task, args)
2020-07-20 03:53:18 +00:00
2020-07-22 02:53:24 +00:00
def set_users_info_in_task(self, task, args):
2020-07-20 03:53:18 +00:00
if len(args) < 1:
raise ApiError(code="missing_argument",
2020-07-22 02:53:24 +00:00
message="Ldap replace script requires at least one argument. The "
"UID for the person(s) we want to look up")
users_info = {}
for arg in args:
uid = task.workflow.script_engine.evaluate_expression(task, arg)
if not isinstance(uid, str):
raise ApiError(code="invalid_argument",
message="Ldap replace script found an invalid argument, type string is required")
user_info_dict = {}
try:
user_info = LdapService.user_info(uid)
user_info_dict = {
"display_name": user_info.display_name,
"given_name": user_info.given_name,
"email_address": user_info.email_address,
"telephone_number": user_info.telephone_number,
"title": user_info.title,
"department": user_info.department,
"affiliation": user_info.affiliation,
"sponsor_type": user_info.sponsor_type,
"uid": user_info.uid,
"proper_name": user_info.proper_name()
}
except:
app.logger.error(f'Ldap replace failed for UID {uid}')
task.data[arg] = user_info_dict