def processGateways(self, permissions, provider, encoder): ''' Process the gateways for the provided permissions. @param permissions: Iterable(PermissionResource) The permissions to create the gateways for. @param provider: callable|None The callable used in solving the authenticated values. @param encoder: IEncoderPath The encoder path to be used for the gateways resource paths and patterns. @return: list[Gateway] The created gateways objects. ''' assert isinstance(permissions, Iterable), 'Invalid permissions %s' % permissions assert isinstance(encoder, IEncoderPath), 'Invalid encoder path %s' % encoder gateways, gatewaysByPattern = [], {} for permission in permissions: assert isinstance(permission, PermissionResource), 'Invalid permission resource %s' % permission if PermissionResource.values in permission: values = permission.values else: values = None pattern, types = processPattern(permission.path, permission.invoker, encoder, values) filters = self.processFilters(types, permission.filters, provider, encoder) if PermissionResource.putHeaders in permission and permission.putHeaders is not None: putHeaders = [self.separatorHeader.join(item) for item in permission.putHeaders.items()] else: putHeaders = None if PermissionResource.navigate in permission: navigate = permission.navigate else: navigate = None gateway = gatewaysByPattern.get(pattern) if gateway and gateway.Filters == filters and gateway.PutHeaders == putHeaders and gateway.Navigate == navigate: gateway.Methods.append(TO_HTTP_METHOD[permission.method]) else: gateway = Gateway() gateway.Methods = [TO_HTTP_METHOD[permission.method]] gateway.Pattern = pattern if filters: gateway.Filters = filters if putHeaders: gateway.PutHeaders = putHeaders if navigate: gateway.Navigate = navigate gatewaysByPattern[pattern] = gateway gateways.append(gateway) return gateways
def iterateGateways(self, permissions, rootURI=None, replacements=None): ''' Iterate the gateways for permissions. ''' assert isinstance(permissions, Iterable), 'Invalid permissions %s' % permissions permissions = sorted(permissions, key=lambda perm: perm.access.Path) permissions.sort(key=lambda perm: perm.access.Priority) for perm in permissions: assert isinstance(perm, Permission), 'Invalid permission %s' % perm assert isinstance(perm.access, Access), 'Invalid permission access %s' % perm.access assert isinstance(perm.filters, dict), 'Invalid permission filters %s' % perm.filters assert isinstance(perm.access.Path, str), 'Invalid access path %s' % perm.access.Path pattern = '%s[\\/]?(?:\\.|$)' % '([^\\/]+)'.join(re.escape(pitem) for pitem in perm.access.Path.split('*')) if rootURI: assert isinstance(rootURI, str), 'Invalid root URI %s' % rootURI pattern = '%s\/%s' % (re.escape(rootURI), pattern) gateway = Gateway() gateway.Pattern = '^%s' % pattern gateway.Methods = [perm.access.Method] filtersEntry = filtersProperty = None for pathsEntry, pathsProperty in perm.filters.values(): assert isinstance(pathsEntry, dict), 'Invalid indexed path entries %s' % pathsEntry # If there is a group with no filters will automatically cancel all other groups filters. if filtersEntry is None: filtersEntry = pathsEntry elif filtersEntry: nfilters = {} for position, paths in pathsEntry.items(): assert isinstance(paths, set), 'Invalid indexed paths %s' % paths cpaths = filtersEntry.get(position) if cpaths: paths.update(cpaths) nfilters[position] = paths filtersEntry = nfilters if filtersProperty is None: filtersProperty = pathsProperty elif filtersProperty: nfilters = {} for name, paths in pathsProperty.items(): assert isinstance(paths, set), 'Invalid indexed paths %s' % paths cpaths = filtersProperty.get(name) if cpaths: paths.update(cpaths) nfilters[position] = paths filtersProperty = nfilters if not filtersEntry and not filtersProperty: break # There are no more filters to process. if filtersEntry: for position in sorted(filtersEntry): for path in sorted(filtersEntry[position]): if gateway.Filters is None: gateway.Filters = [] assert isinstance(path, str), 'Invalid path %s' % path if replacements: path = path % replacements if rootURI: path = '%s/%s' % (rootURI, path) gateway.Filters.append('%s:%s' % (position, path)) if filtersProperty: values = [PROPERTY_NAME] for name in sorted(filtersProperty): paths = sorted(filtersProperty[name]) if rootURI: for k, path in enumerate(paths): if replacements: path = path % replacements paths[k] = '%s/%s' % (rootURI, path) values.append('%s=%s' % (name, '|'.join(paths))) if gateway.PutHeaders is None: gateway.PutHeaders = {} gateway.PutHeaders[HEADER_FILTER_INPUT] = ';'.join(values) if Permission.navigate in perm and perm.navigate: if replacements: path = perm.navigate % replacements else: path = perm.navigate if rootURI: path = '%s/%s' % (rootURI, path) gateway.Navigate = path yield gateway
def iterateGateways(self, permissions, rootURI=None, replacements=None): ''' Iterate the gateways for permissions. ''' assert isinstance(permissions, Iterable), 'Invalid permissions %s' % permissions permissions = sorted(permissions, key=lambda perm: perm.access.Path) permissions.sort(key=lambda perm: perm.access.Priority) for perm in permissions: assert isinstance(perm, Permission), 'Invalid permission %s' % perm assert isinstance( perm.access, Access), 'Invalid permission access %s' % perm.access assert isinstance( perm.filters, dict), 'Invalid permission filters %s' % perm.filters assert isinstance(perm.access.Path, str), 'Invalid access path %s' % perm.access.Path pattern = '%s[\\/]?(?:\\.|$)' % '([^\\/]+)'.join( re.escape(pitem) for pitem in perm.access.Path.split('*')) if rootURI: assert isinstance(rootURI, str), 'Invalid root URI %s' % rootURI pattern = '%s\/%s' % (re.escape(rootURI), pattern) gateway = Gateway() gateway.Pattern = '^%s' % pattern gateway.Methods = [perm.access.Method] filtersEntry = filtersProperty = None for pathsEntry, pathsProperty in perm.filters.values(): assert isinstance( pathsEntry, dict), 'Invalid indexed path entries %s' % pathsEntry # If there is a group with no filters will automatically cancel all other groups filters. if filtersEntry is None: filtersEntry = pathsEntry elif filtersEntry: nfilters = {} for position, paths in pathsEntry.items(): assert isinstance( paths, set), 'Invalid indexed paths %s' % paths cpaths = filtersEntry.get(position) if cpaths: paths.update(cpaths) nfilters[position] = paths filtersEntry = nfilters if filtersProperty is None: filtersProperty = pathsProperty elif filtersProperty: nfilters = {} for name, paths in pathsProperty.items(): assert isinstance( paths, set), 'Invalid indexed paths %s' % paths cpaths = filtersProperty.get(name) if cpaths: paths.update(cpaths) nfilters[position] = paths filtersProperty = nfilters if not filtersEntry and not filtersProperty: break # There are no more filters to process. if filtersEntry: for position in sorted(filtersEntry): for path in sorted(filtersEntry[position]): if gateway.Filters is None: gateway.Filters = [] assert isinstance(path, str), 'Invalid path %s' % path if replacements: path = path % replacements if rootURI: path = '%s/%s' % (rootURI, path) gateway.Filters.append('%s:%s' % (position, path)) if filtersProperty: values = [PROPERTY_NAME] for name in sorted(filtersProperty): paths = sorted(filtersProperty[name]) if rootURI: for k, path in enumerate(paths): if replacements: path = path % replacements paths[k] = '%s/%s' % (rootURI, path) values.append('%s=%s' % (name, '|'.join(paths))) if gateway.PutHeaders is None: gateway.PutHeaders = {} gateway.PutHeaders[HEADER_FILTER_INPUT] = ';'.join(values) if Permission.navigate in perm and perm.navigate: if replacements: path = perm.navigate % replacements else: path = perm.navigate if rootURI: path = '%s/%s' % (rootURI, path) gateway.Navigate = path yield gateway
def processGateways(self, permissions, provider, encoder): ''' Process the gateways for the provided permissions. @param permissions: Iterable(PermissionResource) The permissions to create the gateways for. @param provider: callable|None The callable used in solving the authenticated values. @param encoder: IEncoderPath The encoder path to be used for the gateways resource paths and patterns. @return: list[Gateway] The created gateways objects. ''' assert isinstance(permissions, Iterable), 'Invalid permissions %s' % permissions assert isinstance(encoder, IEncoderPath), 'Invalid encoder path %s' % encoder gateways, gatewaysByPattern = [], {} for permission in permissions: assert isinstance(permission, PermissionResource ), 'Invalid permission resource %s' % permission if PermissionResource.values in permission: values = permission.values else: values = None pattern, types = processPattern(permission.path, permission.invoker, encoder, values) filters = self.processFilters(types, permission.filters, provider, encoder) if PermissionResource.putHeaders in permission and permission.putHeaders is not None: putHeaders = [ self.separatorHeader.join(item) for item in permission.putHeaders.items() ] else: putHeaders = None if PermissionResource.navigate in permission: navigate = permission.navigate else: navigate = None gateway = gatewaysByPattern.get(pattern) if gateway and gateway.Filters == filters and gateway.PutHeaders == putHeaders and gateway.Navigate == navigate: gateway.Methods.append(TO_HTTP_METHOD[permission.method]) else: gateway = Gateway() gateway.Methods = [TO_HTTP_METHOD[permission.method]] gateway.Pattern = pattern if filters: gateway.Filters = filters if putHeaders: gateway.PutHeaders = putHeaders if navigate: gateway.Navigate = navigate gatewaysByPattern[pattern] = gateway gateways.append(gateway) return gateways