Example #1
0
def redirect_to_default(req, domain=None):
    if not req.user.is_authenticated:
        if domain != None:
            url = reverse('domain_login', args=[domain])
        else:
            if settings.ENABLE_PRELOGIN_SITE:
                try:
                    from corehq.apps.prelogin.views import HomePublicView
                    url = reverse(HomePublicView.urlname)
                except ImportError:
                    # this happens when the prelogin app is not included.
                    url = reverse('login')
            else:
                url = reverse('login')
    elif domain and _two_factor_needed(domain, req):
        return TemplateResponse(
            request=req,
            template='two_factor/core/otp_required.html',
            status=403,
        )
    else:
        if domain:
            domain = normalize_domain_name(domain)
            domains = [Domain.get_by_name(domain)]
        else:
            domains = Domain.active_for_user(req.user)

        if 0 == len(domains) and not req.user.is_superuser:
            return redirect('registration_domain')
        elif 1 == len(domains):
            from corehq.apps.dashboard.views import dashboard_default
            from corehq.apps.users.models import DomainMembershipError
            if domains[0]:
                domain = domains[0].name
                couch_user = req.couch_user
                try:
                    role = couch_user.get_role(domain)
                except DomainMembershipError:
                    # commcare users without roles should always be denied access
                    if couch_user.is_commcare_user():
                        raise Http404()
                    else:
                        # web users without roles are redirected to the dashboard default
                        # view since some domains allow web users to request access if they
                        # don't have it
                        return dashboard_default(req, domain)
                else:
                    if role and role.default_landing_page:
                        url = get_redirect_url(role.default_landing_page,
                                               domain)
                    elif couch_user.is_commcare_user():
                        url = reverse(get_cloudcare_urlname(domain),
                                      args=[domain])
                    else:
                        return dashboard_default(req, domain)
            else:
                raise Http404()
        else:
            url = settings.DOMAIN_SELECT_URL
    return HttpResponseRedirect(url)
Example #2
0
def redirect_to_default(req, domain=None):
    if not req.user.is_authenticated:
        if domain != None:
            url = reverse('domain_login', args=[domain])
        else:
            url = reverse('login')
    elif domain and _two_factor_needed(domain, req):
        return TemplateResponse(
            request=req,
            template='two_factor/core/otp_required.html',
            status=403,
        )
    else:
        if domain:
            domain = normalize_domain_name(domain)
            domains = [Domain.get_by_name(domain)]
        else:
            domains = Domain.active_for_user(req.user)

        if 0 == len(domains) and not req.user.is_superuser:
            from corehq.apps.registration.views import track_domainless_new_user
            track_domainless_new_user(req)
            return redirect('registration_domain')
        elif 1 == len(domains):
            from corehq.apps.dashboard.views import dashboard_default
            from corehq.apps.users.models import DomainMembershipError
            if domains[0]:
                domain = domains[0].name
                couch_user = req.couch_user
                try:
                    role = couch_user.get_role(domain)
                except DomainMembershipError:
                    # commcare users without roles should always be denied access
                    if couch_user.is_commcare_user():
                        raise Http404()
                    else:
                        # web users without roles are redirected to the dashboard default
                        # view since some domains allow web users to request access if they
                        # don't have it
                        return dashboard_default(req, domain)
                else:
                    if role and role.default_landing_page:
                        url = get_redirect_url(role.default_landing_page, domain)
                    elif couch_user.is_commcare_user():
                        url = reverse(get_cloudcare_urlname(domain), args=[domain])
                    else:
                        return dashboard_default(req, domain)
            else:
                raise Http404()
        else:
            url = settings.DOMAIN_SELECT_URL
    return HttpResponseRedirect(url)
Example #3
0
def redirect_to_default(req, domain=None):
    from corehq.apps.cloudcare.views import FormplayerMain

    if not req.user.is_authenticated():
        if domain != None:
            url = reverse('domain_login', args=[domain])
        else:
            if settings.ENABLE_PRELOGIN_SITE:
                try:
                    from corehq.apps.prelogin.views import HomePublicView
                    url = reverse(HomePublicView.urlname)
                except ImportError:
                    # this happens when the prelogin app is not included.
                    url = reverse('login')
            else:
                url = reverse('login')
    elif domain and _two_factor_needed(domain, req):
        return TemplateResponse(
            request=req,
            template='two_factor/core/otp_required.html',
            status=403,
        )
    else:
        if domain:
            domain = normalize_domain_name(domain)
            domains = [Domain.get_by_name(domain)]
        else:
            domains = Domain.active_for_user(req.user)

        if 0 == len(domains) and not req.user.is_superuser:
            return redirect('registration_domain')
        elif 1 == len(domains):
            if domains[0]:
                domain = domains[0].name
                couch_user = req.couch_user
                from corehq.apps.users.models import DomainMembershipError
                try:
                    if (couch_user.is_commcare_user()
                            and couch_user.can_view_some_reports(domain)):
                        if toggles.USE_FORMPLAYER_FRONTEND.enabled(domain):
                            url = reverse(FormplayerMain.urlname,
                                          args=[domain])
                        else:
                            url = reverse("cloudcare_main", args=[domain, ""])
                    else:
                        from corehq.apps.dashboard.views import dashboard_default
                        return dashboard_default(req, domain)
                except DomainMembershipError:
                    raise Http404()
            else:
                raise Http404()
        else:
            url = settings.DOMAIN_SELECT_URL
    return HttpResponseRedirect(url)
