Add is_admin method to Element and User classes
[mudpy.git] / mudpy / misc.py
index ba29a90..e737a28 100644 (file)
@@ -184,27 +184,27 @@ 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."""
 
         # has to be in the commands group
         if command not in self.universe.groups["command"].values():
-            result = False
+            return(False)
 
         # avatars of administrators can run any command
-        elif self.owner and self.owner.account.get("administrator"):
-            result = True
+        if self.is_admin():
+            return(True)
 
         # everyone can run non-administrative commands
-        elif not command.get("administrative"):
-            result = True
+        if not command.get("administrative"):
+            return(True)
 
         # otherwise the command cannot be run by this actor
-        else:
-            result = False
-
-        # pass back the result
-        return result
+        return(False)
 
     def update_location(self):
         """Make sure the location's contents contain this element."""
@@ -999,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."""
@@ -1053,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:
@@ -1227,7 +1232,7 @@ def weighted_choice(data):
             expanded.append(key)
 
     # return one at random
-    # Whitelist the random.randrange() call in bandit since it's not used for
+    # Allow the random.randrange() call in bandit since it's not used for
     # security/cryptographic purposes
     return random.choice(expanded)  # nosec
 
@@ -1276,7 +1281,7 @@ def random_name():
     name = ""
 
     # create a name of random length from the syllables
-    # Whitelist the random.randrange() call in bandit since it's not used for
+    # Allow the random.randrange() call in bandit since it's not used for
     # security/cryptographic purposes
     for _syllable in range(random.randrange(2, 6)):  # nosec
         name += weighted_choice(syllables)