def get_user_ids(self):
     res = (UserES()
            .OR(filters.AND(user_filters.mobile_users(), filters.non_null('location_id')),
                filters.AND(user_filters.web_users(), filters.non_null('domain_memberships.location_id')))
            .exclude_source()
            .run())
     return list(res.doc_ids)
Exemple #2
0
    def user_es_query(cls, domain, request):
        user_ids = cls.selected_user_ids(request)
        user_types = cls.selected_user_types(request)
        group_ids = cls.selected_group_ids(request)

        user_type_filters = []
        if HQUserType.ADMIN in user_types:
            user_type_filters.append(user_es.admin_users())
        if HQUserType.UNKNOWN in user_types:
            user_type_filters.append(user_es.unknown_users())
            user_type_filters.append(user_es.web_users())
        if HQUserType.DEMO_USER in user_types:
            user_type_filters.append(user_es.demo_users())

        q = user_es.UserES().domain(domain)
        if HQUserType.REGISTERED in user_types:
            # return all users with selected user_types
            user_type_filters.append(user_es.mobile_users())
            return q.OR(*user_type_filters)
        else:
            # return matching user types and exact matches
            id_filter = filters.OR(
                filters.term("_id", user_ids),
                filters.term("__group_ids", group_ids),
            )
            if user_type_filters:
                return q.OR(
                    id_filter,
                    filters.OR(*user_type_filters),
                )
            else:
                return q.filter(id_filter)
Exemple #3
0
    def user_es_query(cls, domain, request):
        user_ids = cls.selected_user_ids(request)
        user_types = cls.selected_user_types(request)
        group_ids = cls.selected_group_ids(request)

        user_type_filters = []
        if HQUserType.ADMIN in user_types:
            user_type_filters.append(user_es.admin_users())
        if HQUserType.UNKNOWN in user_types:
            user_type_filters.append(user_es.unknown_users())
            user_type_filters.append(user_es.web_users())
        if HQUserType.DEMO_USER in user_types:
            user_type_filters.append(user_es.demo_users())

        q = user_es.UserES().domain(domain)
        if HQUserType.REGISTERED in user_types:
            # return all users with selected user_types
            user_type_filters.append(user_es.mobile_users())
            return q.OR(*user_type_filters)
        else:
            # return matching user types and exact matches
            id_filter = filters.OR(
                filters.term("_id", user_ids),
                filters.term("__group_ids", group_ids),
            )
            if user_type_filters:
                return q.OR(
                    id_filter,
                    filters.OR(*user_type_filters),
                )
            else:
                return q.filter(id_filter)
Exemple #4
0
 def get_user_ids(self):
     res = (UserES()
            .OR(filters.AND(user_filters.mobile_users(), filters.non_null('location_id')),
                filters.AND(user_filters.web_users(), filters.non_null('domain_memberships.location_id')))
            .exclude_source()
            .run())
     return list(res.doc_ids)
Exemple #5
0
    def case_owners(self):

        # Get user ids for each user that match the demo_user, admin, Unknown Users, or All Mobile Workers filters
        user_types = EMWF.selected_user_types(self.request)
        user_type_filters = []
        if HQUserType.ADMIN in user_types:
            user_type_filters.append(user_es.admin_users())
        if HQUserType.UNKNOWN in user_types:
            user_type_filters.append(user_es.unknown_users())
            user_type_filters.append(user_es.web_users())
        if HQUserType.DEMO_USER in user_types:
            user_type_filters.append(user_es.demo_users())
        if HQUserType.REGISTERED in user_types:
            user_type_filters.append(user_es.mobile_users())

        if len(user_type_filters) > 0:
            special_q = user_es.UserES().domain(self.domain).OR(*user_type_filters).show_inactive()
            special_user_ids = special_q.run().doc_ids
        else:
            special_user_ids = []

        # Get user ids for each user that was specifically selected
        selected_user_ids = EMWF.selected_user_ids(self.request)

        # Get group ids for each group that was specified
        selected_reporting_group_ids = EMWF.selected_reporting_group_ids(self.request)
        selected_sharing_group_ids = EMWF.selected_sharing_group_ids(self.request)

        # Get user ids for each user in specified reporting groups
        report_group_q = HQESQuery(index="groups").domain(self.domain)\
                                           .doc_type("Group")\
                                           .filter(filters.term("_id", selected_reporting_group_ids))\
                                           .fields(["users"])
        user_lists = [group["users"] for group in report_group_q.run().hits]
        selected_reporting_group_users = list(set().union(*user_lists))

        # Get ids for each sharing group that contains a user from selected_reporting_group_users OR a user that was specifically selected
        share_group_q = HQESQuery(index="groups").domain(self.domain)\
                                                .doc_type("Group")\
                                                .filter(filters.term("case_sharing", True))\
                                                .filter(filters.term("users", selected_reporting_group_users+selected_user_ids+special_user_ids))\
                                                .fields([])
        sharing_group_ids = share_group_q.run().doc_ids

        owner_ids = list(set().union(
            special_user_ids,
            selected_user_ids,
            selected_sharing_group_ids,
            selected_reporting_group_users,
            sharing_group_ids
        ))
        if HQUserType.COMMTRACK in user_types:
            owner_ids.append("commtrack-system")
        if HQUserType.DEMO_USER in user_types:
            owner_ids.append("demo_user_group_id")

        owner_ids += self.location_sharing_owner_ids()
        owner_ids += self.location_reporting_owner_ids()
        return owner_ids
