def create_user_groups(self, payload): """ This function will hit the key_cloak api to create the user group and return the response fetched for the key_cloak api :param payload: Http request payload :return: response fetched from the key_cloak api """ try: url = KEY_CLOAK_API_URI + KEY_CLOAK_USER_GROUPS_ENDPOINT data_dict = payload headers = { 'Content-Type': 'application/json', 'Accept': '*/*', 'Authorization': self.auth_header } resp = http_post(url, data_dict, headers1=headers) if not resp or int(resp['status_code']) != 200: return { 'status_code': int(resp['status_code']), 'msg': 'not able to create user group', 'status': 'Failure' } resp['msg'] = resp['result']['message'] return resp except Exception as e: self.context.log(message=str(e), obj={"tb": traceback.format_exc()}) return { 'status': 'failure', 'status_code': STATUS_CODES['INTERNAL_SERVER_ERROR'], 'msg': 'Failed to process create user group request.' }
def link_users_userroles(self, payload): """ This function will hit the key_cloak api to link the user/users :param payload: Http request payload :return: dictionary as response """ try: if 'userIds' not in payload: return { 'status_code': STATUS_CODES['PRECONDITION_FAILED'], 'msg': 'user_ids parameter missing in payload', 'status': 'Failure' } if 'roleId' not in payload: return { 'status_code': STATUS_CODES['PRECONDITION_FAILED'], 'msg': 'roleId parameter missing in payload', 'status': 'Failure' } user_role = payload['roleId'] url = KEY_CLOAK_API_URI + KEY_CLOAK_USER_ROLES_ENDPOINT url += '/' + user_role + '/' + KEY_CLOAK_USERS_ENDPOINT headers = { 'Content-Type': 'application/json', 'Accept': '*/*', 'Authorization': self.auth_header } resp = http_post(url, payload['userIds'], headers1=headers) if not resp or int(resp['status_code']) != 200: return { 'status_code': int(resp['status_code']), 'msg': 'not able to create user', 'status': 'Failure' } resp['msg'] = resp['result']['message'] return resp except Exception as e: self.context.log(message=str(e), obj={"tb": traceback.format_exc()}) return { 'status': 'failure', 'status_code': STATUS_CODES['INTERNAL_SERVER_ERROR'], 'msg': 'Failed to process link user/users request.' }
def create_nested_groups(self, payload, user_group_id): """ This function will create the user subgroup in existing user_group and return the dictionary as response :param payload: Http request payload :param user_group_id: Existing user group Id :return: dictionary as response """ try: if not user_group_id: return { 'status': 'failure', 'status_code': STATUS_CODES['PRECONDITION_REQUIRED'], 'msg': 'user group id is missing in request url.' } url = KEY_CLOAK_API_URI + KEY_CLOAK_USER_GROUPS_ENDPOINT data_dict = payload headers = { 'Content-Type': 'application/json', 'Accept': '*/*', 'Authorization': self.auth_header } resp = http_post(url, data_dict, headers1=headers) if not resp or int(resp['status_code']) != 200: return { 'status_code': int(resp['status_code']), 'msg': 'not able to create user group', 'status': 'Failure' } resp['msg'] = resp['result']['message'] return resp except Exception as e: self.context.log(message=str(e), obj={"tb": traceback.format_exc()}) return { 'status': 'failure', 'status_code': STATUS_CODES['INTERNAL_SERVER_ERROR'], 'msg': 'Failed to process create user group request.' }