Beispiel #1
0
 def __try_create(group_data):
     """
     Internal function to create a group
     :param group_data: json object with group details
     :return: void
     """
     logging.info(f'trying to create group {group_data.get("displayName")}')
     make_request(f'{GRAPH_URL}{RESOURCE_PATH}', 'POST', group_data)
     logging.info(
         f'group {group_data.get("displayName")} created successfully')
Beispiel #2
0
 def __try_create(user_data: dict) -> None:
     """
     Internal function to create user
     :param user_data: json object with user details
     :return: void
     """
     logging.info(
         f'trying to create user {user_data.get("userPrincipalName")}')
     make_request(f'{GRAPH_URL}{RESOURCE_PATH}', 'POST', user_data)
     logging.info(
         f'user {user_data.get("userPrincipalName")} created successfully')
Beispiel #3
0
def update_tasks(task_data_list):
    """
    Excpects a list of dicts [{id: <id>, task_data:{<task_data_dixt>}}]
    :param id: the id of the task in planner
    :param task_update_data: {@odata.etag:string, title:string, percentComplete:int, dueDate:dateTimeTimeZone, assigneePriority: string, bucketId:string}
    """
    for task_data in task_data_list:
        response = check_update_tasks_data(task_data)
        if response == "ok":
            task_id = task_data.get('id')
            make_request(f'{GRAPH_URL}{RESOURCE_PATH}{task_id}', 'PATCH',task_data)

    return response
Beispiel #4
0
    def __try_delete(group_data):
        """
        Internal function to delete a group
        :param group_data: json object with group details, must contain group identifier
        :return: void
        """
        group_id = group_data['id'] if 'id' in group_data else None
        if not group_id:
            raise Exception("Couldn't find id for group")

        logging.info(f'trying to delete group {group_data.get("displayName")}')
        make_request(f'{GRAPH_URL}{RESOURCE_PATH}{group_id}', 'DELETE')
        logging.info(
            f'group {group_data.get("displayName")} disabled successfully')
 def post_object(self, object_data):
     """
     create task by posting the task_data to GRAPH
     make_request will handle errors
     """
     try:
         self.update_title(title=object_data["title"],
                           plan_id=object_data["planId"])
         make_request(f'{GRAPH_URL}/planner/tasks/', 'POST', object_data)
         logging.info(f'Created task with title {object_data.get("title")}')
         self.append_response("Ok")
         return True
     except:
         self.append_response("Error")
         return False
Beispiel #6
0
def generic_graph_api_request(path=None):
    """
    Generic Endpoint to call Microsoft Graph API.
    Any request in Microsoft Graph API can be issued here.
    :return: 200 for successful GET requests
             200 and malformed json for erronous GET requests
             code and response from graghapi for other cases
    """
    if r.args.get('auth') and r.args.get('auth') == 'user':
        init_dao_on_behalf_on(env('client_id'), env('client_secret'), env('tenant_id'), env('username'),
                              env('password'))
    else:
        init_dao(env('client_id'), env('client_secret'), env('tenant_id'))
    try:
        if r.method.lower() == 'get':
            response = Response(stream_as_json(get_all_objects(f'/{path}', params=r.args)), content_type=CT, status=200)
        else:
            url=f'{GRAPH_URL}/{path}'
            data=json.loads(r.data) if r.data else None

            response = Response(json.dumps(make_request(url=url, method=r.method, data=data)), content_type=CT, status=200)
    except HTTPError as error:
        logging.exception(error)
        response = Response(error.response.text, content_type=CT, status=error.response.status_code)

    return response
 def post_object(self, object_data):
     """
     create plan by posting the task_data to GRAPH
     make_request will handle errors
     """
     try:
         self.update_title(title=object_data["title"],
                           owner=object_data["owner"])
         print("make request")
         make_request(f'{GRAPH_URL}/planner/plans/', 'POST', object_data)
         logging.info(f'Created plan with title {object_data.get("title")}')
         self.append_response("Ok")
         return True
     except Exception as e:
         self.append_response("Error")
         print("error : ", e)
         return False
Beispiel #8
0
    def __try_delete(user_data: dict) -> None:
        """
        Internal function to 'delete' user (We will not actually perform delete operation but only
        disable user account by setting accountEnabled = false
        :param user_data: json object with user details, must contain user identifier
        (id or userPrincipalName property)
        :return: void
        """
        user_id = user_data['id'] if user_data.get(
            'id') else user_data['userPrincipalName']
        if not user_id:
            raise Exception(
                "Couldn't find id for user, at least id or userPrincipalName needed"
            )

        logging.info(f'trying to disable user {user_id}')
        make_request(f'{GRAPH_URL}{RESOURCE_PATH}{user_id}', 'PATCH',
                     {'accountEnabled': False})
        logging.info(f'user {user_id} disabled successfully')
Beispiel #9
0
    def __try_delete(user_data: dict, do_force_delete) -> None:
        """
        Internal function to disable/delete user
        If force_delete is on, delete user for good, otherwise disable user account by setting accountEnabled = false
        :param user_data: json object with user details, must contain user identifier
        (id or userPrincipalName property)
        :return: void
        """
        user_id = user_data['id'] if user_data.get(
            'id') else user_data['userPrincipalName']
        if not user_id:
            raise Exception(
                "Couldn't find id for user, at least id or userPrincipalName needed"
            )

        logging.info(f'trying to disable/delete user {user_id}')
        if do_force_delete:
            make_request(f'{GRAPH_URL}{RESOURCE_PATH}{user_id}', 'DELETE')
        else:
            make_request(f'{GRAPH_URL}{RESOURCE_PATH}{user_id}', 'PATCH',
                         {'accountEnabled': False})
        logging.info(f'user {user_id} disabled/deleted successfully')
Beispiel #10
0
    def __try_update(user_data: dict) -> None:
        """
        Internal function to update user
        Update user with passwordProfile is not possible without Directory.AccessAsUser.All
        which is not exist in "application" permission so we need to remove this part if exist
        :param user_data: json object with user details, must contain user identifier
        (id or userPrincipalName property)
        :return: void
        """
        user_id = user_data['id'] if user_data.get(
            'id') else user_data['userPrincipalName']

        if not user_id:
            raise Exception(
                "Couldn't find id for user, at least id or userPrincipalName needed"
            )
        # we can't and don't need to update user password when syncing user with Azure
        if user_data.get('passwordProfile'):
            del user_data['passwordProfile']

        logging.info(f'trying to update user {user_id}')
        make_request(f'{GRAPH_URL}{RESOURCE_PATH}{user_id}', 'PATCH',
                     user_data)
        logging.info(f'user {user_id} updated successfully')