users table endpoints. auth to fix
This commit is contained in:
157
src/db/users.py
Normal file
157
src/db/users.py
Normal file
@ -0,0 +1,157 @@
|
||||
import psycopg2.extras
|
||||
from psycopg2._psycopg import connection
|
||||
|
||||
# user create and delete
|
||||
|
||||
def create_user(
|
||||
conn: connection,
|
||||
username: str,
|
||||
password: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
insert into picrinth.users
|
||||
(username, password, disabled, created_at)
|
||||
values (%s, %s, false, now())
|
||||
""",
|
||||
(username, password),
|
||||
)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
|
||||
|
||||
def delete_user(
|
||||
conn: connection,
|
||||
username: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
delete from picrinth.users
|
||||
where username = %s
|
||||
""",
|
||||
(username,),
|
||||
)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
|
||||
|
||||
# user checks
|
||||
|
||||
def check_user_existence(
|
||||
conn: connection,
|
||||
username: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select exists(
|
||||
select 1
|
||||
from picrinth.users
|
||||
where username = %s
|
||||
);
|
||||
""",
|
||||
(username,),
|
||||
)
|
||||
return cur.fetchone()
|
||||
|
||||
def check_user_disabled(
|
||||
conn: connection,
|
||||
username: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select disabled
|
||||
from picrinth.users
|
||||
where username = %s;
|
||||
""",
|
||||
(username,),
|
||||
)
|
||||
return cur.fetchone()
|
||||
|
||||
|
||||
# user updates
|
||||
|
||||
def update_user_password(
|
||||
conn: connection,
|
||||
username: str,
|
||||
password: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
update picrinth.users
|
||||
set password = %s
|
||||
where username = %s
|
||||
""",
|
||||
(password, username),
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
|
||||
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_last_seen(
|
||||
conn: connection,
|
||||
username: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
update picrinth.users
|
||||
set last_seen_at = now()
|
||||
where username = %s
|
||||
""",
|
||||
(username,),
|
||||
)
|
||||
conn.commit()
|
||||
|
||||
|
||||
# user receiving
|
||||
|
||||
def get_user(
|
||||
conn: connection,
|
||||
username: str
|
||||
):
|
||||
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select username, password, disabled,
|
||||
groups_ids, last_seen_at, created_at
|
||||
from picrinth.users
|
||||
where username = %s
|
||||
""",
|
||||
(username,),
|
||||
)
|
||||
return cur.fetchone()
|
||||
|
||||
def get_user_password(
|
||||
conn: connection,
|
||||
username: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select password
|
||||
from picrinth.users
|
||||
where username = %s
|
||||
""",
|
||||
(username,),
|
||||
)
|
||||
return cur.fetchone()
|
||||
Reference in New Issue
Block a user