def register_new_user(self, data): idp = None if settings.ENFORCE_SSO_LOGIN: idp = IdentityProvider.get_required_identity_provider( data['data']['email']) reg_form = RegisterWebUserForm(data['data'], is_sso=idp is not None) if reg_form.is_valid(): ab_test = ab_tests.SessionAbTest(ab_tests.APPCUES_V3_APP, self.request) appcues_ab_test = ab_test.context['version'] if idp: signup_request = AsyncSignupRequest.create_from_registration_form( reg_form, additional_hubspot_data={ "appcues_test": appcues_ab_test, }) return { 'success': True, 'appcues_ab_test': appcues_ab_test, 'ssoLoginUrl': idp.get_login_url(signup_request.username), 'ssoIdpName': idp.name, } self._create_new_account(reg_form, additional_hubspot_data={ "appcues_test": appcues_ab_test, }) try: request_new_domain(self.request, reg_form.cleaned_data['project_name'], is_new_user=True) except NameUnavailableException: # technically, the form should never reach this as names are # auto-generated now. But, just in case... logging.error( "There as an issue generating a unique domain name " "for a user during new registration.") return { 'errors': { 'project name unavailable': [], } } return { 'success': True, 'appcues_ab_test': appcues_ab_test, } logging.error( "There was an error processing a new user registration form." "This shouldn't happen as validation should be top-notch " "client-side. Here is what the errors are: {}".format( reg_form.errors)) return { 'errors': reg_form.errors, }
def check_username_availability(self, data): email = data['email'].strip() duplicate = CouchUser.get_by_username(email) is_existing = User.objects.filter( username__iexact=email).count() > 0 or duplicate message = None restricted_by_domain = None if is_existing: message = _("There is already a user with this email.") else: domain = email[email.find("@") + 1:] for account in BillingAccount.get_enterprise_restricted_signup_accounts( ): if domain in account.enterprise_restricted_signup_domains: restricted_by_domain = domain message = account.restrict_signup_message regex = r'(\b[a-zA-Z0-9_.+%-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+\b)' subject = _("CommCareHQ account request") message = re.sub( regex, "<a href='mailto:\\1?subject={}'>\\1</a>".format( subject), message) break response = { 'isValid': message is None, 'restrictedByDomain': restricted_by_domain, 'message': message, } if settings.ENFORCE_SSO_LOGIN and response['isValid']: idp = IdentityProvider.get_required_identity_provider(email) if idp: response.update({ 'isSso': True, 'ssoMessage': _("This email is managed by {}. You will be asked to login " "with Single Sign-On after the next step.").format( idp.name), }) return response