Exemple #6
0
 def _users_query(self):
     query = (user_es.UserES().remove_default_filters().OR(
         user_es.web_users(), user_es.mobile_users()))
     if 'search_string' in self.request.GET:
         search_string = self.request.GET['search_string']
         fields = [
             'username', 'first_name', 'last_name', 'phone_numbers',
             'domain_membership.domain', 'domain_memberships.domain'
         ]
         query = query.search_string_query(search_string, fields)
     return query
Exemple #7
0
    def case_owners(self):

        # Get user ids for each user that match the demo_user, admin, Unknown Users, or All Mobile Workers filters
        user_types = EMWF.selected_user_types(self.request)
        user_type_filters = []
        if HQUserType.ADMIN in user_types:
            user_type_filters.append(user_es.admin_users())
        if HQUserType.UNKNOWN in user_types:
            user_type_filters.append(user_es.unknown_users())
            user_type_filters.append(user_es.web_users())
        if HQUserType.DEMO_USER in user_types:
            user_type_filters.append(user_es.demo_users())
        if HQUserType.REGISTERED in user_types:
            user_type_filters.append(user_es.mobile_users())

        if len(user_type_filters) > 0:
            special_q = user_es.UserES().domain(
                self.domain).OR(*user_type_filters)
            special_user_ids = special_q.run().doc_ids
        else:
            special_user_ids = []

        # Get user ids for each user that was specifically selected
        selected_user_ids = EMWF.selected_user_ids(self.request)

        # Get group ids for each group that was specified
        selected_reporting_group_ids = EMWF.selected_reporting_group_ids(
            self.request)
        selected_sharing_group_ids = EMWF.selected_sharing_group_ids(
            self.request)

        # Get user ids for each user in specified reporting groups
        report_group_q = HQESQuery(index="groups").domain(self.domain)\
                                           .doc_type("Group")\
                                           .filter(filters.term("_id", selected_reporting_group_ids))\
                                           .fields(["users"])
        user_lists = [group["users"] for group in report_group_q.run().hits]
        selected_reporting_group_users = list(set().union(*user_lists))

        # Get ids for each sharing group that contains a user from selected_reporting_group_users OR a user that was specifically selected
        share_group_q = HQESQuery(index="groups").domain(self.domain)\
                                                .doc_type("Group")\
                                                .filter(filters.term("case_sharing", True))\
                                                .filter(filters.term("users", selected_reporting_group_users+selected_user_ids+special_user_ids))\
                                                .fields([])
        sharing_group_ids = share_group_q.run().doc_ids

        owner_ids = list(set().union(special_user_ids, selected_user_ids,
                                     selected_sharing_group_ids,
                                     selected_reporting_group_users,
                                     sharing_group_ids))
        if HQUserType.COMMTRACK in EMWF.selected_user_types(self.request):
            owner_ids.append("commtrack-system")
        return owner_ids
 def user_query(self):
     user_query = UserES().domain(self.domain).filter(mobile_users())
     locations_id = self.locations_id
     if locations_id:
         user_query = user_query.location(locations_id)
     elif not self.request.couch_user.has_permission(
             self.domain, 'access_all_locations'):
         # EnikshayLocationFilter.get_value should always return a
         # location_id for restricted users
         raise PermissionDenied()
     return user_query
