Compare commits

...

3 Commits

Author SHA1 Message Date
2ff7a515d5 Add ENABLED_LOGS config option 2024-01-19 13:48:23 +01:00
99c9781767 Add admin logs view 2024-01-19 13:38:05 +01:00
191ffebd7e Add logs server-side 2024-01-17 12:56:01 +01:00
16 changed files with 289 additions and 79 deletions

View File

@ -24,3 +24,6 @@ MAX_AGE=31
# Keep in mind that this config option can only be loaded from default_config.py,
# as the custom config is stored in $INSTANCE_PATH/
INSTANCE_PATH="instance"
# Events to log
ENABLED_LOGS=["NEW_GROUPE", "NEW_ALBUM", "NEW_PARTITION", "NEW_USER", "SERVER_RESTART", "FAILED_LOGIN"]

View File

@ -12,7 +12,7 @@ from flask import Flask, g, redirect, render_template, request, send_file, flash
from werkzeug.security import generate_password_hash
from .modules.utils import User, Album, get_all_albums
from .modules import albums, auth, partition, admin, groupe, thumbnails
from .modules import albums, auth, partition, admin, groupe, thumbnails, logging
from .modules.auth import admin_required, login_required
from .modules.db import get_db
@ -33,6 +33,11 @@ def load_config():
".",
os.path.join(app.instance_path, "config.py")
)
if spec is None:
print("[ERROR] Failed to load $INSTANCE_PATH/config.py")
sys.exit(1)
user_config = importlib.util.module_from_spec(spec)
spec.loader.exec_module(user_config)
@ -51,6 +56,18 @@ def load_config():
)
def setup_logging():
logging.log_file = os.path.join(app.instance_path, "logs.txt")
enabled = []
for event in app.config["ENABLED_LOGS"]:
try:
enabled.append(logging.LogEntry.from_string(event))
except KeyError:
print(f"[ERROR] There is an error in your config: Unknown event {event}")
logging.enabled = enabled
def get_version():
try:
result = subprocess.run(["git", "describe", "--tags"], stdout=subprocess.PIPE, check=True)
@ -61,6 +78,7 @@ def get_version():
load_config()
setup_logging()
app.register_blueprint(auth.bp)
app.register_blueprint(admin.bp)
@ -71,6 +89,8 @@ app.register_blueprint(thumbnails.bp)
__version__ = get_version()
logging.log([], logging.LogEntry.SERVER_RESTART)
@app.route("/")
def home():
@ -96,6 +116,12 @@ def add_user():
if error is None:
# Success, go to the login page.
user = User(name=username)
logging.log(
[user.username, user.id, True, current_user.username],
logging.LogEntry.NEW_USER
)
try:
if album_uuid != "":
user.join_album(album_uuid)

View File

@ -2,7 +2,8 @@
"""
Admin Panel
"""
from flask import Blueprint, render_template, session
import os
from flask import Blueprint, render_template, session, current_app, send_file
from .db import get_db
from .auth import admin_required
@ -35,3 +36,28 @@ def index():
users=users,
user=current_user
)
@bp.route("/logs")
@admin_required
def logs():
"""
Admin panel logs page
"""
user = User(user_id=session.get("user_id"))
return render_template(
"admin/logs.html",
user=user
)
@bp.route("/logs.txt")
@admin_required
def logs_txt():
"""
Admin panel logs page
"""
return send_file(
os.path.join(current_app.instance_path, "logs.txt")
)

View File

