Improvements upon front lead to improvements here
This commit is contained in:
parent
3414b5c334
commit
34ae028698
6 changed files with 395 additions and 169 deletions
167
dba.py
167
dba.py
|
|
@ -1,8 +1,11 @@
|
|||
import logging
|
||||
from secrets import token_hex
|
||||
from based.db import DBConnector, ColumnCondition, ColumnUpdate, ColumnDefinition
|
||||
from db_addendum import AssetRefColumnDefinition, UserRefColumnDefinition
|
||||
from db_models import *
|
||||
from models import TableDefinition
|
||||
from secutils import hash_password
|
||||
import utils
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
|
@ -46,25 +49,18 @@ def bootstrapDB(conn: DBConnector):
|
|||
system=True,
|
||||
)
|
||||
|
||||
if not conn.tableExists(ASSET_ACCESS_TABLE_NAME):
|
||||
logger.info("Creating asset access table")
|
||||
conn.createTable(
|
||||
ASSET_ACCESS_TABLE_NAME,
|
||||
ASSET_ACCESS_TABLE_SCHEMA,
|
||||
system=True,
|
||||
)
|
||||
|
||||
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")
|
||||
create_group(conn, "anonymous", "Default group for anonymous access")
|
||||
create_group(conn, "admin", "Administrator group")
|
||||
|
||||
users = list_users(conn)
|
||||
groups = list_groups(conn)
|
||||
|
||||
set_user_group(conn, users[0].id, groups[0].id)
|
||||
set_user_group(conn, users[0].id, groups[1].id)
|
||||
add_metadata(conn, "admin_created", "yes")
|
||||
|
||||
|
||||
|
|
@ -123,6 +119,45 @@ def update_user(conn: DBConnector, id: int, password: str, access_token: str):
|
|||
return False, e
|
||||
|
||||
|
||||
def delete_user(
|
||||
conn: DBConnector,
|
||||
id: int,
|
||||
check_references: bool = True,
|
||||
delete_referencing: bool = False,
|
||||
):
|
||||
try:
|
||||
if check_references:
|
||||
table_with_user_ref: list[tuple[str, ColumnDefinition]] = []
|
||||
for table_def in conn.tables():
|
||||
table = TableDefinition.parse_obj(table_def)
|
||||
columns = utils.parse_columns_from_definition(table.columns)
|
||||
for column in columns:
|
||||
if column is UserRefColumnDefinition:
|
||||
table_with_user_ref.append((table.table_name, column))
|
||||
|
||||
if delete_referencing:
|
||||
for table_name, column in table_with_user_ref:
|
||||
conn.deleteFromTable(
|
||||
table_name,
|
||||
[
|
||||
ColumnCondition(column.name, "eq", id),
|
||||
],
|
||||
)
|
||||
elif table_with_user_ref:
|
||||
raise Exception("User is referenced in other tables")
|
||||
|
||||
conn.deleteFromTable(
|
||||
USERS_TABLE_NAME,
|
||||
[
|
||||
ColumnCondition("id", "eq", id),
|
||||
],
|
||||
)
|
||||
return True, None
|
||||
except Exception as e:
|
||||
logger.exception(e)
|
||||
return False, e
|
||||
|
||||
|
||||
def get_user_by_username(conn: DBConnector, username: str):
|
||||
try:
|
||||
users = conn.filterFromTable(
|
||||
|
|
@ -270,8 +305,8 @@ def get_user_group(conn: DBConnector, user_id: int):
|
|||
logger.warning(f"User with id {user_id} not found, so no group")
|
||||
return None
|
||||
|
||||
uiug = UserInUserGroup.parse_obj(grp_usr_joint[0])
|
||||
return get_group_by_id(conn, uiug.user_group_id)
|
||||
u_i_u_g = UserInUserGroup.parse_obj(grp_usr_joint[0])
|
||||
return get_group_by_id(conn, u_i_u_g.user_group_id)
|
||||
except Exception as e:
|
||||
logger.exception(e)
|
||||
return None
|
||||
|
|
@ -299,7 +334,7 @@ def list_users(conn: DBConnector) -> list[User]:
|
|||
return []
|
||||
|
||||
|
||||
def list_groups(conn: DBConnector):
|
||||
def list_groups(conn: DBConnector) -> list[UserGroup]:
|
||||
try:
|
||||
groups = conn.selectFromTable(USER_GROUP_TABLE_NAME, ["*"])
|
||||
return [*map(UserGroup.parse_obj, groups)]
|
||||
|
|
@ -311,10 +346,10 @@ def list_groups(conn: DBConnector):
|
|||
def create_table(conn: DBConnector, table_name: str, schema: list[ColumnDefinition]):
|
||||
try:
|
||||
conn.createTable(table_name, schema)
|
||||
return True
|
||||
return True, None
|
||||
except Exception as e:
|
||||
logger.exception(e)
|
||||
return False
|
||||
return False, e
|
||||
|
||||
|
||||
def get_table_access_level(
|
||||
|
|
@ -409,37 +444,68 @@ def create_asset(conn: DBConnector, name: str, description: str, fid: str):
|
|||
"fid": fid,
|
||||
},
|
||||
)
|
||||
# TODO: add asset access
|
||||
# TODO: add asset to minio
|
||||
return True
|
||||
return True, None
|
||||
except Exception as e:
|
||||
logger.exception(e)
|
||||
return None
|
||||
return False, None
|
||||
|
||||
|
||||
def remove_asset(conn: DBConnector, token: str | None, asset_id: int):
|
||||
def update_asset(conn: DBConnector, asset_id: int, asset_description: str):
|
||||
try:
|
||||
conn.updateDataInTable(
|
||||
ASSETS_TABLE_NAME,
|
||||
[ColumnUpdate("description", asset_description)],
|
||||
[ColumnCondition("id", "eq", asset_id)],
|
||||
)
|
||||
return True, None
|
||||
except Exception as e:
|
||||
logger.exception(e)
|
||||
return False, e
|
||||
|
||||
|
||||
def remove_asset(
|
||||
conn: DBConnector,
|
||||
asset_id: int,
|
||||
check_references: bool = True,
|
||||
delete_referencing: bool = False,
|
||||
):
|
||||
try:
|
||||
if check_references:
|
||||
table_with_asset_ref: list[tuple[str, ColumnDefinition]] = []
|
||||
for table_def in conn.tables():
|
||||
table = TableDefinition.parse_obj(table_def)
|
||||
columns = utils.parse_columns_from_definition(table.columns)
|
||||
for column in columns:
|
||||
if column is AssetRefColumnDefinition:
|
||||
table_with_asset_ref.append((table.table_name, column))
|
||||
|
||||
if delete_referencing:
|
||||
for table_name, column in table_with_asset_ref:
|
||||
conn.deleteFromTable(
|
||||
table_name,
|
||||
[
|
||||
ColumnCondition(column.name, "eq", asset_id),
|
||||
],
|
||||
)
|
||||
elif table_with_asset_ref:
|
||||
raise Exception("Asset is referenced in other tables")
|
||||
|
||||
conn.deleteFromTable(ASSETS_TABLE_NAME, [ColumnCondition("id", "eq", asset_id)])
|
||||
# TODO: remove asset access
|
||||
# TODO: remove asset from minio
|
||||
return True
|
||||
return True, None
|
||||
except Exception as e:
|
||||
logger.exception(e)
|
||||
return False
|
||||
return False, e
|
||||
|
||||
|
||||
def get_asset(conn: DBConnector, token: str | None, fid: str):
|
||||
def get_asset(conn: DBConnector, fid: str):
|
||||
try:
|
||||
user, group = get_user_by_access_token(conn, token)
|
||||
assets = conn.filterFromTable(
|
||||
ASSETS_TABLE_NAME, ["*"], [ColumnCondition("fid", "eq", fid)]
|
||||
)
|
||||
print(assets)
|
||||
|
||||
if len(assets) == 0:
|
||||
return None
|
||||
asset = Asset.parse_obj(assets[0])
|
||||
asset_access = get_asset_access(conn, asset.id)
|
||||
# TODO: check if user has access to asset
|
||||
|
||||
return asset
|
||||
except Exception as e:
|
||||
|
|
@ -447,44 +513,17 @@ def get_asset(conn: DBConnector, token: str | None, fid: str):
|
|||
return None
|
||||
|
||||
|
||||
def create_asset_access(conn: DBConnector, asset_id: int, user_group_id: int):
|
||||
def get_asset_by_id(conn: DBConnector, asset_id: int):
|
||||
try:
|
||||
conn.insertIntoTable(
|
||||
ASSET_ACCESS_TABLE_NAME,
|
||||
{"asset_id": asset_id, "user_group_id": user_group_id},
|
||||
assets = conn.filterFromTable(
|
||||
ASSETS_TABLE_NAME, ["*"], [ColumnCondition("id", "eq", asset_id)]
|
||||
)
|
||||
return True
|
||||
except Exception as e:
|
||||
# NOTE: this should not happen ever
|
||||
logger.exception(e)
|
||||
return False
|
||||
|
||||
if len(assets) == 0:
|
||||
return None
|
||||
asset = Asset.parse_obj(assets[0])
|
||||
|
||||
def get_asset_access(conn: DBConnector, asset_id: int):
|
||||
try:
|
||||
access = conn.filterFromTable(
|
||||
ASSET_ACCESS_TABLE_NAME,
|
||||
["*"],
|
||||
[ColumnCondition("asset_id", "eq", asset_id)],
|
||||
)
|
||||
if not access:
|
||||
return AccessType.NONE
|
||||
access = AssetAccess.parse_obj(access[0])
|
||||
if access.access_type == "r":
|
||||
return AccessType.READ
|
||||
elif access.access_type == "w":
|
||||
return AccessType.WRITE
|
||||
elif access.access_type == "rw":
|
||||
return AccessType.READ_WRITE
|
||||
else:
|
||||
return AccessType.NONE
|
||||
return asset
|
||||
except Exception as e:
|
||||
logger.exception(e)
|
||||
return AccessType.NONE
|
||||
|
||||
|
||||
def change_asset_access(
|
||||
conn: DBConnector, asset_id: int, user_group_id: int, access_type: AccessType
|
||||
):
|
||||
# TODO: implement
|
||||
raise NotImplementedError()
|
||||
return None
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue