Ejemplo n.º 1
0
    def after_model_change(self, form, model, is_created):
        if model.active == 1:
            print('account is active')
        else:
            print('account needs to be activated')

        #Randomly generate temp user pw and send to email
        if len(model.password) == 0:
            import string
            import random

            def pw_gen(size=10,
                       chars=string.ascii_uppercase + string.ascii_lowercase +
                       string.digits):
                return ''.join(random.choice(chars) for _ in range(size))

            password = pw_gen()
            model.password = password
            confirmation_link, token = generate_confirmation_link(model)
            from app.templates.security import email
            msg = Message("Medusa Temp Password", recipients=[model.email])
            msg.body = "Your temporary password is: " + password + "\n\nPlease click on the following confirmation link " + confirmation_link
            mail.send(msg)
            from flask_security.changeable import change_user_password
            model.password = hash_password(password)
            db.session.commit()
Ejemplo n.º 2
0
def register_user(user):
    """Performs the user registration process.

    Returns True if the user has been logged in, false otherwise.
    """
    if not _security.confirmable or _security.login_without_confirmation:
        user.active = True

    # confirmation token depends on having user.id set, which requires
    # the user be committed to the database
    user.save(commit=True)

    confirmation_link, token = None, None
    if _security.confirmable:
        confirmation_link, token = generate_confirmation_link(user)

    user_registered.send(current_app._get_current_object(),
                         user=user,
                         confirm_token=token)

    if config_value('SEND_REGISTER_EMAIL'):
        send_mail(config_value('EMAIL_SUBJECT_REGISTER'),
                  user.email,
                  'welcome',
                  user=user,
                  confirmation_link=confirmation_link)

    if not _security.confirmable or _security.login_without_confirmation:
        login_user(user)
        # login_user will modify the user object if _security.trackable is set,
        # but it will not request a session commit itself when it needs it :/
        after_this_request(_commit)
        return True

    return False
Ejemplo n.º 3
0
def register_user(should_confirm, **kwargs):
    confirmation_link, token = None, None
    kwargs['password'] = encrypt_password(kwargs['password'])
    user = user_datastore.create_user(**kwargs)
    user_datastore.commit()
    flash(gettext('User created successfully'))
    if should_confirm:
        confirmation_link, token = generate_confirmation_link(user)
        do_flash(*get_message('CONFIRM_REGISTRATION', email=user.email))
        send_mail(
            config_value('EMAIL_SUBJECT_REGISTER'),
            user.email, 'welcome', user=user, confirmation_link=confirmation_link)
    user_registered.send(app, user=user, confirm_token=token)
Ejemplo n.º 4
0
def confirm_email(user):
    confirmation_link, token = generate_confirmation_link(user)

    subject = 'Confirmation Email for ' + user.first_name

    content = '''
              Please confirm your email with the following link: \n \n
    
              {}
              '''.format(confirmation_link)

    msg = Message(subject=subject, body=content, recipients=[user.email])

    mail.send(msg)
Ejemplo n.º 5
0
def resend_email_verification(emails, all):
    user_query = User.query
    if emails:
        conditions = []
        for item in emails:
            conditions.append(User.email == item)
        user_query = user_query.filter(or_(*conditions))
    elif all:
        if "amazon" not in DATABASE_URI or "api.improviser.education" not in app.config[
                "SERVER_NAME"]:
            logger.warning(
                "Cowardly refusing to run on all on local DB and/or local NAME."
            )
            return
        logger.info("Querying ALL users")
        user_query = user_query.filter(User.confirmed_at.is_(None))
    else:
        logger.warning(
            "Cowardly refusing to run on all without `--all` mode set.")
        return

    done = 0
    skipped = 0
    for index, user in enumerate(user_query.all()):
        if not user.confirmed_at:
            logger.info("Working for user", email=user.email)
            confirmation_link, token = generate_confirmation_link(user)

            _security.send_mail(
                "Gentle reminder to verify your iMproviser account",
                user.email,
                "reconfirm_email_address",
                user=user,
                confirmation_link=confirmation_link,
            )
            done += 1
            logger.info("Mail sent", email=user.email, done=index)
        else:
            skipped += 1
            logger.info("Skipping already confirmed user",
                        email=user.email,
                        skipped=skipped)
    logger.info
Ejemplo n.º 6
0
def register_user(**kwargs):
    if kwargs.pop('random_password', False) or len(kwargs['password']) == 0:
        kwargs['password'] = _randompassword()

    # hash password so that we never store the original
    kwargs['password'] = hash_password(kwargs['password'])

    # pop ask_confirm value before kwargs is presented to create_user()
    ask_confirm = kwargs.pop('ask_confirm', False)

    # generate a User record and commit it
    kwargs['active'] = True
    roles = kwargs.get('roles', [])
    roles_step1 = [db.session.query(Role).filter_by(name=r).first() for r in roles]
    roles_step2 = [x for x in roles_step1 if x is not None]
    kwargs['roles'] = roles_step2
    kwargs['fs_uniquifier'] = uuid.uuid4().hex

    user = User(**kwargs)

    db.session.add(user)
    db.session.commit()

    # send confirmation email if we have been asked to
    if ask_confirm:
        confirmation_link, token = generate_confirmation_link(user)
        do_flash(*get_message('CONFIRM_REGISTRATION', email=user.email))

        user_registered.send(current_app._get_current_object(),
                             user=user, confirm_token=token)

        if config_value('SEND_REGISTER_EMAIL'):
            send_mail(config_value('EMAIL_SUBJECT_REGISTER'), user.email,
                      'welcome', user=user, confirmation_link=confirmation_link)

    else:
        user.confirmed_at = datetime.now()
        db.session.commit()

    return user