Esempio n. 1
0
def forgot_password():

    user_email = request.params.get('email', None)
    if not user_email and not user_email.contains('@'):
        bottle.abort("Valid email address required")

    session = None
    try:
        session = db.Session()
        user = session.query(User)\
                      .filter(sa.func.lower(User.email) == user_email.lower())\
                      .first()
        if not user:
            bottle.abort(422, "Could not get a user with the requested email address")
        token, expiration = create_password_reset_token()
        user.password_reset_token = token
        user.password_reset_token_expiration = expiration
        session.commit()
    finally:
        if session:
            session.close()

    app_url = config.get("BAUBLE_APP_URL")
    mappings = {'token': token, 'email': user_email, 'app_url': app_url}

    try:
        email.send_template('reset_password.txt', mappings, **{
            'to': user_email,
            'from': '*****@*****.**',
            'subject': 'Bauble Password Reset'})

    except smtplib.SMTPException as exc:
        print(exc)
        bottle.abort(500, 'Could not send reset password email.')
Esempio n. 2
0
def send_invitation(organization_id):

    token = create_unique_token()
    subject = 'Bauble Invitation'
    to_email = request.json.get('email', None)
    if to_email is None or '@' not in to_email:
        bottle.abort(422, "An email recipient is required.")

    # make sure a user with this email address doesn't already have an account
    count = request.session.query(User).filter_by(email=to_email).count()
    if count > 0:
        bottle.abort(
            409, "A user with this email address already has a Bauble account")

    # if a message was provided then send it else use the default message
    try:
        print('request.json: ', request.json)
        if 'message' in request.json:
            email.send(
                request.json['message'], **{
                    'to': to_email,
                    'subject': subject,
                    'from': '*****@*****.**'
                })
        else:
            email.send_template(
                'default_invite.txt', {
                    'organization': request.organization.name,
                    'app_url': config.get("BAUBLE_APP_URL"),
                    'token': token
                }, **{
                    'to': request.json['email'],
                    'subject': subject,
                    'from': '*****@*****.**'
                })
    except smtplib.SMTPException as exc:
        print('exc: ', exc)
        bottle.abort(500, 'Could not send invitation email.')

    invitation = Invitation(
        **{
            'email': to_email,
            'organization_id': request.organization.id,
            'date_sent': datetime.now(),
            'invited_by_id': request.user.id,
            'message': request.json['message'] if 'message' in
            request.json else None,
            'token': token,
            'token_expiration': datetime.now() + timedelta(weeks=2)
        })

    request.session.add(invitation)
    request.session.commit()
Esempio n. 3
0
def send_invitation(organization_id):

    token = create_unique_token()
    subject = 'Bauble Invitation'
    to_email = request.json.get('email', None)
    if to_email is None or '@' not in to_email:
        bottle.abort(422, "An email recipient is required.")

    # make sure a user with this email address doesn't already have an account
    count = request.session.query(User).filter_by(email=to_email).count()
    if count > 0:
        bottle.abort(409, "A user with this email address already has a Bauble account")

    # if a message was provided then send it else use the default message
    try:
        print('request.json: ', request.json)
        if 'message' in request.json:
            email.send(request.json['message'], **{
                'to': to_email,
                'subject': subject,
                'from': '*****@*****.**'
            })
        else:
            email.send_template('default_invite.txt', {
                'organization': request.organization.name,
                'app_url': config.get("BAUBLE_APP_URL"),
                'token': token
            }, **{
                'to': request.json['email'],
                'subject': subject,
                'from': '*****@*****.**'
            })
    except smtplib.SMTPException as exc:
        print('exc: ', exc)
        bottle.abort(500, 'Could not send invitation email.')

    invitation = Invitation(**{
        'email': to_email,
        'organization_id': request.organization.id,
        'date_sent': datetime.now(),
        'invited_by_id': request.user.id,
        'message': request.json['message'] if 'message' in request.json else None,
        'token': token,
        'token_expiration': datetime.now() + timedelta(weeks=2)
    })

    request.session.add(invitation)
    request.session.commit()
Esempio n. 4
0
def send(body, **headers):
    use_ssl = config.get("SMTP_USE_SSL", "false")
    host = config.get("SMTP_HOST", "")
    port = config.get("SMTP_PORT", 0)
    user = config.get("SMTP_USERNAME", None)
    password = config.get("SMTP_PASSWORD", None)

    SMTP = smtplib.SMTP_SSL if use_ssl or use_ssl.lower() == "true" else smtplib.SMTP

    # **************************************************
    # don't send the email if we're running the tests
    # **************************************************
    if config.get('SEND_EMAILS') is not True:
        return

    timeout = 10  # 10 second timeout for smtp connection
    message = MIMEText(body, 'plain')
    for key, value in headers.items():
        message[key] = value

    with SMTP(host, port, timeout=timeout) as smtp:
        smtp.login(user, password)
        smtp.send_message(message)
Esempio n. 5
0
import sqlalchemy.orm as orm

import bauble.types as types
import bauble.config as config

admin_username = "******"

# TODO:
# 1. when a new organization is created the "validator" user creates
# the organization and the owner user
# 2. the validator then sends a verification email to the new user
# 3. when the verification is clicked the user account is activated
# 4. need to set the password on the organization's pg role and
# store it in the organization table...this will at least prevent logging in and if they can login with the admin we're screwed anyways

debug = config.get('DEBUG', False)
db_url = config.get('BAUBLE_DB_URL')
engine = sa.create_engine(db_url, pool_size=20, encoding="utf-8", echo=debug)
Session = orm.sessionmaker(bind=engine)


def create_unique_schema():
    session = Session()
    unique_name = "bbl_" + str(uuid.uuid4()).replace("-", "_")
    session.execute("CREATE SCHEMA {name};".format(name=unique_name))
    session.commit()
    session.close()
    return unique_name


@contextmanager