Exemple #9
0
    def user_es_query(cls, domain, mobile_user_and_group_slugs, request_user):
        # The queryset returned by this method is location-safe
        user_ids = cls.selected_user_ids(mobile_user_and_group_slugs)
        user_types = cls.selected_user_types(mobile_user_and_group_slugs)
        group_ids = cls.selected_group_ids(mobile_user_and_group_slugs)
        location_ids = cls.selected_location_ids(mobile_user_and_group_slugs)

        user_type_filters = []
        if HQUserType.ADMIN in user_types:
            user_type_filters.append(user_es.admin_users())
        if HQUserType.UNKNOWN in user_types:
            user_type_filters.append(user_es.unknown_users())
            user_type_filters.append(user_es.web_users())
        if HQUserType.DEMO_USER in user_types:
            user_type_filters.append(user_es.demo_users())

        q = user_es.UserES().domain(domain)
        if HQUserType.ACTIVE in user_types and HQUserType.DEACTIVATED in user_types:
            q = q.show_inactive()
        elif HQUserType.DEACTIVATED in user_types:
            q = q.show_only_inactive()

        if not request_user.has_permission(domain, 'access_all_locations'):
            cls._verify_users_are_accessible(domain, request_user, user_ids)
            return q.OR(
                filters.term("_id", user_ids),
                user_es.location(list(SQLLocation.active_objects
                                      .get_locations_and_children(location_ids)
                                      .accessible_to_user(domain, request_user)
                                      .location_ids())),
            )
        elif HQUserType.ACTIVE in user_types or HQUserType.DEACTIVATED in user_types:
            # return all users with selected user_types
            user_type_filters.append(user_es.mobile_users())
            return q.OR(*user_type_filters)
        else:
            # return matching user types and exact matches
            location_ids = list(SQLLocation.active_objects
                                .get_locations_and_children(location_ids)
                                .location_ids())
            id_filter = filters.OR(
                filters.term("_id", user_ids),
                filters.term("__group_ids", group_ids),
                user_es.location(location_ids),
            )
            if user_type_filters:
                return q.OR(
                    id_filter,
                    filters.OR(*user_type_filters),
                )
            else:
                return q.filter(id_filter)
Exemple #10
0
    def user_es_query(cls, domain, mobile_user_and_group_slugs):
        user_ids = cls.selected_user_ids(mobile_user_and_group_slugs)
        user_types = cls.selected_user_types(mobile_user_and_group_slugs)
        group_ids = cls.selected_group_ids(mobile_user_and_group_slugs)
        location_ids = cls.selected_location_ids(mobile_user_and_group_slugs)

        user_type_filters = []
        if HQUserType.ADMIN in user_types:
            user_type_filters.append(user_es.admin_users())
        if HQUserType.UNKNOWN in user_types:
            user_type_filters.append(user_es.unknown_users())
            user_type_filters.append(user_es.web_users())
        if HQUserType.DEMO_USER in user_types:
            user_type_filters.append(user_es.demo_users())

        q = user_es.UserES().domain(domain)
        if HQUserType.REGISTERED in user_types:
            # return all users with selected user_types
            user_type_filters.append(user_es.mobile_users())
            return q.OR(*user_type_filters)
        else:
            # return matching user types and exact matches
            location_ids = list(SQLLocation.active_objects
                                .get_locations_and_children(location_ids)
                                .location_ids())
            id_filter = filters.OR(
                filters.term("_id", user_ids),
                filters.term("__group_ids", group_ids),
                user_es.location(location_ids),
            )
            if user_type_filters:
                return q.OR(
                    id_filter,
                    filters.OR(*user_type_filters),
                )
            else:
                return q.filter(id_filter)
Exemple #11
0
 def user_query(self):
     user_query = UserES().domain(self.domain).filter(mobile_users())
     locations_id = self.locations_id
     if locations_id:
         user_query = user_query.location(locations_id)
     return user_query
