示例#1
0
 def test_me(self):
     al = AccessList()
     me = IahrConfig.ME
     assert al.is_allowed(me)
     al.ban(me)
     assert al.is_allowed(me)
     al = AccessList(allow_others=True)
     assert al.is_allowed(me)
示例#2
0
    def test_others(self, operation):
        should_be = operation == 'allow'
        opposite = 'allow' if operation == 'ban' else 'ban'
        al = AccessList()
        others = IahrConfig.OTHERS

        assert not al.is_allowed(self.ENT)
        getattr(al, operation)(others)
        assert al.is_allowed(self.ENT) == should_be

        getattr(al, opposite)(self.ENT)
        assert not (al.is_allowed(self.ENT) == should_be)
        getattr(al, operation)(others)
        assert al.is_allowed(self.ENT) == should_be
示例#3
0
class Routine:
    """
        Class that contains raw command handler and 
        manages permissions to use it in chats and by users
    """
    def __init__(self, handler: Callable, about: str, allow_selfact=False):
        self.about = about
        self.handler = handler

        self.usraccess = AccessList(allow_others=False,
                                    allow_selfact=allow_selfact)
        self.chataccess = AccessList(allow_others=True,
                                     allow_selfact=allow_selfact)

    def help(self):
        return self.about

    ##################################################
    # Rights to run this handler
    ##################################################

    def allow_usr(self, usr: str):
        self.usraccess.allow(usr)
        print(usr, self.usraccess)

    def ban_usr(self, usr: str):
        self.usraccess.ban(usr)

    def is_allowed_usr(self, usr: str):
        return self.usraccess.is_allowed(usr)

    def allow_chat(self, chat: str):
        self.chataccess.allow(chat)

    def ban_chat(self, chat: str):
        self.chataccess.ban(chat)

    def is_allowed_chat(self, chat: str):
        return self.chataccess.is_allowed(chat)

    def get_handler(self, usr: str, chat: str):
        """
            Try to get handler if allowed
        """
        IahrConfig.LOGGER.debug(f'getting handler:usr={usr}:chat={chat}')

        if not self.is_allowed_chat(chat) or not self.is_allowed_usr(usr):
            if not (self.chataccess.is_self(usr)
                    and self.usraccess.is_self(usr)):
                return

        return self.handler

    ##################################################
    # Session managing
    ##################################################

    def get_state(self):
        return {
            'usraccess': self.usraccess,
            'chataccess': self.chataccess,
        }

    def set_state(self, state):
        self.usraccess = state['usraccess']
        self.chataccess = state['chataccess']

    JSON_ENCODER = AccessList.ALEncoder
    JSON_DECODER = AccessList.ALDecoder

    def __repr__(self):
        return f'Routine(usraccess={self.usraccess}, chataccess={self.chataccess})'
示例#4
0
 def test_simple_operation(self, operation, should_be):
     for allow_others in (True, False):
         al = AccessList(allow_others)
         getattr(al, operation)(self.ENT)
         assert al.is_allowed(self.ENT) == should_be