What is a lot of fucking work!
This commit is contained in:
parent
85c07ed4f3
commit
3414b5c334
8 changed files with 586 additions and 216 deletions
56
dba.py
56
dba.py
|
|
@ -9,17 +9,21 @@ logger = logging.getLogger(__name__)
|
|||
|
||||
def bootstrapDB(conn: DBConnector):
|
||||
if not conn.tableExists(META_INFO_TABLE_NAME):
|
||||
logger.info("Creating meta info table")
|
||||
conn.createTable(
|
||||
META_INFO_TABLE_NAME, META_INFO_TABLE_SCHEMA, system=True, hidden=True
|
||||
)
|
||||
|
||||
if not conn.tableExists(USER_GROUP_TABLE_NAME):
|
||||
logger.info("Creating user group table")
|
||||
conn.createTable(USER_GROUP_TABLE_NAME, USER_GROUP_TABLE_SCHEMA, system=True)
|
||||
|
||||
if not conn.tableExists(USERS_TABLE_NAME):
|
||||
logger.info("Creating users table")
|
||||
conn.createTable(USERS_TABLE_NAME, USERS_TABLE_SCHEMA, system=True)
|
||||
|
||||
if not conn.tableExists(USER_IN_USER_GROUP_JOIN_TABLE_NAME):
|
||||
logger.info("Creating user in user group join table")
|
||||
conn.createTable(
|
||||
USER_IN_USER_GROUP_JOIN_TABLE_NAME,
|
||||
USER_IN_USER_GROUP_JOIN_TABLE_SCHEMA,
|
||||
|
|
@ -27,6 +31,7 @@ def bootstrapDB(conn: DBConnector):
|
|||
)
|
||||
|
||||
if not conn.tableExists(TABLE_ACCESS_TABLE_NAME):
|
||||
logger.info("Creating table access table")
|
||||
conn.createTable(
|
||||
TABLE_ACCESS_TABLE_NAME,
|
||||
TABLE_ACCESS_TABLE_SCHEMA,
|
||||
|
|
@ -34,6 +39,7 @@ def bootstrapDB(conn: DBConnector):
|
|||
)
|
||||
|
||||
if not conn.tableExists(ASSETS_TABLE_NAME):
|
||||
logger.info("Creating assets table")
|
||||
conn.createTable(
|
||||
ASSETS_TABLE_NAME,
|
||||
ASSETS_TABLE_SCHEMA,
|
||||
|
|
@ -41,6 +47,7 @@ def bootstrapDB(conn: DBConnector):
|
|||
)
|
||||
|
||||
if not conn.tableExists(ASSET_ACCESS_TABLE_NAME):
|
||||
logger.info("Creating asset access table")
|
||||
conn.createTable(
|
||||
ASSET_ACCESS_TABLE_NAME,
|
||||
ASSET_ACCESS_TABLE_SCHEMA,
|
||||
|
|
@ -50,6 +57,7 @@ def bootstrapDB(conn: DBConnector):
|
|||
meta = get_metadata(conn, "admin_created")
|
||||
testAdminCreated = meta and meta.value == "yes"
|
||||
if not testAdminCreated:
|
||||
logger.info("Creating admin user and group")
|
||||
create_user(conn, "admin", "admin")
|
||||
create_group(conn, "admin")
|
||||
|
||||
|
|
@ -72,7 +80,7 @@ def add_metadata(conn: DBConnector, name: str, value: str):
|
|||
def get_metadata(conn: DBConnector, name: str):
|
||||
try:
|
||||
metadata = conn.filterFromTable(
|
||||
META_INFO_TABLE_NAME, ["*"], [ColumnCondition("name", name)]
|
||||
META_INFO_TABLE_NAME, ["*"], [ColumnCondition("name", "eq", name)]
|
||||
)
|
||||
if len(metadata) == 0:
|
||||
logger.warning(f"Metadata {name} not found")
|
||||
|
|
@ -106,7 +114,7 @@ def update_user(conn: DBConnector, id: int, password: str, access_token: str):
|
|||
ColumnUpdate("access_token", access_token),
|
||||
],
|
||||
[
|
||||
ColumnCondition("id", id),
|
||||
ColumnCondition("id", "eq", id),
|
||||
],
|
||||
)
|
||||
return True, None
|
||||
|
|
@ -118,7 +126,7 @@ def update_user(conn: DBConnector, id: int, password: str, access_token: str):
|
|||
def get_user_by_username(conn: DBConnector, username: str):
|
||||
try:
|
||||
users = conn.filterFromTable(
|
||||
USERS_TABLE_NAME, ["*"], [ColumnCondition("username", username)]
|
||||
USERS_TABLE_NAME, ["*"], [ColumnCondition("username", "eq", username)]
|
||||
)
|
||||
if len(users) == 0:
|
||||
logger.warning(f"User {username} not found")
|
||||
|
|
@ -132,7 +140,7 @@ def get_user_by_username(conn: DBConnector, username: str):
|
|||
def get_user_by_id(conn: DBConnector, user_id: int):
|
||||
try:
|
||||
users = conn.filterFromTable(
|
||||
USERS_TABLE_NAME, ["*"], [ColumnCondition("id", user_id)]
|
||||
USERS_TABLE_NAME, ["*"], [ColumnCondition("id", "eq", user_id)]
|
||||
)
|
||||
if len(users) == 0:
|
||||
logger.warning(f"User with id {user_id} not found")
|
||||
|
|
@ -146,7 +154,9 @@ def get_user_by_id(conn: DBConnector, user_id: int):
|
|||
def get_user_by_access_token(conn: DBConnector, access_token: str | None):
|
||||
try:
|
||||
users = conn.filterFromTable(
|
||||
USERS_TABLE_NAME, ["*"], [ColumnCondition("access_token", access_token)]
|
||||
USERS_TABLE_NAME,
|
||||
["*"],
|
||||
[ColumnCondition("access_token", "eq", access_token)],
|
||||
)
|
||||
if len(users) == 0:
|
||||
logger.warning("Invalid access token")
|
||||
|
|
@ -168,8 +178,8 @@ def check_user(conn: DBConnector, username: str, password: str):
|
|||
USERS_TABLE_NAME,
|
||||
["*"],
|
||||
[
|
||||
ColumnCondition("username", username),
|
||||
ColumnCondition("password", hashedPwd),
|
||||
ColumnCondition("username", "eq", username),
|
||||
ColumnCondition("password", "eq", hashedPwd),
|
||||
],
|
||||
)
|
||||
if len(user) == 0:
|
||||
|
|
@ -195,7 +205,7 @@ def create_group(conn: DBConnector, name: str, description: str = ""):
|
|||
def get_group_by_name(conn: DBConnector, name: str):
|
||||
try:
|
||||
groups = conn.filterFromTable(
|
||||
USER_GROUP_TABLE_NAME, ["*"], [ColumnCondition("name", name)]
|
||||
USER_GROUP_TABLE_NAME, ["*"], [ColumnCondition("name", "eq", name)]
|
||||
)
|
||||
if len(groups) == 0:
|
||||
logger.warning(f"Group {name} not found")
|
||||
|
|
@ -209,7 +219,7 @@ def get_group_by_name(conn: DBConnector, name: str):
|
|||
def get_group_by_id(conn: DBConnector, group_id: int):
|
||||
try:
|
||||
groups = conn.filterFromTable(
|
||||
USER_GROUP_TABLE_NAME, ["*"], [ColumnCondition("id", group_id)]
|
||||
USER_GROUP_TABLE_NAME, ["*"], [ColumnCondition("id", "eq", group_id)]
|
||||
)
|
||||
if len(groups) == 0:
|
||||
logger.warning(f"Group with id {group_id} not found")
|
||||
|
|
@ -226,7 +236,7 @@ def set_user_group(conn: DBConnector, user_id: int, group_id: int):
|
|||
USER_IN_USER_GROUP_JOIN_TABLE_NAME,
|
||||
["*"],
|
||||
[
|
||||
ColumnCondition("user_id", user_id),
|
||||
ColumnCondition("user_id", "eq", user_id),
|
||||
],
|
||||
):
|
||||
conn.insertIntoTable(
|
||||
|
|
@ -240,7 +250,7 @@ def set_user_group(conn: DBConnector, user_id: int, group_id: int):
|
|||
ColumnUpdate("user_group_id", group_id),
|
||||
],
|
||||
[
|
||||
ColumnCondition("user_id", user_id),
|
||||
ColumnCondition("user_id", "eq", user_id),
|
||||
],
|
||||
)
|
||||
return True, None
|
||||
|
|
@ -254,7 +264,7 @@ def get_user_group(conn: DBConnector, user_id: int):
|
|||
grp_usr_joint = conn.filterFromTable(
|
||||
USER_IN_USER_GROUP_JOIN_TABLE_NAME,
|
||||
["*"],
|
||||
[ColumnCondition("user_id", user_id)],
|
||||
[ColumnCondition("user_id", "eq", user_id)],
|
||||
)
|
||||
if len(grp_usr_joint) == 0:
|
||||
logger.warning(f"User with id {user_id} not found, so no group")
|
||||
|
|
@ -272,7 +282,7 @@ def get_group_users(conn: DBConnector, group_id: int) -> list[User]:
|
|||
users = conn.filterFromTable(
|
||||
USER_IN_USER_GROUP_JOIN_TABLE_NAME,
|
||||
["*"],
|
||||
[ColumnCondition("user_group_id", group_id)],
|
||||
[ColumnCondition("user_group_id", "eq", group_id)],
|
||||
)
|
||||
return [*map(User.parse_obj, users)]
|
||||
except Exception as e:
|
||||
|
|
@ -321,8 +331,8 @@ def get_table_access_level(
|
|||
TABLE_ACCESS_TABLE_NAME,
|
||||
["*"],
|
||||
[
|
||||
ColumnCondition("table_name", table_name),
|
||||
ColumnCondition("user_group_id", user_group.id),
|
||||
ColumnCondition("table_name", "eq", table_name),
|
||||
ColumnCondition("user_group_id", "eq", user_group.id),
|
||||
],
|
||||
)
|
||||
if not access:
|
||||
|
|
@ -349,8 +359,8 @@ def get_allowed_columns_for_group(
|
|||
TABLE_ACCESS_TABLE_NAME,
|
||||
["*"],
|
||||
[
|
||||
ColumnCondition("table_name", table_name),
|
||||
ColumnCondition("user_group_id", group_id),
|
||||
ColumnCondition("table_name", "eq", table_name),
|
||||
ColumnCondition("user_group_id", "eq", group_id),
|
||||
],
|
||||
)
|
||||
if not allowed_columns:
|
||||
|
|
@ -400,7 +410,7 @@ def create_asset(conn: DBConnector, name: str, description: str, fid: str):
|
|||
},
|
||||
)
|
||||
# TODO: add asset access
|
||||
# TODO: add asset to seaweedfs
|
||||
# TODO: add asset to minio
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.exception(e)
|
||||
|
|
@ -409,9 +419,9 @@ def create_asset(conn: DBConnector, name: str, description: str, fid: str):
|
|||
|
||||
def remove_asset(conn: DBConnector, token: str | None, asset_id: int):
|
||||
try:
|
||||
conn.deleteFromTable(ASSETS_TABLE_NAME, [ColumnCondition("id", asset_id)])
|
||||
conn.deleteFromTable(ASSETS_TABLE_NAME, [ColumnCondition("id", "eq", asset_id)])
|
||||
# TODO: remove asset access
|
||||
# TODO: remove asset from seaweedfs
|
||||
# TODO: remove asset from minio
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.exception(e)
|
||||
|
|
@ -422,7 +432,7 @@ def get_asset(conn: DBConnector, token: str | None, fid: str):
|
|||
try:
|
||||
user, group = get_user_by_access_token(conn, token)
|
||||
assets = conn.filterFromTable(
|
||||
ASSETS_TABLE_NAME, ["*"], [ColumnCondition("fid", fid)]
|
||||
ASSETS_TABLE_NAME, ["*"], [ColumnCondition("fid", "eq", fid)]
|
||||
)
|
||||
print(assets)
|
||||
if len(assets) == 0:
|
||||
|
|
@ -453,7 +463,9 @@ def create_asset_access(conn: DBConnector, asset_id: int, user_group_id: int):
|
|||
def get_asset_access(conn: DBConnector, asset_id: int):
|
||||
try:
|
||||
access = conn.filterFromTable(
|
||||
ASSET_ACCESS_TABLE_NAME, ["*"], [ColumnCondition("asset_id", asset_id)]
|
||||
ASSET_ACCESS_TABLE_NAME,
|
||||
["*"],
|
||||
[ColumnCondition("asset_id", "eq", asset_id)],
|
||||
)
|
||||
if not access:
|
||||
return AccessType.NONE
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue