예제 #1
0
def register(request):
    if request.user is not None and request.user.is_authenticated():
        return HttpResponseRedirect(reverse('tas:profile'))

    if request.method == 'POST':
        register_form = UserRegistrationForm(request.POST)
        if register_form.is_valid():
            data = register_form.cleaned_data

            if request.POST.get('request_pi_eligibility'):
                data['piEligibility'] = 'Requested'
            else:
                data['piEligibility'] = 'Ineligible'

            data['source'] = 'Chameleon'
            logger.info('Attempting new user registration: %s' % _clean_registration_data(data))
            try:
                tas = TASClient()
                created_user = tas.save_user(None, data)

                if data['piEligibility'] == 'Requested':
                    _create_ticket_for_pi_request(created_user)

                messages.success(request, 'Congratulations! Your account request has been received. Please check your email for account verification.')
                return HttpResponseRedirect('/')
            except Exception as e:
                logger.exception('Error saving user')
                if len(e.args) > 1:
                    if re.search('DuplicateLoginException', e.args[1]):
                        message = 'The username you chose has already been taken. Please choose another. If you already have an account with TACC, please log in using those credentials.'
                        errors = register_form._errors.setdefault('username', ErrorList())
                        errors.append(message)
                        messages.error(request, message)
                    elif re.search('DuplicateEmailException', e.args[1]):
                        message = 'This email is already registered. If you already have an account with TACC, please log in using those credentials.'
                        messages.error(request, message + ' <a href="{0}">Did you forget your password?</a>'.format(reverse('tas:password_reset')))
                        errors = register_form._errors.setdefault('email', ErrorList())
                        errors.append(message)
                    elif re.search('PasswordInvalidException', e.args[1]):
                        message = 'The password you provided did not meet the complexity requirements.'
                        messages.error(request, message)
                        errors = register_form._errors.setdefault('password', ErrorList())
                        errors.append(message)
                    else:
                        messages.error(request, 'An unexpected error occurred. If this problem persists please create a help ticket.')
                else:
                    messages.error(request, 'An unexpected error occurred. If this problem persists please create a help ticket.')

            # return HttpResponseRedirect(reverse('tas:profile'))
    else:
        register_form = UserRegistrationForm()

    context = {
        'register_form': register_form,
        }

    if request.method == 'POST':
        context['request_pi_eligibility'] = request.POST.get('request_pi_eligibility')

    return render(request, 'tas/register.html', context)
예제 #2
0
def openid_register(request):
    openid = request.session["openid"]
    if request.method == "POST":
        reg_form = UserRegistrationForm(request.POST)
        if reg_form.is_valid():
            data = reg_form.cleaned_data

            if request.POST.get("request_pi_eligibility"):
                data["piEligibility"] = "Requested"
            else:
                data["piEligibility"] = "Ineligible"

            data["source"] = "Chameleon"
            logger.info("Attempting OpenID user registration: %s" % _clean_registration_data(data))
            try:
                tas = TASClient()
                created_user = tas.save_user(None, data)

                # eager create local user and save OpenIDUserIdentity
                local_user = get_user_model().objects.create_user(
                    username=created_user["username"],
                    email=created_user["email"],
                    first_name=created_user["firstName"],
                    last_name=created_user["lastName"],
                )

                # set not active until email confirmation
                local_user.is_active = False
                local_user.save()
                oid = OpenIDUserIdentity(uid=openid["url"], user=local_user)
                oid.save()

                messages.success(
                    request,
                    "Congratulations! Your account request has been "
                    "received. Please check your email for account "
                    "verification.",
                )
                return HttpResponseRedirect("/")
            except Exception as e:
                logger.exception("Error saving user")
                if len(e.args) > 1:
                    if re.search("DuplicateLoginException", e.args[1]):
                        message = (
                            "The username you chose has already been taken. "
                            "Please choose another. If you already have an "
                            "account with TACC, please log in using those "
                            "credentials."
                        )
                        errors = reg_form._errors.setdefault("username", ErrorList())
                        errors.append(message)
                        messages.error(request, message)
                    elif re.search("DuplicateEmailException", e.args[1]):
                        forgot_password_url = reverse("tas:password_reset")
                        message = (
                            "This email is already registered. If you already "
                            "have an account with TACC, please log in using those "
                            'credentials. <a href="%s">Did you forget your password?</a>' % forgot_password_url
                        )

                        messages.error(request, message)
                        errors = reg_form._errors.setdefault("email", ErrorList())
                        errors.append(message)
                    elif re.search("PasswordInvalidException", e.args[1]):
                        message = "The password you provided did not meet the complexity requirements."
                        messages.error(request, message)
                        errors = reg_form._errors.setdefault("password", ErrorList())
                        errors.append(message)
                    else:
                        messages.error(
                            request,
                            "An unexpected error occurred. If this problem " "persists please create a help ticket.",
                        )
                else:
                    messages.error(
                        request,
                        "An unexpected error occurred. If this problem " "persists please create a help ticket.",
                    )
    else:
        full_name = openid["ax"]["full_name"][0].split(" ")
        reg_form = UserRegistrationForm(
            initial={
                "email": openid["sreg"]["email"],
                "firstName": full_name[0],
                "lastName": full_name[-1],
                "username": openid["sreg"]["nickname"],
            }
        )
        try:
            username_taken = get_user_model().objects.get(username=openid["sreg"]["nickname"])
            if username_taken:
                messages.warning(
                    request,
                    "Your GENI nickname (<b>%s</b>) is not available on "
                    "Chameleon. Please choose a different username." % openid["sreg"]["nickname"],
                )
        except:
            pass

    context = {"register_form": reg_form}

    if request.method == "POST":
        context["request_pi_eligibility"] = request.POST.get("request_pi_eligibility")

    return render(request, "chameleon_openid/register.html", context)