Example #4
0
def redirect_to_default(req, domain=None):
    from corehq.apps.cloudcare.views import FormplayerMain

    if not req.user.is_authenticated():
        if domain != None:
            url = reverse('domain_login', args=[domain])
        else:
            if settings.ENABLE_PRELOGIN_SITE:
                try:
                    from corehq.apps.prelogin.views import HomePublicView
                    url = reverse(HomePublicView.urlname)
                except ImportError:
                    # this happens when the prelogin app is not included.
                    url = reverse('login')
            else:
                url = reverse('login')
    elif domain and _two_factor_needed(domain, req):
        return TemplateResponse(
            request=req,
            template='two_factor/core/otp_required.html',
            status=403,
        )
    else:
        if domain:
            domain = normalize_domain_name(domain)
            domains = [Domain.get_by_name(domain)]
        else:
            domains = Domain.active_for_user(req.user)
        if 0 == len(domains) and not req.user.is_superuser:
            return redirect('registration_domain')
        elif 1 == len(domains):
            if domains[0]:
                domain = domains[0].name
                couch_user = req.couch_user

                if (couch_user.is_commcare_user() and
                        couch_user.can_view_some_reports(domain)):
                    if toggles.USE_FORMPLAYER_FRONTEND.enabled(domain):
                        url = reverse(FormplayerMain.urlname, args=[domain])
                    else:
                        url = reverse("cloudcare_main", args=[domain, ""])
                else:
                    from corehq.apps.dashboard.views import dashboard_default
                    return dashboard_default(req, domain)

            else:
                raise Http404
        else:
            url = settings.DOMAIN_SELECT_URL
    return HttpResponseRedirect(url)
Example #5
0
def select(request,
           domain_select_template='domain/select.html',
           do_not_redirect=False):
    domains_for_user = Domain.active_for_user(request.user)
    if not domains_for_user:
        from corehq.apps.registration.views import track_domainless_new_user
        track_domainless_new_user(request)
        return redirect('registration_domain')

    email = request.couch_user.get_email()
    open_invitations = [
        e for e in Invitation.by_email(email) if not e.is_expired
    ]

    additional_context = {
        'domains_for_user': domains_for_user,
        'open_invitations': open_invitations,
        'current_page': {
            'page_name': _('Select A Project')
        },
    }

    last_visited_domain = request.session.get('last_visited_domain')
    if open_invitations \
       or do_not_redirect \
       or not last_visited_domain:
        return render(request, domain_select_template, additional_context)
    else:
        domain_obj = Domain.get_by_name(last_visited_domain)
        if domain_obj and domain_obj.is_active:
            # mirrors logic in login_and_domain_required
            if (request.couch_user.is_member_of(domain_obj)
                    or (request.user.is_superuser
                        and not domain_obj.restrict_superusers)
                    or domain_obj.is_snapshot):
                try:
                    from corehq.apps.dashboard.views import dashboard_default
                    return dashboard_default(request, last_visited_domain)
                except Http404:
                    pass

        del request.session['last_visited_domain']
        return render(request, domain_select_template, additional_context)
Example #6
0
def redirect_to_default(req, domain=None):
    if not req.user.is_authenticated():
        if domain != None:
            url = reverse('domain_login', args=[domain])
        else:
            if settings.ENABLE_PRELOGIN_SITE:
                try:
                    from corehq.apps.prelogin.views import HomePublicView
                    url = reverse(HomePublicView.urlname)
                except ImportError:
                    # this happens when the prelogin app is not included.
                    url = reverse('landing_page')
            else:
                url = reverse('landing_page')
    else:
        if domain:
            domain = normalize_domain_name(domain)
            domains = [Domain.get_by_name(domain)]
        else:
            domains = Domain.active_for_user(req.user)
        if 0 == len(domains) and not req.user.is_superuser:
            return redirect('registration_domain')
        elif 1 == len(domains):
            if domains[0]:
                domain = domains[0].name

                if (req.couch_user.is_commcare_user()
                    and not is_mobile_worker_with_report_access(
                        req.couch_user, domain)):
                    url = reverse("cloudcare_main", args=[domain, ""])
                else:
                    from corehq.apps.dashboard.views import dashboard_default
                    return dashboard_default(req, domain)

            else:
                raise Http404
        else:
            url = settings.DOMAIN_SELECT_URL
    return HttpResponseRedirect(url)
Example #7
0
def select(request, domain_select_template='domain/select.html', do_not_redirect=False):
    domains_for_user = Domain.active_for_user(request.user)
    if not domains_for_user:
        from corehq.apps.registration.views import track_domainless_new_user
        track_domainless_new_user(request)
        return redirect('registration_domain')

    email = request.couch_user.get_email()
    open_invitations = [e for e in Invitation.by_email(email) if not e.is_expired]

    additional_context = {
        'domains_for_user': domains_for_user,
        'open_invitations': open_invitations,
        'current_page': {'page_name': _('Select A Project')},
    }

    last_visited_domain = request.session.get('last_visited_domain')
    if open_invitations \
       or do_not_redirect \
       or not last_visited_domain:
        return render(request, domain_select_template, additional_context)
    else:
        domain_obj = Domain.get_by_name(last_visited_domain)
        if domain_obj and domain_obj.is_active:
            # mirrors logic in login_and_domain_required
            if (
                request.couch_user.is_member_of(domain_obj)
                or (request.user.is_superuser and not domain_obj.restrict_superusers)
                or domain_obj.is_snapshot
            ):
                try:
                    from corehq.apps.dashboard.views import dashboard_default
                    return dashboard_default(request, last_visited_domain)
                except Http404:
                    pass

        del request.session['last_visited_domain']
        return render(request, domain_select_template, additional_context)