@ -13,7 +13,7 @@ from flask import (Blueprint, abort, flash, redirect, render_template,
from .auth import login_required
from .db import get_db
from .utils import User, Album
from . import search, utils
from . import search, utils, logging
bp = Blueprint("albums", __name__, url_prefix="/albums")
@ -116,6 +116,8 @@ def create_album_req():
db = get_db()
error = None
user = User(user_id=session["user_id"])
if not name or name.strip() == "":
error = "Un nom est requis. L'album n'a pas été créé"
@ -131,6 +133,8 @@ def create_album_req():
)
db.commit()
logging.log([album.name, album.uuid, user.username], logging.LogEntry.NEW_ALBUM)
if "response" in request.args and request.args["response"] == "json":
return {
"status": "ok",
@ -217,7 +221,7 @@ def delete_album(uuid):
@login_required
def add_partition(album_uuid):
"""
Ajouter une partition à un album (par upload)
Ajouter une partition à un album (nouveau fichier)
"""
T = TypeVar("T")
def get_opt_string(dictionary: dict[T, str], key: T):
@ -265,6 +269,7 @@ def add_partition(album_uuid):
author = get_opt_string(request.form, "author")
body = get_opt_string(request.form, "body")
partition_uuid: str
while True:
try:
partition_uuid = str(uuid4())
@ -307,6 +312,11 @@ def add_partition(album_uuid):
except db.IntegrityError:
pass
logging.log(
[request.form["name"], partition_uuid, user.username],
logging.LogEntry.NEW_PARTITION
)
if "response" in request.args and request.args["response"] == "json":
return {
"status": "ok",
@ -320,7 +330,7 @@ def add_partition(album_uuid):
@login_required
def add_partition_from_search():
"""
Ajout d'une partition (depuis la recherche)
Ajout d'une partition (depuis la recherche locale)
"""
user = User(user_id=session.get("user_id"))
error = None

View File

@ -12,6 +12,7 @@ from werkzeug.security import check_password_hash, generate_password_hash
from .db import get_db
from .utils import User
from . import logging
bp = Blueprint("auth", __name__, url_prefix="/auth")
@ -120,8 +121,15 @@ def register():
if error is not None:
flash(error)
else:
user = User(name=username)
flash("Utilisateur créé avec succès. Vous pouvez vous connecter.")
logging.log(
[user.username, user.id, False],
logging.LogEntry.NEW_USER
)
return render_template("auth/register.html")
@ -139,12 +147,16 @@ def login():
).fetchone()
if (user is None) or not check_password_hash(user["password"], password):
logging.log([username], logging.LogEntry.FAILED_LOGIN)
error = "Nom d'utilisateur ou mot de passe incorrect."
if error is None:
# store the user id in a new session and return to the index
session.clear()
session["user_id"] = user["id"]
logging.log([username], logging.LogEntry.LOGIN)
return redirect(url_for("albums.index"))
flash(error)

View File

@ -9,6 +9,7 @@ from .auth import login_required
from .db import get_db
from .utils import User, Album, Groupe
from . import utils
from . import logging
bp = Blueprint("groupe", __name__, url_prefix="/groupe")
@ -63,6 +64,8 @@ def create_groupe():
db = get_db()
error = None
user = User(user_id=session["user_id"])
if not name or name.strip() == "":
error = "Un nom est requis. Le groupe n'a pas été créé"
@ -93,6 +96,8 @@ def create_groupe():
except db.IntegrityError:
pass
logging.log([name, uuid, user.username], logging.LogEntry.NEW_GROUPE)
if "response" in request.args and request.args["response"] == "json":
return {
"status": "ok",
@ -194,6 +199,8 @@ def create_album_req(groupe_uuid):
)
db.commit()
logging.log([album.name, album.uuid, user.username], logging.LogEntry.NEW_ALBUM)
if "response" in request.args and request.args["response"] == "json":
return {
"status": "ok",

View File

@ -0,0 +1,74 @@
from datetime import datetime
from typing import Union
from enum import Enum
global log_file
global enabled
class LogEntry(Enum):
LOGIN = 1
NEW_GROUPE = 2
NEW_ALBUM = 3
NEW_PARTITION = 4
NEW_USER = 5
SERVER_RESTART = 6
FAILED_LOGIN = 7
def from_string(entry: str):
mapping = {
"LOGIN": LogEntry.LOGIN,
"NEW_GROUPE": LogEntry.NEW_GROUPE,
"NEW_ALBUM": LogEntry.NEW_ALBUM,
"NEW_PARTITION": LogEntry.NEW_PARTITION,
"NEW_USER": LogEntry.NEW_USER,
"SERVER_RESTART": LogEntry.SERVER_RESTART,
"FAILED_LOGIN": LogEntry.FAILED_LOGIN
}
# Will return KeyError if not available
return mapping[entry]
def add_entry(entry: str) -> None:
date = datetime.now().strftime("%y-%b-%Y %H:%M:%S")
with open(log_file, 'a', encoding="utf8") as f:
f.write(f"[{date}] {entry}\n")
def log(content: list[Union[str, bool, int]], log_type: LogEntry) -> None:
description: str = ""
if log_type not in enabled:
return
match log_type:
case LogEntry.LOGIN: # content = (user.name)
description = f"Successful login for {content[0]}"
case LogEntry.NEW_GROUPE: # content = (groupe.name, groupe.id, user.name)
description = f"{content[2]} added groupe '{content[0]}' ({content[1]})"
case LogEntry.NEW_ALBUM: # content = (album.name, album.id, user.name)
description = f"{content[2]} added album '{content[0]}' ({content[1]})"
case LogEntry.NEW_PARTITION: # content = (partition.name, partition.uuid, user.name)
description = f"{content[2]} added partition '{content[0]}' ({content[1]})"
case LogEntry.NEW_USER: # content = (user.name, user.id, from_register_page, admin.name if relevant)
if not content[2]:
description = f"New user {content[0]}[{content[1]}]"
else:
description = f"New user {content[0]}[{content[1]}] added by {content[3]}"
case LogEntry.SERVER_RESTART: # content = ()
description = "Server just restarted"
case LogEntry.FAILED_LOGIN: # content = (user.name)
description = f"Failed login for {content[0]}"
add_entry(description)
log_file = "logs.txt"
enabled = [i for i in LogEntry]

View File

@ -0,0 +1,20 @@
let logsEmbed = document.getElementById("logs-embed");
logsEmbed.addEventListener("load", () => {
var cssLink = document.createElement("link");
cssLink.href = "/static/style/logs.css";
cssLink.rel = "stylesheet";
cssLink.type = "text/css";
// add css
logsEmbed.contentDocument.head.appendChild(cssLink);
// Scroll to bottom
logsEmbed.contentWindow.scrollTo(0, logsEmbed.contentDocument.body.scrollHeight);
});
// check if the iframe is already loaded (happened with FF Android)
if (logsEmbed.contentDocument.readyState == "complete") {
logsEmbed.dispatchEvent(new Event("load"));
}

View File

@ -0,0 +1,65 @@
/** Color Schemes */
/* Themes used: Catppuccin Latte & Moccha
* https://github.com/catppuccin/catppuccin */
/* Dark theme: Catpuccin Mocha */
:root {
--color-rosewater: #f5e0dc;
--color-flamingo: #f2cdcd;
--color-pink: #f5c2e7;
--color-mauve: #cba6f7;
--color-red: #f38ba8;
--color-maroon: #eba0ac;
--color-peach: #fab387;
--color-yellow: #f9e2af;
--color-green: #a6e3a1;
--color-teal: #94e2d5;
--color-sky: #89dceb;
--color-sapphire: #74c7ec;
--color-blue: #89b4fa;
--color-lavender: #b4befe;
--color-text: #cdd6f4;
--color-subtext1: #bac2de;
--color-subtext0: #a6adc8;
--color-overlay2: #9399b2;
--color-overlay1: #7f849c;
--color-overlay0: #6c7086;
--color-surface2: #585b70;
--color-surface1: #45475a;
--color-surface0: #313244;
--color-base: #1e1e2e;
--color-mantle: #181825;
--color-crust: #11111b;
}
/* Light theme: Catppuccin Latte */
@media (prefers-color-scheme: light) {
:root {
--color-rosewater: #dc8a78;
--color-flamingo: #dd7878;
--color-pink: #ea76cb;
--color-mauve: #8839ef;
--color-red: #d20f39;
--color-maroon: #e64553;
--color-peach: #fe640b;
--color-yellow: #df8e1d;
--color-green: #40a02b;
--color-teal: #179299;
--color-sky: #04a5e5;
--color-sapphire: #209fb5;
--color-blue: #1e66f5;
--color-lavender: #7287fd;
--color-text: #4c4f69;
--color-subtext1: #5c5f77;
--color-subtext0: #6c6f85;
--color-overlay2: #7c7f93;
--color-overlay1: #8c8fa1;
--color-overlay0: #9ca0b0;
--color-surface2: #acb0be;
--color-surface1: #bcc0cc;
--color-surface0: #ccd0da;
--color-base: #eff1f5;
--color-mantle: #e6e9ef;
--color-crust: #dce0e8;
}
}

View File

@ -0,0 +1,10 @@
@import url('/static/style/colors.css');
body {
background-color: var(--color-crust);
color: var(--color-text);
}
pre {
white-space: pre;
}

View File

@ -1,75 +1,7 @@
@import url('/static/style/colors.css');
/** @import url('https://www.augustin64.fr/static/font/iosevka.css'); */
/** Color Schemes */
/* Themes used: Catppuccin Latte & Moccha
* https://github.com/catppuccin/catppuccin */
/* Dark theme: Catpuccin Mocha */
:root {
--color-rosewater: #f5e0dc;
--color-flamingo: #f2cdcd;
--color-pink: #f5c2e7;
--color-mauve: #cba6f7;
--color-red: #f38ba8;
--color-maroon: #eba0ac;
--color-peach: #fab387;
--color-yellow: #f9e2af;
--color-green: #a6e3a1;
--color-teal: #94e2d5;
--color-sky: #89dceb;
--color-sapphire: #74c7ec;
--color-blue: #89b4fa;
--color-lavender: #b4befe;
--color-text: #cdd6f4;
--color-subtext1: #bac2de;
--color-subtext0: #a6adc8;
--color-overlay2: #9399b2;
--color-overlay1: #7f849c;
--color-overlay0: #6c7086;
--color-surface2: #585b70;
--color-surface1: #45475a;
--color-surface0: #313244;
--color-base: #1e1e2e;
--color-mantle: #181825;
--color-crust: #11111b;
/* --font-family: Iosevka Web; /* Specify the font here */
}
/* Light theme: Catppuccin Latte */
@media (prefers-color-scheme: light) {
:root {
--color-rosewater: #dc8a78;
--color-flamingo: #dd7878;
--color-pink: #ea76cb;
--color-mauve: #8839ef;
--color-red: #d20f39;
--color-maroon: #e64553;
--color-peach: #fe640b;
--color-yellow: #df8e1d;
--color-green: #40a02b;
--color-teal: #179299;
--color-sky: #04a5e5;
--color-sapphire: #209fb5;
--color-blue: #1e66f5;
--color-lavender: #7287fd;
--color-text: #4c4f69;
--color-subtext1: #5c5f77;
--color-subtext0: #6c6f85;
--color-overlay2: #7c7f93;
--color-overlay1: #8c8fa1;
--color-overlay0: #9ca0b0;
--color-surface2: #acb0be;
--color-surface1: #bcc0cc;
--color-surface0: #ccd0da;
--color-base: #eff1f5;
--color-mantle: #e6e9ef;
--color-crust: #dce0e8;
}
}
/** Various settings (variables) */
:root {
--sidebar-size: max(10vw, 160px);
@ -773,3 +705,12 @@ midi-player {
margin-bottom: 100px;
margin-top: 20px;
}
#logs-embed {
margin: auto;
height: 80vh;
width: 95%;
padding: 5px;
border-radius: 5px;
background-color: var(--color-crust);
}

View File

@ -6,10 +6,13 @@
<div id="actions-rapides">
<a href="/add-user">
<div class="button">Ajouter un utilisateur</div>
<div class="button">Nouvel utilisateur</div>
</a>
<a href="/partition">
<div class="button">Voir toutes les partitions</div>
<div class="button">Voir les partitions</div>
</a>
<a href="/admin/logs">
<div class="button">Voir les logs</div>
</a>
</div>
<div class="x-scrollable">

View File

@ -0,0 +1,10 @@
{% set scripts=["scripts/logs.js"] %}
{% extends 'base.html' %}
{% block header %}
<h1>{% block title %}Logs{% endblock %}</h1>
{% endblock %}
{% block content %}
<iframe type="text/plain" id="logs-embed" src="/admin/logs.txt" frameborder="0" width="100%" height="100%"></iframe>
{% endblock %}

View File

@ -5,8 +5,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>{% block title %}{% endblock %} - PartitionCloud</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='mobile.css') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='style/style.css') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='style/mobile.css') }}">
<link rel="icon" type="image/png" href="{{ url_for('static', filename='icons/512.png') }}" />
<link rel="apple-touch-icon" href="{{ url_for('static', filename='icons/512.png') }}">
<link rel="manifest" href="{{ url_for('static', filename='manifest.webmanifest') }}" />
@ -185,5 +185,8 @@
<div id="footer"><a href="https://github.com/partitioncloud/partitioncloud-server">PartitionCloud</a> {{ version }}</div>
</div>
</body>
<script src="{{ url_for('static', filename='main.js') }}"></script>
<script src="{{ url_for('static', filename='scripts/main.js') }}"></script>
{% for script in scripts %}
<script src="{{ url_for('static', filename=script) }}"></script>
{% endfor %}
</html>