From dd4b7ad229454e9cd5807d9f87b8703d0a1c32bc Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Tue, 21 Jan 2025 13:21:47 +0100 Subject: [PATCH] views: add client search to scopes Add a view to search clients by IP or MAC. Adapt parse_scopes_from_tree() to include the path of the scope in the tree. --- ogcp/templates/actions/client_search.html | 88 +++++++++++++++++++++++ ogcp/templates/scopes.html | 2 + ogcp/views.py | 23 +++++- 3 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 ogcp/templates/actions/client_search.html diff --git a/ogcp/templates/actions/client_search.html b/ogcp/templates/actions/client_search.html new file mode 100644 index 0000000..bace57a --- /dev/null +++ b/ogcp/templates/actions/client_search.html @@ -0,0 +1,88 @@ +{% extends 'scopes.html' %} +{% import "bootstrap/wtf.html" as wtf %} +{% import "macros.html" as macros %} + +{% set btn_back = true %} + +{% block nav_client %} active {% endblock %} +{% block nav_client_search %} active {% endblock %} +{% block content %} + +

+ {{ _('Search clients') }} +

+ +
+ + + + + + + + + + +
+ +
+ + + + +{% endblock %} \ No newline at end of file diff --git a/ogcp/templates/scopes.html b/ogcp/templates/scopes.html index 31de77d..9393d8a 100644 --- a/ogcp/templates/scopes.html +++ b/ogcp/templates/scopes.html @@ -42,6 +42,8 @@ {% endif %} + {% endif %} diff --git a/ogcp/views.py b/ogcp/views.py index 694435f..13446f7 100644 --- a/ogcp/views.py +++ b/ogcp/views.py @@ -246,15 +246,17 @@ def get_all_repositories(): return data -def parse_scopes_from_tree(tree, scope_type): +def parse_scopes_from_tree(tree, scope_type, tree_path=''): scopes = [] for scope in tree['scope']: if scope['type'] == scope_type: if 'name' in tree: scope['parent'] = tree['name'] + scope['tree_path'] = tree_path scopes.append(scope) else: - scopes += parse_scopes_from_tree(scope, scope_type) + new_tree_path = f'{tree_path}{scope.get("name")}/' + scopes += parse_scopes_from_tree(scope, scope_type, new_tree_path) return scopes def add_state_and_ips(scope, clients, ips): @@ -1998,6 +2000,23 @@ def action_client_delete(): else: return redirect(url_for('scopes')) +@app.route('/action/client/search', methods=['GET']) +@handle_server_errors('scopes') +@login_required +def action_client_search(): + scopes, clients = get_scopes() + clients = parse_scopes_from_tree(scopes, 'computer') + for client in clients: + payload = {'client': [client['ip'][0]]} + info_response = multi_request('get', '/client/info', payload) + for res in info_response: + mac = res['json']['mac'].lower() + client['mac'] = mac + + return render_template('actions/client_search.html', + scopes=scopes, + clients=clients) + @app.route('/action/cmd/run', methods=['GET', 'POST']) @handle_server_errors('commands') @login_required