user management fix
This commit is contained in:
+2
-386
@@ -1,6 +1,5 @@
|
||||
import sys
|
||||
import pwd
|
||||
import sqlite3
|
||||
|
||||
from PySide6.QtCore import Qt, QTime
|
||||
from PySide6.QtWidgets import (
|
||||
@@ -26,389 +25,7 @@ from PySide6.QtWidgets import (
|
||||
QWidget,
|
||||
)
|
||||
|
||||
from app.database import DATABASE_PATH
|
||||
|
||||
|
||||
# ============================================================
|
||||
# Database
|
||||
# ============================================================
|
||||
|
||||
class Database:
|
||||
|
||||
@staticmethod
|
||||
def get_connection():
|
||||
connection = sqlite3.connect(DATABASE_PATH)
|
||||
connection.row_factory = sqlite3.Row
|
||||
connection.execute("PRAGMA foreign_keys = ON")
|
||||
return connection
|
||||
|
||||
@staticmethod
|
||||
def get_users():
|
||||
if not DATABASE_PATH.exists():
|
||||
return []
|
||||
|
||||
connection = Database.get_connection()
|
||||
|
||||
try:
|
||||
return connection.execute(
|
||||
"""
|
||||
SELECT *
|
||||
FROM users
|
||||
ORDER BY username COLLATE NOCASE
|
||||
"""
|
||||
).fetchall()
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
@staticmethod
|
||||
def get_user(user_id):
|
||||
connection = Database.get_connection()
|
||||
|
||||
try:
|
||||
return connection.execute(
|
||||
"""
|
||||
SELECT *
|
||||
FROM users
|
||||
WHERE id = ?
|
||||
""",
|
||||
(user_id,),
|
||||
).fetchone()
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
@staticmethod
|
||||
def get_remaining_time(user_id):
|
||||
if not DATABASE_PATH.exists():
|
||||
return 0, 0
|
||||
|
||||
connection = Database.get_connection()
|
||||
|
||||
try:
|
||||
allowance_row = connection.execute(
|
||||
"""
|
||||
SELECT COALESCE(
|
||||
allowance_seconds,
|
||||
0
|
||||
) AS total
|
||||
FROM daily_allowances
|
||||
WHERE user_id = ?
|
||||
AND weekday = CAST(
|
||||
strftime('%w', 'now', 'localtime')
|
||||
AS INTEGER
|
||||
)
|
||||
""",
|
||||
(user_id,),
|
||||
).fetchone()
|
||||
|
||||
allowance = (
|
||||
allowance_row["total"]
|
||||
if allowance_row
|
||||
else 0
|
||||
)
|
||||
|
||||
usage_row = connection.execute(
|
||||
"""
|
||||
SELECT COALESCE(
|
||||
used_seconds,
|
||||
0
|
||||
) AS total
|
||||
FROM usage
|
||||
WHERE user_id = ?
|
||||
AND date = date('now', 'localtime')
|
||||
""",
|
||||
(user_id,),
|
||||
).fetchone()
|
||||
|
||||
used = (
|
||||
usage_row["total"]
|
||||
if usage_row
|
||||
else 0
|
||||
)
|
||||
|
||||
remaining = max(
|
||||
allowance - used,
|
||||
0,
|
||||
)
|
||||
|
||||
return remaining, allowance
|
||||
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
@staticmethod
|
||||
def get_allowances(user_id):
|
||||
connection = Database.get_connection()
|
||||
|
||||
try:
|
||||
rows = connection.execute(
|
||||
"""
|
||||
SELECT weekday, allowance_seconds
|
||||
FROM daily_allowances
|
||||
WHERE user_id = ?
|
||||
""",
|
||||
(user_id,),
|
||||
).fetchall()
|
||||
|
||||
return {
|
||||
row["weekday"]: row["allowance_seconds"]
|
||||
for row in rows
|
||||
}
|
||||
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
@staticmethod
|
||||
def get_access_windows(user_id):
|
||||
connection = Database.get_connection()
|
||||
|
||||
try:
|
||||
rows = connection.execute(
|
||||
"""
|
||||
SELECT
|
||||
weekday,
|
||||
start_minute,
|
||||
end_minute
|
||||
FROM access_windows
|
||||
WHERE user_id = ?
|
||||
ORDER BY weekday, start_minute
|
||||
""",
|
||||
(user_id,),
|
||||
).fetchall()
|
||||
|
||||
result = {}
|
||||
|
||||
for row in rows:
|
||||
result.setdefault(
|
||||
row["weekday"],
|
||||
[],
|
||||
).append(
|
||||
(
|
||||
row["start_minute"],
|
||||
row["end_minute"],
|
||||
)
|
||||
)
|
||||
|
||||
return result
|
||||
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
@staticmethod
|
||||
def create_user(
|
||||
username,
|
||||
enabled,
|
||||
allowances,
|
||||
access_windows,
|
||||
):
|
||||
connection = Database.get_connection()
|
||||
|
||||
try:
|
||||
cursor = connection.execute(
|
||||
"""
|
||||
INSERT INTO users (
|
||||
username,
|
||||
enabled
|
||||
)
|
||||
VALUES (?, ?)
|
||||
""",
|
||||
(
|
||||
username,
|
||||
1 if enabled else 0,
|
||||
),
|
||||
)
|
||||
|
||||
user_id = cursor.lastrowid
|
||||
|
||||
for weekday, seconds in allowances.items():
|
||||
connection.execute(
|
||||
"""
|
||||
INSERT INTO daily_allowances (
|
||||
user_id,
|
||||
weekday,
|
||||
allowance_seconds
|
||||
)
|
||||
VALUES (?, ?, ?)
|
||||
""",
|
||||
(
|
||||
user_id,
|
||||
weekday,
|
||||
seconds,
|
||||
),
|
||||
)
|
||||
|
||||
for weekday, windows in access_windows.items():
|
||||
for start_minute, end_minute in windows:
|
||||
connection.execute(
|
||||
"""
|
||||
INSERT INTO access_windows (
|
||||
user_id,
|
||||
weekday,
|
||||
start_minute,
|
||||
end_minute
|
||||
)
|
||||
VALUES (?, ?, ?, ?)
|
||||
""",
|
||||
(
|
||||
user_id,
|
||||
weekday,
|
||||
start_minute,
|
||||
end_minute,
|
||||
),
|
||||
)
|
||||
|
||||
connection.commit()
|
||||
|
||||
return user_id
|
||||
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
@staticmethod
|
||||
def update_user(
|
||||
user_id,
|
||||
enabled,
|
||||
allowances,
|
||||
access_windows,
|
||||
):
|
||||
connection = Database.get_connection()
|
||||
|
||||
try:
|
||||
connection.execute(
|
||||
"""
|
||||
UPDATE users
|
||||
SET enabled = ?
|
||||
WHERE id = ?
|
||||
""",
|
||||
(
|
||||
1 if enabled else 0,
|
||||
user_id,
|
||||
),
|
||||
)
|
||||
|
||||
for weekday, seconds in allowances.items():
|
||||
connection.execute(
|
||||
"""
|
||||
INSERT INTO daily_allowances (
|
||||
user_id,
|
||||
weekday,
|
||||
allowance_seconds
|
||||
)
|
||||
VALUES (?, ?, ?)
|
||||
ON CONFLICT(user_id, weekday)
|
||||
DO UPDATE SET
|
||||
allowance_seconds =
|
||||
excluded.allowance_seconds
|
||||
""",
|
||||
(
|
||||
user_id,
|
||||
weekday,
|
||||
seconds,
|
||||
),
|
||||
)
|
||||
|
||||
# Replace access windows completely.
|
||||
connection.execute(
|
||||
"""
|
||||
DELETE FROM access_windows
|
||||
WHERE user_id = ?
|
||||
""",
|
||||
(user_id,),
|
||||
)
|
||||
|
||||
for weekday, windows in access_windows.items():
|
||||
for start_minute, end_minute in windows:
|
||||
connection.execute(
|
||||
"""
|
||||
INSERT INTO access_windows (
|
||||
user_id,
|
||||
weekday,
|
||||
start_minute,
|
||||
end_minute
|
||||
)
|
||||
VALUES (?, ?, ?, ?)
|
||||
""",
|
||||
(
|
||||
user_id,
|
||||
weekday,
|
||||
start_minute,
|
||||
end_minute,
|
||||
),
|
||||
)
|
||||
|
||||
connection.commit()
|
||||
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
@staticmethod
|
||||
def delete_user(user_id):
|
||||
connection = Database.get_connection()
|
||||
|
||||
try:
|
||||
connection.execute(
|
||||
"""
|
||||
DELETE FROM users
|
||||
WHERE id = ?
|
||||
""",
|
||||
(user_id,),
|
||||
)
|
||||
|
||||
connection.commit()
|
||||
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
@staticmethod
|
||||
def set_enabled(user_id, enabled):
|
||||
connection = Database.get_connection()
|
||||
|
||||
try:
|
||||
connection.execute(
|
||||
"""
|
||||
UPDATE users
|
||||
SET enabled = ?
|
||||
WHERE id = ?
|
||||
""",
|
||||
(
|
||||
1 if enabled else 0,
|
||||
user_id,
|
||||
),
|
||||
)
|
||||
|
||||
connection.commit()
|
||||
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
@staticmethod
|
||||
def add_temporary_time(
|
||||
user_id,
|
||||
seconds,
|
||||
):
|
||||
connection = Database.get_connection()
|
||||
|
||||
try:
|
||||
connection.execute(
|
||||
"""
|
||||
INSERT INTO temporary_grants (
|
||||
user_id,
|
||||
seconds,
|
||||
remaining_seconds
|
||||
)
|
||||
VALUES (?, ?, ?)
|
||||
""",
|
||||
(
|
||||
user_id,
|
||||
seconds,
|
||||
seconds,
|
||||
),
|
||||
)
|
||||
|
||||
connection.commit()
|
||||
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
from .client import Database, DatabaseError
|
||||
|
||||
# ============================================================
|
||||
# Linux users
|
||||
@@ -421,7 +38,6 @@ def linux_user_exists(username):
|
||||
except KeyError:
|
||||
return False
|
||||
|
||||
|
||||
# ============================================================
|
||||
# Helpers
|
||||
# ============================================================
|
||||
@@ -1464,7 +1080,7 @@ class UserDialog(QDialog):
|
||||
access_windows,
|
||||
)
|
||||
|
||||
except sqlite3.IntegrityError:
|
||||
except DatabaseError:
|
||||
|
||||
QMessageBox.warning(
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user