示例#1
0
def public_methods(obj):
    """
    Returns a list of names of the methods of `obj` to be exposed.
    Supports attribute access in addition to RBAC decorated methods.

    obj: object
        Object to be scanned.
    """
    # Proxy pass-through only happens remotely.
    if isinstance(obj, BaseProxy):  #pragma no cover
        methods = []
        for name in dir(obj):
            if name[0] != '_':
                attr = getattr(obj, name)
                if inspect.ismethod(attr) or inspect.isfunction(attr):
                    methods.append(name)
    else:
        methods = rbac_methods(obj)

    # Add special methods for attribute access.
    methods.extend([name for name in SPECIALS if hasattr(obj, name)])

    # Add special __is_instance__ and __has_interface__ methods.
    methods.append('__is_instance__')
    methods.append('__has_interface__')
    return methods
    def test_decorator(self):
        logging.debug('')
        logging.debug('test_decorator')

        # Decorated methods.
        obj = Object()
        methods = [name for name in dir(obj)
                                 if inspect.ismethod(getattr(obj, name))]
        methods.remove('no_rbac')
        self.assertEqual(sorted(rbac_methods(obj)), sorted(methods))

        # Result proxying.
        normal_value = object()
        proxy_value = ProxyRequired()
        dummy = AccessController()
        self.assertFalse(need_proxy(obj.no_rbac, proxy_value, dummy))
        self.assertFalse(need_proxy(obj.single_role, normal_value, dummy))
        self.assertFalse(need_proxy(obj.proxy_result, normal_value, dummy))
        self.assertTrue(need_proxy(obj.proxy_result, proxy_value, dummy))

        # Access checking.
        assert_raises(self, "check_role('owner', obj.no_rbac)",
                      globals(), locals(), RoleError,
                      'No RBAC for function!')

        assert_raises(self, "check_role('xyzzy', obj.single_role)",
                      globals(), locals(), RoleError,
                      "No access for role 'xyzzy'")

        check_role('owner', obj.multi_role)
        check_role('user',  obj.multi_role)
        check_role('xyzzy', obj.role_pattern)
示例#3
0
def public_methods(obj):
    """
    Returns a list of names of the methods of `obj` to be exposed.
    Supports attribute access in addition to RBAC decorated methods.

    obj: object
        Object to be scanned.
    """
    # Proxy pass-through only happens remotely.
    if isinstance(obj, BaseProxy):  #pragma no cover
        methods = []
        for name in dir(obj):
            if name[0] != '_':
                attr = getattr(obj, name)
                if inspect.ismethod(attr) or inspect.isfunction(attr):
                    methods.append(name)
    else:
        methods = rbac_methods(obj)

    # Add special methods for attribute access.
    methods.extend([name for name in SPECIALS if hasattr(obj, name)])

    # Add special __is_instance__ and __has_interface__ methods.
    methods.append('__is_instance__')
    methods.append('__has_interface__')
    return methods
示例#4
0
    def test_decorator(self):
        logging.debug('')
        logging.debug('test_decorator')

        # Decorated methods.
        obj = Object()
        methods = [
            name for name in dir(obj) if inspect.ismethod(getattr(obj, name))
        ]
        methods.remove('no_rbac')
        self.assertEqual(sorted(rbac_methods(obj)), sorted(methods))

        # Result proxying.
        normal_value = object()
        proxy_value = ProxyRequired()
        dummy = AccessController()
        self.assertFalse(need_proxy(obj.no_rbac, proxy_value, dummy))
        self.assertFalse(need_proxy(obj.single_role, normal_value, dummy))
        self.assertFalse(need_proxy(obj.proxy_result, normal_value, dummy))
        self.assertTrue(need_proxy(obj.proxy_result, proxy_value, dummy))

        # Access checking.
        assert_raises(self, "check_role('owner', obj.no_rbac)", globals(),
                      locals(), RoleError, 'No RBAC for function!')

        assert_raises(self, "check_role('xyzzy', obj.single_role)", globals(),
                      locals(), RoleError, "No access for role 'xyzzy'")

        check_role('owner', obj.multi_role)
        check_role('user', obj.multi_role)
        check_role('xyzzy', obj.role_pattern)