예제 #3
0
def register(request):
    if request.user is not None and request.user.is_authenticated():
        return HttpResponseRedirect(reverse('tas:profile'))

    if request.method == 'POST':
        register_form = UserRegistrationForm(request.POST)
        if register_form.is_valid():
            data = register_form.cleaned_data

            if request.POST.get('request_pi_eligibility'):
                data['piEligibility'] = 'Requested'
            else:
                data['piEligibility'] = 'Ineligible'

            data['source'] = 'Chameleon'
            logger.info('Attempting new user registration: %s' %
                        _clean_registration_data(data))
            try:
                tas = TASClient()
                created_user = tas.save_user(None, data)

                if data['piEligibility'] == 'Requested':
                    _create_ticket_for_pi_request(created_user)

                messages.success(
                    request,
                    'Congratulations! Your account request has been received. Please check your email for account verification.'
                )
                return HttpResponseRedirect('/')
            except Exception as e:
                logger.exception('Error saving user')
                if len(e.args) > 1:
                    if re.search('DuplicateLoginException', e.args[1]):
                        message = 'The username you chose has already been taken. Please choose another. If you already have an account with TACC, please log in using those credentials.'
                        errors = register_form._errors.setdefault(
                            'username', ErrorList())
                        errors.append(message)
                        messages.error(request, message)
                    elif re.search('DuplicateEmailException', e.args[1]):
                        message = 'This email is already registered. If you already have an account with TACC, please log in using those credentials.'
                        messages.error(
                            request, message +
                            ' <a href="{0}">Did you forget your password?</a>'.
                            format(reverse('tas:password_reset')))
                        errors = register_form._errors.setdefault(
                            'email', ErrorList())
                        errors.append(message)
                    elif re.search('PasswordInvalidException', e.args[1]):
                        message = 'The password you provided did not meet the complexity requirements.'
                        messages.error(request, message)
                        errors = register_form._errors.setdefault(
                            'password', ErrorList())
                        errors.append(message)
                    else:
                        messages.error(
                            request,
                            'An unexpected error occurred. If this problem persists please create a help ticket.'
                        )
                else:
                    messages.error(
                        request,
                        'An unexpected error occurred. If this problem persists please create a help ticket.'
                    )

            # return HttpResponseRedirect(reverse('tas:profile'))
    else:
        register_form = UserRegistrationForm()

    context = {
        'register_form': register_form,
    }

    if request.method == 'POST':
        context['request_pi_eligibility'] = request.POST.get(
            'request_pi_eligibility')

    return render(request, 'tas/register.html', context)