diff --git a/.env b/.env index 3083da4..efb7143 100644 --- a/.env +++ b/.env @@ -9,3 +9,4 @@ OS_AUTH_URL=http://localhost:8080/auth/v1.0 OS_USERNAME=test:tester OS_PASSWORD=testing OS_AUTH_VERSION=1.0 +DIRECTORY_API_TOKEN=ADD_DIRECTORY_API_TOKEN_HERE diff --git a/webapp/routes.py b/webapp/routes.py index 3600e5d..b135779 100644 --- a/webapp/routes.py +++ b/webapp/routes.py @@ -30,6 +30,7 @@ get_tokens, update_asset, update_redirect, + get_users, ) @@ -170,3 +171,8 @@ def update(): view_func=delete_redirect, methods=["DELETE"], ) +api_blueprint.add_url_rule( + "/get-users/", + view_func=get_users, + methods=["GET"], +) diff --git a/webapp/views.py b/webapp/views.py index 1e38ae4..c8c16d9 100644 --- a/webapp/views.py +++ b/webapp/views.py @@ -3,9 +3,11 @@ import json import re import uuid +import requests from datetime import datetime from distutils.util import strtobool from urllib.parse import unquote, urlparse +from os import environ # Packages from flask import ( @@ -20,6 +22,7 @@ # Local from webapp.database import db_session from webapp.decorators import token_required +from webapp.sso import login_required from webapp.lib.file_helpers import get_mimetype, remove_filename_hash from webapp.lib.http_helpers import set_headers_for_type from webapp.lib.processors import ImageProcessor @@ -373,3 +376,37 @@ def delete_redirect(redirect_path): db_session.commit() return jsonify({}), 204 + + +@login_required +def get_users(username: str): + query = """ + query($name: String!) { + employees(filter: { contains: { name: $name }}) { + id + name + email + team + department + jobTitle + } + } + """ + + headers = {"Authorization": "token " + environ.get("DIRECTORY_API_TOKEN")} + response = requests.post( + "https://directory.wpe.internal/graphql/", + json={ + "query": query, + "variables": {"name": username.strip()}, + }, + headers=headers, + verify=False, + timeout=10, + ) + + if response.status_code == 200: + users = response.json().get("data", {}).get("employees", []) + return jsonify(list(users)) + else: + return jsonify({"error": "Failed to fetch users"}), 500