Example #1
0
    def get_projects_with_rights(self, username, action):
        """
        :returns: a list of projects where user have right for "action".

        .. note::

           Permissions coming via LDAP groups are not included in the results

        """
        user = get_userstore().getUser(username)

        # Get subjects
        subjects = set([username])
        subjects.update(get_special_users(username))

        # Surround string elements with ' and join them with comma
        actions_str = ','.join("'%s'" % safe_string(p) for p in [action, 'TRAC_ADMIN'])
        subjects_str = ','.join(["'{0}'".format(safe_string(subject)) for subject in subjects])
        organizations_str = ','.join(["{0}".format(safe_int(org_key)) for org_key in user.organization_keys])

        query = ("SELECT DISTINCT projects.* FROM projects "
                 "INNER JOIN `group` ON group.trac_environment_key = projects.trac_environment_key "
                 "INNER JOIN group_permission ON group_permission.group_key = group.group_id "
                 "INNER JOIN action ON group_permission.permission_key = action.action_id "
                 "LEFT JOIN user_group ON user_group.group_key = group.group_id "
                 "LEFT JOIN user ON user.user_id = user_group.user_key "
                 "LEFT JOIN organization_group ON organization_group.group_key = group.group_id "
                 "WHERE (user.username IN(%s) "
                 "OR organization_group.organization_key IN(%s)) "
                 "AND action.action_string IN(%s) "
                 "ORDER BY projects.project_name" % (subjects_str, organizations_str, actions_str))

        return self.queryProjectObjects(query)
Example #2
0
    def get_projects_with_params(self, username, perm, namelike=None, categories=None):
        """ Returns a list of projects where user have right for "action".
        """
        categories = categories or []
        user = get_userstore().getUser(username)
        user_organization = user.organization_keys

        # Get subjects
        subjects = set([username])
        subjects.update(get_special_users(username))

        # Construct comma separated lists for queries
        actions_str = ','.join("'%s'" % safe_string(p) for p in [perm, 'TRAC_ADMIN'])
        subjects_str = ','.join("'%s'" % safe_string(subject) for subject in subjects)
        categories_str = ','.join("'%s'" % safe_string(cat) for cat in categories)

        query = ("SELECT DISTINCT projects.* FROM projects "
                 "INNER JOIN `group` ON group.trac_environment_key = projects.trac_environment_key "
                 "INNER JOIN group_permission ON group_permission.group_key = group.group_id "
                 "INNER JOIN action ON group_permission.permission_key = action.action_id "
                 "LEFT JOIN user_group ON user_group.group_key = group.group_id "
                 "LEFT JOIN user ON user.user_id = user_group.user_key ")

        if categories:
            query += ("INNER JOIN project_categories ON projects.project_id = project_categories.project_key "
                      "INNER JOIN categories ON categories.category_id = project_categories.category_key ")

        query += ("LEFT JOIN organization_group ON organization_group.group_key = group.group_id "
                  "WHERE (user.username IN (%s) " % subjects_str)

        if not user_organization:
            query += "OR organization_group.organization_key = NULL )"
        else:
            # List user organizations as safe int, separated with comma: (1,5,3,65)
            orc = lambda org_key: str(safe_int(org_key))
            query += "OR organization_group.organization_key IN (%s) ) " % ','.join(map(orc, user_organization))

        query += "AND action.action_string IN(" + actions_str + ") "

        if categories:
            query += "AND categories.category_name IN(" + categories_str + ") "

        if namelike:
            conditions = []
            search_strs = namelike.split(' ')
            for search in search_strs:
                if not search == '':
                    search = safe_string(search)
                    conditions.append("projects.project_name LIKE '%" + search + "%'")

            where = " OR ".join(conditions)
            query += "AND (" + where + ") "
        query += "ORDER BY projects.project_name"
        return self.queryProjectObjects(query)