Exemple #12
0
    def user_es_query(cls, domain, mobile_user_and_group_slugs, request_user):
        # The queryset returned by this method is location-safe
        q = user_es.UserES().domain(domain, allow_mirroring=True)
        if ExpandedMobileWorkerFilter.no_filters_selected(
                mobile_user_and_group_slugs):
            return q.show_inactive()

        user_ids = cls.selected_user_ids(mobile_user_and_group_slugs)
        user_types = cls.selected_user_types(mobile_user_and_group_slugs)
        group_ids = cls.selected_group_ids(mobile_user_and_group_slugs)
        location_ids = cls.selected_location_ids(mobile_user_and_group_slugs)

        user_type_filters = []
        if HQUserType.ADMIN in user_types:
            user_type_filters.append(user_es.admin_users())
        if HQUserType.UNKNOWN in user_types:
            user_type_filters.append(user_es.unknown_users())
        if HQUserType.WEB in user_types:
            user_type_filters.append(user_es.web_users())
        if HQUserType.DEMO_USER in user_types:
            user_type_filters.append(user_es.demo_users())

        if HQUserType.ACTIVE in user_types and HQUserType.DEACTIVATED in user_types:
            q = q.show_inactive()
        elif HQUserType.DEACTIVATED in user_types:
            q = q.show_only_inactive()

        if not request_user.has_permission(domain, 'access_all_locations'):
            cls._verify_users_are_accessible(domain, request_user, user_ids)
            return q.OR(
                filters.term("_id", user_ids),
                user_es.location(
                    list(
                        SQLLocation.active_objects.get_locations_and_children(
                            location_ids).accessible_to_user(
                                domain, request_user).location_ids())),
            )

        if HQUserType.ACTIVE in user_types or HQUserType.DEACTIVATED in user_types:
            # return all users with selected user_types
            user_type_filters.append(user_es.mobile_users())
            return q.OR(*user_type_filters)

        # return matching user types and exact matches
        location_ids = list(
            SQLLocation.active_objects.get_locations_and_children(
                location_ids).location_ids())

        group_id_filter = filters.term("__group_ids", group_ids)

        if FILTER_ON_GROUPS_AND_LOCATIONS.enabled(
                domain) and group_ids and location_ids:
            group_and_location_filter = filters.AND(
                group_id_filter,
                user_es.location(location_ids),
            )
        else:
            group_and_location_filter = filters.OR(
                group_id_filter,
                user_es.location(location_ids),
            )

        id_filter = filters.OR(
            filters.term("_id", user_ids),
            group_and_location_filter,
        )

        if user_type_filters:
            return q.OR(
                id_filter,
                group_and_location_filter,
                filters.OR(*user_type_filters),
            )
        return q.filter(id_filter)
Exemple #13
0
    def user_es_query(cls, domain, mobile_user_and_group_slugs, request_user):
        # The queryset returned by this method is location-safe
        q = cls._base_user_es_query(domain, request_user)
        q = customize_user_query(request_user, domain, q)
        if (
            ExpandedMobileWorkerFilter.no_filters_selected(mobile_user_and_group_slugs)
            and request_user.has_permission(domain, 'access_all_locations')
        ):
            return q.show_inactive()

        user_ids = cls.selected_user_ids(mobile_user_and_group_slugs)
        user_types = cls.selected_user_types(mobile_user_and_group_slugs)
        group_ids = cls.selected_group_ids(mobile_user_and_group_slugs)
        location_ids = cls.selected_location_ids(mobile_user_and_group_slugs)

        user_type_filters = []
        has_user_ids = bool(user_ids)

        if has_user_ids:
            # if userid are passed then remove default active filter
            # and move it with mobile worker filter
            q = q.remove_default_filter('active')
            has_user_ids = True
            if HQUserType.DEACTIVATED in user_types:
                deactivated_mbwf = filters.AND(user_es.is_active(False), user_es.mobile_users())
                user_type_filters.append(deactivated_mbwf)
            if HQUserType.ACTIVE in user_types:
                activated_mbwf = filters.AND(user_es.is_active(), user_es.mobile_users())
                user_type_filters.append(activated_mbwf)
        elif HQUserType.ACTIVE in user_types and HQUserType.DEACTIVATED in user_types:
            q = q.show_inactive()
        elif HQUserType.DEACTIVATED in user_types:
            q = q.show_only_inactive()

        if HQUserType.ADMIN in user_types:
            user_type_filters.append(user_es.admin_users())
        if HQUserType.UNKNOWN in user_types:
            user_type_filters.append(user_es.unknown_users())
        if HQUserType.WEB in user_types:
            user_type_filters.append(user_es.web_users())
        if HQUserType.DEMO_USER in user_types:
            user_type_filters.append(user_es.demo_users())

        if not request_user.has_permission(domain, 'access_all_locations'):
            cls._verify_users_are_accessible(domain, request_user, user_ids)
            return q.OR(
                filters.term("_id", user_ids),
                user_es.location(list(SQLLocation.active_objects
                                      .get_locations_and_children(location_ids)
                                      .accessible_to_user(domain, request_user)
                                      .location_ids())),
            )

        if HQUserType.ACTIVE in user_types or HQUserType.DEACTIVATED in user_types:
            if has_user_ids:
                return q.OR(*user_type_filters, filters.OR(filters.term("_id", user_ids)))
            else:
                return q.OR(*user_type_filters, user_es.mobile_users())

        # return matching user types and exact matches
        location_ids = list(SQLLocation.active_objects
                            .get_locations_and_children(location_ids)
                            .location_ids())

        group_id_filter = filters.term("__group_ids", group_ids)

        if FILTER_ON_GROUPS_AND_LOCATIONS.enabled(domain) and group_ids and location_ids:
            group_and_location_filter = filters.AND(
                group_id_filter,
                user_es.location(location_ids),
            )
        else:
            group_and_location_filter = filters.OR(
                group_id_filter,
                user_es.location(location_ids),
            )

        id_filter = filters.OR(
            filters.term("_id", user_ids),
            group_and_location_filter,
        )

        if user_type_filters:
            return q.OR(
                id_filter,
                group_and_location_filter,
                filters.OR(*user_type_filters),
            )
        return q.filter(id_filter)
