From 87065461784719196154b37340e6c8f76b4d801f Mon Sep 17 00:00:00 2001 From: Jeremy Stanley Date: Wed, 30 Sep 2020 19:57:13 +0000 Subject: [PATCH] Add is_admin method to Element and User classes For clarity and convenience, create an is_admin() boolean check for whether a User's account or an actor Element's owner's account is flagged as an administrator. Put them to use in places where the administrator facet was previously checked directly. This allows us to do a little additional safety checking to avoid hitting corner cases where we try to check some object which may or may not have an owner. --- mudpy/misc.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/mudpy/misc.py b/mudpy/misc.py index fd4e0ac..e737a28 100644 --- a/mudpy/misc.py +++ b/mudpy/misc.py @@ -184,6 +184,10 @@ class Element: prepend_padding ) + def is_admin(self): + """Boolean check whether an actor is controlled by an admin owner.""" + return(self.owner and self.owner.is_admin()) + def can_run(self, command): """Check if the user can run this command object.""" @@ -192,7 +196,7 @@ class Element: return(False) # avatars of administrators can run any command - if self.owner and self.owner.account.get("administrator"): + if self.is_admin(): return(True) # everyone can run non-administrative commands @@ -995,6 +999,10 @@ class User: avatar, 6) return avatars + def is_admin(self): + """Boolean check whether user's account is an admin.""" + return(self.account.get("administrator", False)) + def broadcast(message, add_prompt=True): """Send a message to all connected users.""" @@ -1049,9 +1057,10 @@ def log(message, level=0): # display to connected administrators for user in universe.userlist: - if user.state == "active" and user.account.get( - "administrator" - ) and user.account.get("loglevel", 0) <= level: + if ( + user.state == "active" + and user.is_admin() + and user.account.get("loglevel", 0) <= level): # iterate over every line in the message full_message = "" for line in lines: -- 2.11.0