Example #3
0
    def get_projects_with_params(self, username, perm, namelike=None, categories=None):
        """
        :returns: a list of archived projects where user have right for permission (action).
        """
        categories = categories or []
        user = get_userstore().getUser(username)
        user_organization = user.organization_keys

        # Get subjects
        subjects = set([username])
        subjects.update(get_special_users(username))

        # Construct comma separated lists for queries
        actions_str = ",".join("'%s'" % safe_string(p) for p in [perm, "TRAC_ADMIN"])
        subjects_str = ",".join("'%s'" % safe_string(subject) for subject in subjects)
        categories_str = ",".join("'%s'" % safe_string(cat) for cat in categories)

        query = """
        SELECT DISTINCT pa.* FROM project_archive AS pa
        INNER JOIN `group` ON group.trac_environment_key = (
            SELECT environment_id
            FROM trac_environment
            WHERE identifier = pa.environment_name
        )
        INNER JOIN group_permission ON group_permission.group_key = group.group_id
        INNER JOIN action ON group_permission.permission_key = action.action_id
        LEFT JOIN user_group ON user_group.group_key = group.group_id
        LEFT JOIN user ON user.user_id = user_group.user_key
        """

        # NOTE! When project is archived/removed, the category information is also removed
        if categories:
            query += (
                "INNER JOIN project_categories ON pa.orig_project_id = project_categories.project_key "
                "INNER JOIN categories ON categories.category_id = project_categories.category_key "
            )

        query += (
            "LEFT JOIN organization_group ON organization_group.group_key = group.group_id "
            "WHERE (user.username IN (%s) " % subjects_str
        )

        if not user_organization:
            query += "OR organization_group.organization_key = NULL )"
        else:
            # List user organizations as safe int, separated with comma: (1,5,3,65)
            orc = lambda org_key: str(safe_int(org_key))
            query += "OR organization_group.organization_key IN (%s) ) " % ",".join(map(orc, user_organization))

        query += "AND action.action_string IN(" + actions_str + ") "

        if categories:
            query += "AND categories.category_name IN(" + categories_str + ") "

        if namelike:
            conditions = []
            search_strs = namelike.split(" ")
            for search in search_strs:
                if not search == "":
                    search = safe_string(search)
                    conditions.append("pa.project_name LIKE '%" + search + "%'")

            where = " OR ".join(conditions)
            query += "AND (" + where + ") "
        query += "ORDER BY pa.project_name"

        return self.query_archived_projects(query)
Example #4
0
    def get_projects_with_params(self,
                                 username,
                                 perm,
                                 namelike=None,
                                 categories=None):
        """
        :returns: a list of archived projects where user have right for permission (action).
        """
        categories = categories or []
        user = get_userstore().getUser(username)
        user_organization = user.organization_keys

        # Get subjects
        subjects = set([username])
        subjects.update(get_special_users(username))

        # Construct comma separated lists for queries
        actions_str = ','.join("'%s'" % safe_string(p)
                               for p in [perm, 'TRAC_ADMIN'])
        subjects_str = ','.join("'%s'" % safe_string(subject)
                                for subject in subjects)
        categories_str = ','.join("'%s'" % safe_string(cat)
                                  for cat in categories)

        query = """
        SELECT DISTINCT pa.* FROM project_archive AS pa
        INNER JOIN `group` ON group.trac_environment_key = (
            SELECT environment_id
            FROM trac_environment
            WHERE identifier = pa.environment_name
        )
        INNER JOIN group_permission ON group_permission.group_key = group.group_id
        INNER JOIN action ON group_permission.permission_key = action.action_id
        LEFT JOIN user_group ON user_group.group_key = group.group_id
        LEFT JOIN user ON user.user_id = user_group.user_key
        """

        # NOTE! When project is archived/removed, the category information is also removed
        if categories:
            query += (
                "INNER JOIN project_categories ON pa.orig_project_id = project_categories.project_key "
                "INNER JOIN categories ON categories.category_id = project_categories.category_key "
            )

        query += (
            "LEFT JOIN organization_group ON organization_group.group_key = group.group_id "
            "WHERE (user.username IN (%s) " % subjects_str)

        if not user_organization:
            query += "OR organization_group.organization_key = NULL )"
        else:
            # List user organizations as safe int, separated with comma: (1,5,3,65)
            orc = lambda org_key: str(safe_int(org_key))
            query += "OR organization_group.organization_key IN (%s) ) " % ','.join(
                map(orc, user_organization))

        query += "AND action.action_string IN(" + actions_str + ") "

        if categories:
            query += "AND categories.category_name IN(" + categories_str + ") "

        if namelike:
            conditions = []
            search_strs = namelike.split(' ')
            for search in search_strs:
                if not search == '':
                    search = safe_string(search)
                    conditions.append("pa.project_name LIKE '%" + search +
                                      "%'")

            where = " OR ".join(conditions)
            query += "AND (" + where + ") "
        query += "ORDER BY pa.project_name"

        return self.query_archived_projects(query)