From 9ac81611ad0735ce1a4253ac680887f8ed5ed3b8 Mon Sep 17 00:00:00 2001 From: Mark Hendriksen Date: Sun, 31 Mar 2024 19:08:15 +0200 Subject: [PATCH] add remove apiuser (#1012) * fix typo * fix connection error because of api version * RDM002 * rdm002 * update mqtt sensor * fix sensor type * add remove apiuser --- BridgeEmulator/flaskUI/restful.py | 18 ++++++++++++++++-- BridgeEmulator/services/updateManager.py | 14 ++++---------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/BridgeEmulator/flaskUI/restful.py b/BridgeEmulator/flaskUI/restful.py index 24170c06a..3f6f4bfc5 100644 --- a/BridgeEmulator/flaskUI/restful.py +++ b/BridgeEmulator/flaskUI/restful.py @@ -42,10 +42,12 @@ def authorize(username, resource='', resourceId='', resourceParam=''): if username not in bridgeConfig["apiUsers"] and request.remote_addr != "127.0.0.1": return [{"error": {"type": 1, "address": "/" + resource + "/" + resourceId, "description": "unauthorized user"}}] - if resourceId not in ["0", "new", "timezones"] and resourceId != '' and resourceId not in bridgeConfig[resource]: + if resourceId not in ["0", "new", "timezones", "whitelist"] and resourceId != '' and resourceId not in bridgeConfig[resource]: + logging.debug(str(resourceId) + " not in bridgeConfig " + str(resource)) return [{"error": {"type": 3, "address": "/" + resource + "/" + resourceId, "description": "resource, " + resource + "/" + resourceId + ", not available"}}] if resourceId != "0" and resourceParam != '' and not hasattr(bridgeConfig[resource][resourceId], resourceParam): + logging.debug(str(resourceId) + " has no attribute " + str(resourceParam)) return [{"error": {"type": 3, "address": "/" + resource + "/" + resourceId + "/" + resourceParam, "description": "resource, " + resource + "/" + resourceId + "/" + resourceParam + ", not available"}}] if request.remote_addr != "127.0.0.1": bridgeConfig["apiUsers"][username].last_use_date = datetime.utcnow().strftime( @@ -126,7 +128,7 @@ def get(self, username, resource): return capabilities() else: response = {} - if resource in ["lights", "groups", "scenes", "rules", "resourcelinks", "schedules", "sensors"]: + if resource in ["lights", "groups", "scenes", "rules", "resourcelinks", "schedules", "sensors", "apiUsers"]: for object in bridgeConfig[resource]: response[object] = bridgeConfig[resource][object].getV1Api().copy() elif resource == "config": @@ -457,6 +459,18 @@ def delete(self, username, resource, resourceid, param): authorisation = authorize(username, resource, resourceid) if "success" not in authorisation: return authorisation + if resourceid == "whitelist": + for config in ["lights", "groups", "scenes", "rules", "resourcelinks", "schedules", "sensors"]: + for object in bridgeConfig[config]: + if "owner" in bridgeConfig[config][object].getV1Api(): + current_owner = bridgeConfig[config][object].getV1Api()["owner"] + if current_owner == param: + logging.debug("transfer ownership from: " + str(current_owner) + " to: " + str(username)) + bridgeConfig[config][object].owner = bridgeConfig["apiUsers"][username] + logging.debug("Deleted api user: " + str(param) + " " + bridgeConfig["apiUsers"][param].name) + del bridgeConfig["apiUsers"][param] + configManager.bridgeConfig.save_config() + return [{"success": "/" + resource + "/" + resourceid + "/" + param + " deleted."}] if param not in bridgeConfig[resource][resourceid]: return [{"error": {"type": 4, "address": "/" + resource + "/" + resourceid, "description": "method, DELETE, not available for resource, " + resource + "/" + resourceid}}] diff --git a/BridgeEmulator/services/updateManager.py b/BridgeEmulator/services/updateManager.py index cfb6478f1..ce74bc404 100644 --- a/BridgeEmulator/services/updateManager.py +++ b/BridgeEmulator/services/updateManager.py @@ -52,11 +52,11 @@ def githubCheck(): if publish_time > creation_time: logging.info("update on github") bridgeConfig["config"]["swupdate2"]["state"] = "allreadytoinstall" - bridgeConfig["config"]["swupdate2"]["bridge"]["state"] = "allreadytoinstall" + #bridgeConfig["config"]["swupdate2"]["bridge"]["state"] = "allreadytoinstall" elif githubUICheck() == True: logging.info("UI update on github") bridgeConfig["config"]["swupdate2"]["state"] = "anyreadytoinstall" - bridgeConfig["config"]["swupdate2"]["bridge"]["state"] = "anyreadytoinstall" + #bridgeConfig["config"]["swupdate2"]["bridge"]["state"] = "anyreadytoinstall" else: logging.info("no update for diyHue or UI on github") bridgeConfig["config"]["swupdate2"]["state"] = "noupdates" @@ -94,19 +94,13 @@ def githubUICheck(): def githubInstall(): if bridgeConfig["config"]["swupdate2"]["state"] == "anyreadytoinstall":#ui update bridgeConfig["config"]["swupdate2"]["state"] = "installing" - bridgeConfig["config"]["swupdate2"]["bridge"]["state"] = "installing" + #bridgeConfig["config"]["swupdate2"]["bridge"]["state"] = "installing" subprocess.Popen("sh githubUIInstall.sh",shell=True, close_fds=True) if bridgeConfig["config"]["swupdate2"]["state"] == "allreadytoinstall":#diyhue + ui update bridgeConfig["config"]["swupdate2"]["state"] = "installing" - bridgeConfig["config"]["swupdate2"]["bridge"]["state"] = "installing" + #bridgeConfig["config"]["swupdate2"]["bridge"]["state"] = "installing" subprocess.Popen("sh githubInstall.sh",shell=True, close_fds=True) -def githubInstall_test(): - logging.info("work in progress") - bridgeConfig["config"]["swupdate2"]["install"] = False - bridgeConfig["config"]["swupdate2"]["state"] = "noupdates" - bridgeConfig["config"]["swupdate2"]["bridge"]["state"] = "noupdates" - def startupCheck(): if bridgeConfig["config"]["swupdate2"]["install"] == True: bridgeConfig["config"]["swupdate2"]["install"] = False