Beispiel #1
0
def get_enterprise_links_for_dropdown(couch_user, view_name="domain_homepage"):
    # Returns dicts with keys 'name', 'display_name', and 'url'
    from corehq.apps.enterprise.models import EnterprisePermissions
    domain_links_by_name = {
        d['name']: d
        for d in get_domain_links_for_dropdown(couch_user)
    }
    subdomain_objects_by_name = {}
    for domain_name in domain_links_by_name:
        for subdomain in EnterprisePermissions.get_domains(domain_name):
            if subdomain not in domain_links_by_name:
                subdomain_objects_by_name[subdomain] = Domain.get_by_name(
                    subdomain)

    return _domains_to_links(subdomain_objects_by_name.values(), view_name)
Beispiel #2
0
    def _get_relevant_domains(cls, couch_user, domain=None):
        domains = set()

        # Include current domain, which user may not be a member of
        if domain:
            domains.add(domain)

        if not couch_user:
            return domains

        domains = domains | set(couch_user.get_domains())

        from corehq.apps.enterprise.models import EnterprisePermissions
        subdomains = set()
        for domain in domains:
            subdomains = subdomains | set(
                EnterprisePermissions.get_domains(domain))

        return domains | subdomains
Beispiel #3
0
def do_import(spreadsheet,
              config,
              domain,
              task=None,
              record_form_callback=None):
    has_domain_column = 'domain' in [
        c.lower() for c in spreadsheet.get_header_columns()
    ]
    if has_domain_column and DOMAIN_PERMISSIONS_MIRROR.enabled(domain):
        allowed_domains = EnterprisePermissions.get_domains(domain)
        sub_domains = set()
        import_results = _ImportResults()
        for row_num, row in enumerate(spreadsheet.iter_row_dicts(), start=1):
            if row_num == 1:
                continue  # skip first row (header row)
            sheet_domain = row.get('domain')
            if sheet_domain != domain and sheet_domain not in allowed_domains:
                err = exceptions.CaseRowError(column_name='domain')
                err.title = _('Invalid domain')
                err.message = _(
                    'Following rows contain invalid value for domain column.')
                import_results.add_error(row_num, err)
            else:
                sub_domains.add(sheet_domain)
        for sub_domain in sub_domains:
            importer = _TimedAndThrottledImporter(sub_domain,
                                                  config,
                                                  task,
                                                  record_form_callback,
                                                  import_results,
                                                  multi_domain=True)
            importer.do_import(spreadsheet)
        return import_results.to_json()
    else:
        importer = _TimedAndThrottledImporter(domain,
                                              config,
                                              task,
                                              record_form_callback,
                                              multi_domain=False)
        return importer.do_import(spreadsheet)
Beispiel #4
0
    def _base_user_es_query(cls, domain, request_user):
        if not request_user.has_permission(domain, 'access_all_locations'):
            return super()._base_user_es_query(domain, request_user)

        domains = list(set(EnterprisePermissions.get_domains(domain)) | {domain})
        return user_es.UserES().domains(domains)
Beispiel #5
0
 def __init__(self, request, domain, search):
     super().__init__(request, domain, search, False)
     # this will include all domains in the enterprise only if the current domain
     # is the 'source'
     self.enterprise_domains = list(
         set(EnterprisePermissions.get_domains(domain)) | {domain})
Beispiel #6
0
    def post(self, request, *args, **kwargs):
        try:
            data = json.loads(request.body.decode('utf-8'))
        except ValueError:
            return HttpResponseBadRequest()

        if not data or not isinstance(data, dict):
            return HttpResponseBadRequest()

        session_id = data.get('sessionId', None)
        if not session_id:
            return HttpResponseBadRequest()

        session = get_session(session_id)
        user = get_django_user_from_session(session)
        if user:
            couch_user = CouchUser.get_by_username(user.username)
            if not couch_user:
                raise Http404
        else:
            raise Http404

        domain = data.get('domain')
        if domain and toggles.DISABLE_WEB_APPS.enabled(domain):
            return HttpResponse('Service Temporarily Unavailable',
                                content_type='text/plain',
                                status=503)

        # reset the session's expiry if there's some formplayer activity
        secure_session = session.get('secure_session')
        TimeoutMiddleware.update_secure_session(session,
                                                secure_session,
                                                couch_user,
                                                domain=domain)
        session.save()

        domains = set()
        for member_domain in couch_user.domains:
            domains.add(member_domain)
            domains.update(EnterprisePermissions.get_domains(member_domain))

        enabled_toggles = toggles_enabled_for_user(
            user.username) | toggles_enabled_for_domain(domain)
        return JsonResponse({
            'username':
            user.username,
            'djangoUserId':
            user.pk,
            'superUser':
            user.is_superuser,
            'authToken':
            None,
            'domains':
            list(domains),
            'anonymous':
            False,
            'enabled_toggles':
            list(enabled_toggles),
            'enabled_previews':
            list(previews_enabled_for_domain(domain))
        })
Beispiel #7
0
 def validate_spec(self, spec):
     target_domain = spec.get('domain')
     if target_domain and target_domain != self.domain:
         if target_domain not in EnterprisePermissions.get_domains(
                 self.domain):
             return self.error_message.format(target_domain, self.domain)