Skip to content

Commit

Permalink
Added OData expressions to AD users endpoint
Browse files Browse the repository at this point in the history
Also renamed it to better explain what it's pulling, given there are various sharepoint-specific users endpoints in play as well.
  • Loading branch information
JMaynor committed Sep 12, 2024
1 parent 8e049b8 commit a8ab42a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
23 changes: 20 additions & 3 deletions grafap/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@


@Decorators.refresh_graph_token
def get_users() -> dict:
def get_ad_users(select: str = None, filter: str = None, expand: str = None) -> dict:
"""
Gets all users in a given tenant
Gets AD users in a given tenant
:param select: OData $select query option
:param filter: OData $filter query option
:param expand: OData $expand query option
"""
if "GRAPH_BASE_URL" not in os.environ:
raise Exception("Error, could not find GRAPH_BASE_URL in env")
Expand Down Expand Up @@ -44,8 +48,21 @@ def recurs_get(url, headers):
else:
return data["value"]

# Construct the query string
query_params = []
if select:
query_params.append(f"$select={select}")
if filter:
query_params.append(f"$filter={filter}")
if expand:
query_params.append(f"$expand={expand}")

query_string = "&".join(query_params)
base_url = "https://graph.microsoft.com/v1.0/users"
url = f"{base_url}?{query_string}" if query_string else base_url

result = recurs_get(
"https://graph.microsoft.com/v1.0/" + "users",
url,
headers={"Authorization": "Bearer " + os.environ["GRAPH_BEARER_TOKEN"]},
)

Expand Down
10 changes: 10 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,13 @@
# Write first attachment data to a file
with open(attachments[0]["name"], "wb") as f:
f.write(attachments[0]["data"])

# AD Users

users = grafap.get_ad_users(
select="id,userPrincipalName,givenName,surname,displayName,department,businessPhones,employeeOrgData,employeeId",
filter="mail eq 'example@domain.com'",
expand="manager($select=id,userPrincipalName,givenName,surname,displayName,department,businessPhones,employeeOrgData,employeeId,manager)",
)

pass

0 comments on commit a8ab42a

Please sign in to comment.