Beispiel #1
0
    def _buildQuery(self, types, paths, depth, query, filterPermissions):
        qs = []
        if query is not None:
            qs.append(query)

        # Build the path query
        if not paths:
            paths = ('/'.join(self.context.getPhysicalPath()), )

        q = {'query': paths}
        if depth is not None:
            q['depth'] = depth
        pathq = Generic('path', q)
        qs.append(pathq)

        # Build the type query
        if not isinstance(types, (tuple, list)):
            types = (types, )
        subqs = [Eq('objectImplements', dottedname(t)) for t in types]
        if subqs:
            # Don't unnecessarily nest in an Or if there is only one type query
            typeq = subqs[0] if len(subqs) == 1 else Or(*subqs)
            qs.append(typeq)

        # filter based on permissions
        if filterPermissions:
            qs.append(
                In('allowedRolesAndUsers',
                   allowedRolesAndGroups(self.context)))

        # Consolidate into one query
        return And(*qs)
Beispiel #2
0
    def _buildQuery(self, types, paths, depth, query, filterPermissions):
        qs = []
        if query is not None:
            qs.append(query)

        # Build the path query
        if not paths:
            paths = ('/'.join(self.context.getPhysicalPath()),)

        q = {'query':paths}
        if depth is not None:
            q['depth'] = depth
        pathq = Generic('path', q)
        qs.append(pathq)

        # Build the type query
        if not isinstance(types, (tuple, list)):
            types = (types,)
        subqs = [Eq('objectImplements', dottedname(t)) for t in types]
        if subqs:
            # Don't unnecessarily nest in an Or if there is only one type query
            typeq = subqs[0] if len(subqs) == 1 else Or(*subqs)
            qs.append(typeq)

        # filter based on permissions
        if filterPermissions:
            qs.append(In('allowedRolesAndUsers', allowedRolesAndGroups(self.context)))

        # Consolidate into one query
        return And(*qs)
    def doMySearch( self, keywords, unrestricted=False ):

        def listMatchGlob(op, index, list):
            return op(*[ MatchGlob(index, '*%s*' % i ) for i in list ])
        
        full_query = listMatchGlob(And, 'searchKeywords', keywords)

        querySet = full_query
        if not unrestricted:
            # take permissions into account
            roles = In('allowedRolesAndUsers', allowedRolesAndGroups(self._dmd))
            querySet = [full_query, roles]
            querySet = And(*querySet)
            
        catalogItems = self._dmd.global_catalog.evalAdvancedQuery(querySet)
        brainResults = [DeviceSearchResult(catalogItem)
                        for catalogItem in catalogItems
                        if catalogItem.searchExcerpt is not None]
        return brainResults
    def _build_query(self,
                     types=(),
                     paths=(),
                     depth=None,
                     query=None,
                     filterPermissions=True,
                     globFilters=None):
        """
        Build and AdvancedQuery query

        @params types: list/tuple of values for objectImplements field
        @params globFilters: dict with user passed field: value filters
        @params query: AdvancedQuery passed by the user. Most of the time None
        @param filterPermissions: Boolean indicating whether to check for user perms or not

        @return: tuple (AdvancedQuery query, not indexed filters dict)
        """
        indexed, stored, _ = self.model_catalog_client.get_indexes()
        not_indexed_user_filters = {}  # Filters that use not indexed fields

        user_filters_query = None
        types_query = None
        paths_query = None
        permissions_query = None

        partial_queries = []
        if query:
            """
            # if query is a dict, we convert it to AdvancedQuery
            # @TODO We should make the default query something other than AdvancedQuery
            subqueries = []
            if isinstance(query, dict):
                for attr, value in query.iteritems():
                    if isinstance(value, str) and '*' in value:
                        subqueries.append(MatchGlob(attr, value))
                    else:
                        subqueries.append(Eq(attr, value))
                query = And(*subqueries)
            partial_queries.append(query)
            """
            partial_queries.append(self._parse_user_query(query))

        # Build query from filters passed by user
        if globFilters:
            for key, value in globFilters.iteritems():
                if key in indexed:
                    if user_filters_query:
                        user_filters_query = And(user_filters_query,
                                                 MatchRegexp(key, value))
                    else:
                        user_filters_query = MatchRegexp(key, value)
                else:
                    not_indexed_user_filters[key] = value

        if user_filters_query:
            partial_queries.append(user_filters_query)

        # Build the objectImplements query
        if not isinstance(types, (tuple, list)):
            types = (types, )
        types_query_list = [
            Eq('objectImplements', dottedname(t)) for t in types
        ]
        if types_query_list:
            if len(types_query_list) > 1:
                types_query = Or(*types_query_list)
            else:
                types_query = types_query_list[0]

            partial_queries.append(types_query)

        # Build query for paths
        if paths is not False:  # When paths is False we dont add any path condition
            # TODO: Account for depth or get rid of it
            # TODO: Consider indexing the device's uid as a path
            context_path = '/'.join(self.context.getPrimaryPath())
            uid_path_query = In(
                'path', (context_path, )
            )  # MatchGlob(UID, context_path)   # Add the context uid as filter
            partial_queries.append(uid_path_query)
            if paths:
                if isinstance(paths, basestring):
                    paths = (paths, )
                partial_queries.append(In('path', paths))
            """  OLD CODE. Why this instead of In?  What do we need depth for?
            q = {'query':paths}
            if depth is not None:
                q['depth'] = depth
            paths_query = Generic('path', q)
            """

        # filter based on permissions
        if filterPermissions and allowedRolesAndGroups(self.context):
            permissions_query = In('allowedRolesAndUsers',
                                   allowedRolesAndGroups(self.context))
            partial_queries.append(permissions_query)

        # Put together all queries
        search_query = And(*partial_queries)
        return (search_query, not_indexed_user_filters)
