fixed auth, added more endpoints and config saving
This commit is contained in:
@ -4,18 +4,18 @@ import psycopg2
|
||||
from loguru import logger
|
||||
|
||||
from db.models import database
|
||||
from settings import settings
|
||||
from settings import startup_settings
|
||||
|
||||
|
||||
def connect_db():
|
||||
logger.info("Initializing DB connection")
|
||||
try:
|
||||
database.conn = psycopg2.connect(
|
||||
dbname=settings.db_name,
|
||||
user=settings.db_user,
|
||||
password=settings.db_password,
|
||||
host=settings.db_host,
|
||||
port=settings.db_port,
|
||||
dbname=startup_settings.db_name,
|
||||
user=startup_settings.db_user,
|
||||
password=startup_settings.db_password,
|
||||
host=startup_settings.db_host,
|
||||
port=startup_settings.db_port,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to initialize DB connection: {e}")
|
||||
|
||||
@ -6,16 +6,17 @@ from psycopg2._psycopg import connection
|
||||
def create_user(
|
||||
conn: connection,
|
||||
username: str,
|
||||
password: str
|
||||
password: str,
|
||||
role: str = "user"
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
insert into picrinth.users
|
||||
(username, password, disabled, created_at)
|
||||
values (%s, %s, false, now())
|
||||
(username, password, role, disabled, created_at)
|
||||
values (%s, %s, %s, false, now())
|
||||
""",
|
||||
(username, password),
|
||||
(username, password, role),
|
||||
)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
@ -74,6 +75,22 @@ def check_user_disabled(
|
||||
|
||||
# user updates
|
||||
|
||||
def update_user_username(
|
||||
conn: connection,
|
||||
username: str,
|
||||
newUsername: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
update picrinth.users
|
||||
set username = %s
|
||||
where username = %s;
|
||||
""",
|
||||
(newUsername, username),
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
def update_user_password(
|
||||
conn: connection,
|
||||
username: str,
|
||||
@ -91,22 +108,24 @@ def update_user_password(
|
||||
conn.commit()
|
||||
|
||||
|
||||
def update_user_username(
|
||||
def update_user_disabled(
|
||||
conn: connection,
|
||||
username: str,
|
||||
newUsername: str
|
||||
disabled: bool
|
||||
):
|
||||
# if disabled = True -> user is disabled
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
update picrinth.users
|
||||
set username = %s
|
||||
where username = %s;
|
||||
set disabled = %s
|
||||
where username = %s
|
||||
""",
|
||||
(newUsername, username),
|
||||
(disabled, username),
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
|
||||
def update_user_last_seen(
|
||||
conn: connection,
|
||||
username: str
|
||||
@ -132,8 +151,9 @@ def get_user(
|
||||
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select username, password, disabled,
|
||||
groups_ids, last_seen_at, created_at
|
||||
select username, password, role,
|
||||
disabled, groups_ids,
|
||||
last_seen_at, created_at
|
||||
from picrinth.users
|
||||
where username = %s
|
||||
""",
|
||||
@ -154,4 +174,4 @@ def get_user_password(
|
||||
""",
|
||||
(username,),
|
||||
)
|
||||
return cur.fetchone()
|
||||
return cur.fetchone()[0] # type: ignore
|
||||
|
||||
Reference in New Issue
Block a user