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)
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)
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)
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)