diff sat/memory/memory.py @ 3777:001ea5f4a2f9

core: method to know if a profile/entity is an admin: the new `is_admin` SatXMPPEntity property tell if it's an administrator. Admin JIDs are retrieve on init, so they can be looked after when profle is not available (notably when a component handle a request and has only a JID available). The new `memory.isAdminJID` method is then used.
author Goffi <goffi@goffi.org>
date Sun, 15 May 2022 14:14:52 +0200
parents 71516731d0aa
children 580df5b557be
line wrap: on
line diff
--- a/sat/memory/memory.py	Sat May 14 23:00:35 2022 +0200
+++ b/sat/memory/memory.py	Sun May 15 14:14:52 2022 +0200
@@ -239,6 +239,9 @@
         self.disco = Discovery(host)
         self.config = tools_config.parseMainConf(log_filenames=True)
         self._cache_path = Path(self.getConfig("", "local_dir"), C.CACHE_DIR)
+        self.admins = self.getConfig("", "admins_list", [])
+        self.admin_jids = set()
+
 
     async def initialise(self):
         self.storage = Storage()
@@ -251,6 +254,21 @@
         self.memory_data = PersistentDict("memory")
         await self.memory_data.load()
         await self.disco.load()
+        for admin in self.admins:
+            try:
+                admin_jid_s = await self.asyncGetParamA(
+                    "JabberID", "Connection", profile_key=admin
+                )
+            except Exception as e:
+                log.warning(f"Can't retrieve jid of admin {admin!r}: {e}")
+            else:
+                if admin_jid_s is not None:
+                    try:
+                        admin_jid = jid.JID(admin_jid_s).userhostJID()
+                    except RuntimeError:
+                        log.warning(f"Invalid JID for admin {admin}: {admin_jid_s}")
+                    else:
+                        self.admin_jids.add(admin_jid)
 
 
     ## Configuration ##
@@ -1849,3 +1867,16 @@
             log.debug("No presence information for {}".format(entity_jid))
             return False
         return presence_data.show != C.PRESENCE_UNAVAILABLE
+
+    def isAdmin(self, profile: str) -> bool:
+        """Tell if given profile has administrator privileges"""
+        return profile in self.admins
+
+    def isAdminJID(self, entity: jid.JID) -> bool:
+        """Tells if an entity jid correspond to an admin one
+
+        It is sometime not possible to use the profile alone to check if an entity is an
+        admin (e.g. a request managed by a component). In this case we check if the JID
+        correspond to an admin profile
+        """
+        return entity.userhostJID() in self.admin_jids