sharing: review enable/disable/hide/unhide API

This commit is contained in:
Peter Bieringer
2026-03-03 17:39:00 +01:00
parent 3267cc5228
commit c74365cd8a
5 changed files with 92 additions and 243 deletions

View File

@@ -383,99 +383,6 @@ class Sharing(sharing.BaseSharing):
else:
return {"status": "not-found"}
def toggle_sharing(self,
ShareType: str,
PathOrToken: str,
OwnerOrUser: str,
Action: str,
PathMapped: Union[str, None] = None,
User: Union[str, None] = None,
Timestamp: int = 0) -> dict:
""" toggle sharing """
row: dict
if Action not in sharing.API_SHARE_TOGGLES_V1:
# should not happen
raise
if logger.isEnabledFor(logging.DEBUG):
logger.debug("TRACE/sharing/%s/%s: OwnerOrUser=%r User=%r PathOrToken=%r PathMapped=%r", ShareType, Action, OwnerOrUser, User, PathOrToken, PathMapped)
# lookup entry
found = False
index = 0
for row in self._sharing_cache:
if index == 0:
# skip fieldnames
pass
if logger.isEnabledFor(logging.DEBUG):
logger.debug("TRACE/sharing/*/" + Action + ": check: %r", row)
if row['ShareType'] != ShareType:
pass
elif row['PathOrToken'] != PathOrToken:
pass
elif PathMapped is not None and row['PathMapped'] != PathMapped:
pass
elif row['Owner'] == OwnerOrUser:
found = True
break
else:
found = True
break
index += 1
if found:
# if logger.isEnabledFor(logging.DEBUG):
# logger.debug("TRACE/sharing/*/" + Action + ": found: %r", row)
if User is not None and row['User'] != User:
return {"status": "permission-denied"}
elif row['Owner'] == OwnerOrUser:
pass
elif row['User'] == OwnerOrUser:
pass
else:
return {"status": "permission-denied"}
# TODO: locking
if row['Owner'] == OwnerOrUser:
if logger.isEnabledFor(logging.DEBUG):
logger.debug("TRACE/sharing/%s/%s: Owner=%r User=%r PathOrToken=%r index=%d", ShareType, Action, OwnerOrUser, User, PathOrToken, index)
if Action == "disable":
row['EnabledByOwner'] = False
elif Action == "enable":
row['EnabledByOwner'] = True
elif Action == "hide":
row['HiddenByOwner'] = True
elif Action == "unhide":
row['HiddenByOwner'] = False
row['TimestampUpdated'] = Timestamp
if row['User'] == OwnerOrUser:
if logger.isEnabledFor(logging.DEBUG):
logger.debug("TRACE/sharing/%s/%s: User=%r PathOrToken=%r index=%d", ShareType, Action, OwnerOrUser, PathOrToken, index)
if Action == "disable":
row['EnabledByUser'] = False
elif Action == "enable":
row['EnabledByUser'] = True
elif Action == "hide":
row['HiddenByUser'] = True
elif Action == "unhide":
row['HiddenByUser'] = False
row['TimestampUpdated'] = Timestamp
# replace
self._sharing_cache[index] = row
with self._storage.acquire_lock("w", OwnerOrUser, path=self._sharing_db_file):
if self._write_csv(self._sharing_db_file):
if logger.isEnabledFor(logging.DEBUG):
logger.debug("TRACE: write CSV done")
return {"status": "success"}
logger.error("sharing: cannot update CSV database")
return {"status": "error"}
else:
return {"status": "not-found"}
# local functions
def _create_empty_csv(self, file: str) -> bool:
with self._storage.acquire_lock("w", None, path=file):