permissionHandler

Module permissionHandler |
[hide private]
[frames] | no frames]

## Source Code for Module permissionHandler

     1
     2  from utils import elementType, getFirstData
     3  from types import MethodType
     4
     5
     6 -class PermissionHandler:
     7      # Write ourselves some code to run to check permissions.
     8
     9      actionIdentifier = ""
    10      codeText = []
    11      hasPermission = None
    12
    13 -    def __init__(self, node, parent):
    14          self.parent = parent
    15          self._walkNode(node)
    16
    17 -    def _walkNode(self, node):
    18
    19          if node.localName == "action":
    20              # top node
    21              self.actionIdentifier = node.getAttributeNS(None, 'identifier')
    22              if not self.actionIdentifier:
    23                  raise ConfigFileException("No action identifier")
    24              self.code = ["  if "]
    25              for c in node.childNodes:
    26                  if c.nodeType == elementType:
    27                      self._walkNode(c)
    28              self.code.append(":")
    29              cstring = " ".join(self.code)
    30              fncode = "def handler(self, session, user):\n" + cstring + "\n    return True;\n  else:\n    return False"
    31              exec fncode
    32              setattr(self, 'hasPermission', MethodType(locals()['handler'], self, self.__class__))
    33          elif node.localName in ["all", "any"]:
    34              if node.localName == "all":
    35                  bool = "and"
    36              else:
    37                  bool = "or"
    38              self.code.append("(")
    39              for c in node.childNodes:
    40                  if c.nodeType == elementType:
    41                      self._walkNode(c)
    42                      self.code.append(bool)
    43              self.code.pop()
    44              self.code.append(")")
    45          elif node.localName == "flag":
    46              f = getFirstData(node)
    47              self.code.append("user.hasFlag(session, \"%s\", object)" % f)
    48          elif node.localName == "environment":
    49              e = getFirstData(node)
    50              self.code.append("session.environment == \"%s\"" % e)
    51          elif node.localName == "hostmask":
    52              e = getFirstData(node)
    53              self.code.append("user.connectedFrom(session, \"%s\")" % e)
    54