Beispiel #5
0
def rolesgroups(self):
    from Products.Zuul.utils import allowedRolesAndGroups
    self.log.info(allowedRolesAndGroups(self.dmd))
    def _build_query(self, types=(), paths=(), depth=None, query=None, filterPermissions=True, globFilters=None):
        """
        Build and AdvancedQuery query

        @params types: list/tuple of values for objectImplements field
        @params globFilters: dict with user passed field: value filters
        @params query: AdvancedQuery passed by the user. Most of the time None
        @param filterPermissions: Boolean indicating whether to check for user perms or not

        @return: tuple (AdvancedQuery query, not indexed filters dict)
        """
        indexed, stored, _ = self.model_catalog_client.get_indexes()
        not_indexed_user_filters = {} # Filters that use not indexed fields

        user_filters_query = None
        types_query = None
        paths_query = None
        permissions_query = None

        partial_queries = []
        if query:
            """
            # if query is a dict, we convert it to AdvancedQuery
            # @TODO We should make the default query something other than AdvancedQuery
            subqueries = []
            if isinstance(query, dict):
                for attr, value in query.iteritems():
                    if isinstance(value, str) and '*' in value:
                        subqueries.append(MatchGlob(attr, value))
                    else:
                        subqueries.append(Eq(attr, value))
                query = And(*subqueries)
            partial_queries.append(query)
            """
            partial_queries.append(self._parse_user_query(query))

        # Build query from filters passed by user
        if globFilters:
            for key, value in globFilters.iteritems():
                if key in indexed:
                    if user_filters_query:
                        user_filters_query = And(user_filters_query, MatchRegexp(key, value))
                    else:
                        user_filters_query = MatchRegexp(key, value)
                else:
                    not_indexed_user_filters[key] = value

        if user_filters_query:
            partial_queries.append(user_filters_query)

        # Build the objectImplements query
        if not isinstance(types, (tuple, list)):
            types = (types,)
        types_query_list = [ Eq('objectImplements', dottedname(t)) for t in types ]
        if types_query_list:
            if len(types_query_list) > 1:
                types_query = Or(*types_query_list)
            else:
                types_query = types_query_list[0]

            partial_queries.append(types_query)

        # Build query for paths
        if paths is not False:   # When paths is False we dont add any path condition
            # TODO: Account for depth or get rid of it
            # TODO: Consider indexing the device's uid as a path
            context_path = '/'.join(self.context.getPrimaryPath())
            uid_path_query = In('path', (context_path,)) # MatchGlob(UID, context_path)   # Add the context uid as filter
            partial_queries.append( uid_path_query )
            if paths:
                if isinstance(paths, basestring):
                    paths = (paths,)
                partial_queries.append( In('path', paths) )

            """  OLD CODE. Why this instead of In?  What do we need depth for?
            q = {'query':paths}
            if depth is not None:
                q['depth'] = depth
            paths_query = Generic('path', q)
            """

        # filter based on permissions
        if filterPermissions and allowedRolesAndGroups(self.context):
            permissions_query = In('allowedRolesAndUsers', allowedRolesAndGroups(self.context))
            partial_queries.append(permissions_query)

        # Put together all queries
        search_query = And(*partial_queries)
        return (search_query, not_indexed_user_filters)