Exemple #14
0
    def user_es_query(cls, domain, mobile_user_and_group_slugs, request_user):
        # The queryset returned by this method is location-safe
        q = user_es.UserES().domain(domain)
        if ExpandedMobileWorkerFilter.no_filters_selected(mobile_user_and_group_slugs):
            return q.show_inactive()

        user_ids = cls.selected_user_ids(mobile_user_and_group_slugs)
        user_types = cls.selected_user_types(mobile_user_and_group_slugs)
        group_ids = cls.selected_group_ids(mobile_user_and_group_slugs)
        location_ids = cls.selected_location_ids(mobile_user_and_group_slugs)

        user_type_filters = []
        if HQUserType.ADMIN in user_types:
            user_type_filters.append(user_es.admin_users())
        if HQUserType.UNKNOWN in user_types:
            user_type_filters.append(user_es.unknown_users())
        if HQUserType.WEB in user_types:
            user_type_filters.append(user_es.web_users())
        if HQUserType.DEMO_USER in user_types:
            user_type_filters.append(user_es.demo_users())

        if HQUserType.ACTIVE in user_types and HQUserType.DEACTIVATED in user_types:
            q = q.show_inactive()
        elif HQUserType.DEACTIVATED in user_types:
            q = q.show_only_inactive()

        if not request_user.has_permission(domain, 'access_all_locations'):
            cls._verify_users_are_accessible(domain, request_user, user_ids)
            return q.OR(
                filters.term("_id", user_ids),
                user_es.location(list(SQLLocation.active_objects
                                      .get_locations_and_children(location_ids)
                                      .accessible_to_user(domain, request_user)
                                      .location_ids())),
            )

        if HQUserType.ACTIVE in user_types or HQUserType.DEACTIVATED in user_types:
            # return all users with selected user_types
            user_type_filters.append(user_es.mobile_users())
            return q.OR(*user_type_filters)

        # return matching user types and exact matches
        location_ids = list(SQLLocation.active_objects
                            .get_locations_and_children(location_ids)
                            .location_ids())

        group_id_filter = filters.term("__group_ids", group_ids)

        if FILTER_ON_GROUPS_AND_LOCATIONS.enabled(domain) and group_ids and location_ids:
            group_and_location_filter = filters.AND(
                group_id_filter,
                user_es.location(location_ids),
            )
        else:
            group_and_location_filter = filters.OR(
                group_id_filter,
                user_es.location(location_ids),
            )

        id_filter = filters.OR(
            filters.term("_id", user_ids),
            group_and_location_filter,
        )

        if user_type_filters:
            return q.OR(
                id_filter,
                group_and_location_filter,
                filters.OR(*user_type_filters),
            )
        return q.filter(id_filter)