def redirect_to_default(req, domain=None): if not req.user.is_authenticated(): if domain != None: url = reverse('domain_login', args=[domain]) else: # this actually gets hijacked by the static site, but is necessary url = reverse('corehq.apps.hqwebapp.views.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(): url = reverse("cloudcare_main", args=[domain, ""]) elif req.couch_user.can_view_reports( domain) or req.couch_user.get_viewable_reports(domain): url = reverse('corehq.apps.reports.views.default', args=[domain]) else: url = reverse('corehq.apps.app_manager.views.default', args=[domain]) else: raise Http404 else: url = settings.DOMAIN_SELECT_URL return HttpResponseRedirect(url)
def _two_factor_needed(domain_name, request): domain_name = normalize_domain_name(domain_name) domain_obj = Domain.get_by_name(domain_name) if domain_obj: return (domain_obj.two_factor_auth and not request.couch_user.two_factor_disabled and not request.user.is_verified())
def redirect_to_default(req, domain=None): if not req.user.is_authenticated(): if domain != None: url = reverse('domain_login', args=[domain]) else: 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: 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', domain_type=get_domain_type(None, req)) 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: url = reverse('dashboard_default', args=[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: # this actually gets hijacked by the static site, but is necessary url = reverse('corehq.apps.hqwebapp.views.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(): if not is_mobile_worker_with_report_access( req.couch_user, domain): url = reverse("cloudcare_main", args=[domain, ""]) else: url = reverse("saved_reports", args=[domain]) elif req.couch_user.can_view_reports(domain) or req.couch_user.get_viewable_reports(domain): url = reverse('corehq.apps.reports.views.default', args=[domain]) else: url = reverse('corehq.apps.app_manager.views.default', args=[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: 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)
def __init__(self, request, base_context=None, domain=None, **kwargs): if not self.name or not self.section_name or self.slug is None or not self.dispatcher: raise NotImplementedError( "Missing a required parameter: (name: %(name)s, section_name: %(section_name)s," " slug: %(slug)s, dispatcher: %(dispatcher)s" % dict(name=self.name, section_name=self.section_name, slug=self.slug, dispatcher=self.dispatcher)) from corehq.apps.reports.dispatcher import ReportDispatcher if isinstance(self.dispatcher, ReportDispatcher): raise ValueError( "Class property dispatcher should point to a subclass of ReportDispatcher." ) self.request = request self.request_params = json_request( self.request.GET if self.request.method == 'GET' else self.request.POST) self.domain = normalize_domain_name(domain) self.context = base_context or {} self._update_initial_context() self.is_rendered_as_email = False # setting this to true in email_response self.override_template = "reports/async/email_report.html"
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): if MONITOR_2FA_CHANGES.enabled(domain): from corehq.apps.hqwebapp.utils import monitor_2fa_soft_assert monitor_2fa_soft_assert( False, f'2FA required page shown to user ' f'{req.user.username} on {domain} after ' f'login') 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.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 url = reverse("dashboard_domain", args=[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: url = reverse("dashboard_domain", args=[domain]) else: raise Http404() else: url = settings.DOMAIN_SELECT_URL return HttpResponseRedirect(url)
def _two_factor_needed(domain_name, request): domain_name = normalize_domain_name(domain_name) domain_obj = Domain.get_by_name(domain_name) if domain_obj: return ( domain_obj.two_factor_auth and not request.couch_user.two_factor_disabled and not request.user.is_verified() )
def load_domain(req, domain): domain_name = normalize_domain_name(domain) domain = Domain.get_by_name(domain_name) req.project = domain req.can_see_organization = True try: ensure_request_has_privilege(req, privileges.CROSS_PROJECT_REPORTS) except PermissionDenied: req.can_see_organization = False return domain_name, domain
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)
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): 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)
def __init__(self, request, base_context=None, domain=None, **kwargs): if not self.name or not self.section_name or self.slug is None or not self.dispatcher: raise NotImplementedError( "Missing a required parameter: (name: %(name)s, section_name: %(section_name)s," " slug: %(slug)s, dispatcher: %(dispatcher)s" % dict(name=self.name, section_name=self.section_name, slug=self.slug, dispatcher=self.dispatcher) ) from corehq.apps.reports.dispatcher import ReportDispatcher if isinstance(self.dispatcher, ReportDispatcher): raise ValueError("Class property dispatcher should point to a subclass of ReportDispatcher.") self.request = request self.request_params = json_request(self.request.GET if self.request.method == "GET" else self.request.POST) self.domain = normalize_domain_name(domain) self.context = base_context or {} self._update_initial_context() self.is_rendered_as_email = False # setting this to true in email_response self.override_template = "reports/async/email_report.html"
def legacy_domain_name(request, domain, path): domain = normalize_domain_name(domain) return HttpResponseRedirect(get_domained_url(domain, path))
def _get_domain(xform): matches = DOMAIN_RE.search(xform.path) if matches and len(matches.groups()): return normalize_domain_name(matches.groups()[0])
def _two_factor_needed(domain_name, request): domain_name = normalize_domain_name(domain_name) domain = Domain.get_by_name(domain_name) if domain: return domain.two_factor_auth and not request.user.is_verified()
def handle(self, *args, **options): django_domains = OldDomain.objects.all() django_domain_names = set([domain.name for domain in django_domains]) couch_domain_names = set([x['key'][0] for x in get_db().view('domain/docs', group_level=1).all()]) couch_user_domain_names = set([x['key'] for x in get_db().view('users/by_domain', group=True).all()]) print get_db().view('users/by_domain').all() normalized_names = {} domains_that_need_to_change = set() # print some warnings if things are fishy for domain in couch_domain_names.union(couch_user_domain_names): if domain not in django_domain_names: print "Warning: domain '%s' not in SQL" % domain normalized = normalize_domain_name(domain) if normalized in normalized_names: print "Warning: domains '%s' and '%s' both exist" % (domain, normalized_names[normalized]) normalized_names[normalized] = domain if normalized != domain: domains_that_need_to_change.add(domain) print "Going to change the following domains:" for domain in domains_that_need_to_change: print " %s" % domain print if raw_input("Are you sure you want to continue? (Y/n)") != 'Y': print "Mission aborted" return print "Migrating SQL domains" for django_domain in django_domains: django_domain.name = normalize_domain_name(django_domain.name) django_domain.save() print "Migrating domains in Couch docs" class MyDoc(Document): class Meta: app_label = 'domain' def get_docs(domain): chunk_size = 500 i = 0 while True: docs = MyDoc.view('domain/docs', startkey=[domain], endkey=[domain, {}], reduce=False, include_docs=True, skip=i*chunk_size, limit=chunk_size) for doc in docs: yield doc if not len(docs): break i += 1 for domain in domains_that_need_to_change: print "%s:" % domain for doc in get_docs(domain): print '.', if 'domain' in doc: doc['domain'] = normalize_domain_name(doc['domain']) if 'domains' in doc: for i,domain in enumerate(doc['domains']): doc['domains'][i] = normalize_domain_name(doc['domains'][i]) doc.save() print print "Patching users" for domain in domains_that_need_to_change: print '.', couch_users = CouchUser.view('users/by_domain', key=domain, include_docs=True, reduce=False) for user in couch_users: for dm in user.web_account.domain_memberships: dm.domain = normalize_domain_name(dm.domain) for account in user.commcare_accounts: if account.domain: account.domain = normalize_domain_name(account.domain) user.save()
def domain(self): domain = self.args[0] if len(self.args) > 0 else self.kwargs.get('domain', "") return normalize_domain_name(domain)
def load_domain(req, domain): domain_name = normalize_domain_name(domain) domain = Domain.get_by_name(domain_name) req.project = domain return domain_name, domain
def domain(self): domain = self.args[0] if len(self.args) > 0 else self.kwargs.get( 'domain', "") return normalize_domain_name(domain)
def sync_from_old_couch_user(self, old_couch_user): super(WebUser, self).sync_from_old_couch_user(old_couch_user) for dm in old_couch_user.web_account.domain_memberships: dm.domain = normalize_domain_name(dm.domain) self.domain_memberships.append(dm) self.domains.append(dm.domain)
def sync_from_old_couch_user(self, old_couch_user): super(CommCareUser, self).sync_from_old_couch_user(old_couch_user) self.domain = normalize_domain_name(old_couch_user.default_account.domain) self.registering_device_id = old_couch_user.default_account.registering_device_id self.user_data = old_couch_user.default_account.user_data
def load_domain(req, domain): domain_name = normalize_domain_name(domain) domain = Domain.get_by_name(domain_name) req.project = domain req.can_see_organization = has_privilege(req, privileges.CROSS_PROJECT_REPORTS) return domain_name, domain