From 7747b7844abb401b26c8e4d60525a6f1d33c0fb0 Mon Sep 17 00:00:00 2001 From: Aevann Date: Tue, 6 Feb 2024 03:19:38 +0200 Subject: [PATCH] add ip templates --- files/classes/ip_logs.py | 3 +++ files/helpers/config/const.py | 1 + files/routes/admin.py | 18 +++++++++++++++ files/templates/admin/ip_users.html | 28 ++++++++++++++++++++++++ files/templates/admin/user_ips.html | 24 ++++++++++++++++++++ files/templates/userpage/admintools.html | 4 ++++ 6 files changed, 78 insertions(+) create mode 100644 files/templates/admin/ip_users.html create mode 100644 files/templates/admin/user_ips.html diff --git a/files/classes/ip_logs.py b/files/classes/ip_logs.py index 09ea519fe..3d6d3db37 100644 --- a/files/classes/ip_logs.py +++ b/files/classes/ip_logs.py @@ -1,6 +1,7 @@ import time from sqlalchemy import Column, ForeignKey +from sqlalchemy.orm import relationship from sqlalchemy.sql.sqltypes import * from files.classes import Base @@ -12,6 +13,8 @@ class IPLog(Base): created_utc = Column(Integer) last_used = Column(Integer) + user = relationship("User") + def __init__(self, *args, **kwargs): if "created_utc" not in kwargs: kwargs["created_utc"] = int(time.time()) diff --git a/files/helpers/config/const.py b/files/helpers/config/const.py index c4606c089..a70dca07c 100644 --- a/files/helpers/config/const.py +++ b/files/helpers/config/const.py @@ -205,6 +205,7 @@ PERMS = { # Minimum admin_level to perform action. 'MODERATE_PENDING_SUBMITTED_ASSETS': 3, 'UPDATE_ASSETS': 3, 'CHANGE_UNDER_SIEGE': 3, + 'VIEW_IPS': 3, 'PROGSTACK': 4, 'UNDO_AWARD_PINS': 4, diff --git a/files/routes/admin.py b/files/routes/admin.py index 2a9d73b61..676024046 100644 --- a/files/routes/admin.py +++ b/files/routes/admin.py @@ -2156,3 +2156,21 @@ def change_under_siege_post(v): g.db.add(ma) return {"message": "Thresholds changed successfully!"} + +if FEATURES['IP_LOGGING']: + @app.get("/@/ips/") + @limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400) + @limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400, key_func=get_ID) + @admin_level_required(PERMS['VIEW_IPS']) + def view_user_ips(v, username): + u = get_user(username, v=v) + ip_logs = g.db.query(IPLog).filter_by(user_id=u.id).order_by(IPLog.last_used.desc()) + return render_template('admin/user_ips.html', v=v, u=u, ip_logs=ip_logs) + + @app.get("/ip_users/") + @limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400) + @limiter.limit(DEFAULT_RATELIMIT, deduct_when=lambda response: response.status_code < 400, key_func=get_ID) + @admin_level_required(PERMS['VIEW_IPS']) + def view_ip_users(v, ip): + ip_logs = g.db.query(IPLog).filter_by(ip=ip).order_by(IPLog.last_used.desc()) + return render_template('admin/ip_users.html', v=v, ip=ip, ip_logs=ip_logs) diff --git a/files/templates/admin/ip_users.html b/files/templates/admin/ip_users.html new file mode 100644 index 000000000..8a64c6f25 --- /dev/null +++ b/files/templates/admin/ip_users.html @@ -0,0 +1,28 @@ +{% extends "default.html" %} +{% block pagetitle %}{{ip}} Users{% endblock %} + +{% block content %} +
Users who visited from {{ip}}
+
+ + + + + + + + + {% for ip_log in ip_logs %} + + + + + + {% endfor %} +
NameFirst UsedLast Used
+ {% with user=ip_log.user %} + {% include "user_in_table.html" %} + {% endwith %} +
+
+{% endblock %} diff --git a/files/templates/admin/user_ips.html b/files/templates/admin/user_ips.html new file mode 100644 index 000000000..fd01ef089 --- /dev/null +++ b/files/templates/admin/user_ips.html @@ -0,0 +1,24 @@ +{% extends "default.html" %} +{% block pagetitle %}@{{u.username}}'s IPs{% endblock %} + +{% block content %} +
@{{u.username}}'s IPs
+
+ + + + + + + + + {% for ip_log in ip_logs %} + + + + + + {% endfor %} +
IPFirst UsedLast Used
{{ip_log.ip}}
+
+{% endblock %} diff --git a/files/templates/userpage/admintools.html b/files/templates/userpage/admintools.html index 5b4ebbdf7..fcd4999c5 100644 --- a/files/templates/userpage/admintools.html +++ b/files/templates/userpage/admintools.html @@ -127,6 +127,10 @@ {% if v.admin_level >= PERMS['USER_RESET_PASSWORD'] %} {% endif %} + + {% if FEATURES['IP_LOGGING'] and v.admin_level >= PERMS['VIEW_IPS'] %} + View User IPs + {% endif %} {% endif %}