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.
This commit is contained in:
Goffi 2022-05-15 14:14:52 +02:00
commit c8025a52eb

View file

@ -239,6 +239,9 @@ class Memory:
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 @@ class Memory:
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 @@ class Memory:
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