Пример #1
0
def create_user(organization_id: str) -> Any:
    form = UserForm(request.form)
    organization = Organization.query. \
        filter(Organization.id == organization_id).first()

    if request.method == 'POST' and form.validate():
        user = User()
        user.organization_id = organization.id
        form.populate_obj(user)
        user.active = True

        try:
            db.session.add(user)
            db.session.commit()
        except IntegrityError:
            db.session.rollback()
            form.email.errors.append('This email is already in use')
            return render_template('users/users/new.html',
                                   form=form,
                                   organization=organization)
        except:
            db.session.rollback()
            flash('Error saving user')
            return render_template('users/users/new.html',
                                   form=form,
                                   organization=organization)

        return redirect(
            url_for('organizations.organization',
                    organization_id=organization.id))

    return render_template('users/users/new.html',
                           form=form,
                           organization=organization)
Пример #2
0
def register() -> Any:
    show_captcha = current_app.config.get("USE_GOOGLE_CAPTCHA", False) == True
    site_key = current_app.config.get("GOOGLE_CAPTCHA_PUBLIC", '')
    form = RegisterForm(request.form)

    if request.method == 'POST' and form.validate():

        errors = False

        if show_captcha:
            try:
                recapture_response = request.form["g-recaptcha-response"]
                response = requests.post('https://www.google.com/recaptcha/api/siteverify', data =
                    {
                        'secret': current_app.config.get("GOOGLE_CAPTCHA_SECRET", ''),
                        'response': recapture_response
                    }
                )
                captcha_success = response.json()["success"]
            except:
                captcha_success = False

            if not captcha_success:
                errors = True
                form.captcha.errors.append('The capture did not validate, are you human?')

        valid_email_address = '@' in parseaddr(form.email.data)[1]

        if valid_email_address:
            user = User.query. \
                filter(User.email == form.email.data).first()
            if user:
                form.email.errors.append('User with the email already exists.')
                errors = True
        else:
            form.email.errors.append('Email is not valid.')
            errors = True

        if errors:
            return render_template('login/register.html', form=form, show_captcha=show_captcha, site_key=site_key)

        organization = Organization.query.filter(Organization.name == "debugproxy").first()

        if not organization:
            flash("Unable to create user. Please try again later.")
            return render_template('login/register.html', form=form, show_captcha=show_captcha, site_key=site_key)

        user = User()
        form.populate_obj(user)
        user.organization_id = organization.id
        user.active = True

        try:
            db.session.add(user)
            db.session.commit()
        except IntegrityError:
            db.session.rollback()
            flash("Unable to create user. Please try again later.")
            return render_template('login/register.html', form=form, show_captcha=show_captcha, site_key=site_key)

        email_manager = EmailManager(current_app)
        email_manager.send_registered_email(user, None, True)

        return redirect(url_for('login.registration_email_sent'))


    return render_template('login/register.html', form=form, show_captcha=show_captcha, site_key=site_key)