From fcba3b6132890eaeb94f66fc668327d25c23954d Mon Sep 17 00:00:00 2001 From: Pierre-Luc Bertrand Date: Fri, 15 Apr 2022 18:33:33 -0400 Subject: [PATCH] Refactor topic_checking to simplify booleans --- amqtt/plugins/topic_checking.py | 58 ++++++++++++++++----------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/amqtt/plugins/topic_checking.py b/amqtt/plugins/topic_checking.py index aa2179f..ac33f02 100644 --- a/amqtt/plugins/topic_checking.py +++ b/amqtt/plugins/topic_checking.py @@ -67,35 +67,35 @@ class TopicAccessControlListPlugin(BaseTopicPlugin): async def topic_filtering(self, *args, **kwargs): filter_result = super().topic_filtering(*args, **kwargs) - if filter_result: - session = kwargs.get("session", None) - req_topic = kwargs.get("topic", None) - action = kwargs.get("action", None) + if not filter_result: + return False - # hbmqtt and older amqtt do not support publish filtering - if action == Action.publish and "publish-acl" not in self.topic_config: - # maintain backward compatibility, assume permitted + # hbmqtt and older amqtt do not support publish filtering + action = kwargs.get("action", None) + if action == Action.publish and "publish-acl" not in self.topic_config: + # maintain backward compatibility, assume permitted + return True + + req_topic = kwargs.get("topic", None) + if not req_topic: + return False + + session = kwargs.get("session", None) + username = session.username + if username is None: + username = "anonymous" + + if action == Action.publish: + acl = self.topic_config["publish-acl"] + elif action == Action.subscribe: + acl = self.topic_config["acl"] + + allowed_topics = acl.get(username, None) + if not allowed_topics: + return False + + for allowed_topic in allowed_topics: + if self.topic_ac(req_topic, allowed_topic): return True - if req_topic: - username = session.username - if username is None: - username = "anonymous" - - if action == Action.publish: - acl = self.topic_config["publish-acl"] - elif action == Action.subscribe: - acl = self.topic_config["acl"] - - allowed_topics = acl.get(username, None) - if allowed_topics: - for allowed_topic in allowed_topics: - if self.topic_ac(req_topic, allowed_topic): - return True - return False - else: - return False - else: - return False - else: - return False + return False