예제 #1
0
파일: auth.py 프로젝트: Bauble/bauble.api
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.')
예제 #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()
예제 #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()