Esempio n. 1
0
def user_etl(rec):
    if rec == 'header':
        return ['member_number', 'user_name', 'role', 'password']
    user = User(member_id=get_member_id(rec['member_number']),
                user_name=rec['user_name'])
    user.set_password(rec['password'])
    user.role = UserRole.from_name(rec['role'])
    return user
Esempio n. 2
0
def register_user(member_number, user_name, password, email=None, role=UserRole.member, two_phase=False, activate_url=None):
    user_id = 0
    if user_name and password:
        member = get_member(member_number)
        if member:
            if not member.is_active():
                ok, message, message_type = False, 'Sorry, you are not a current member, please contact [email protected]', 'danger'
            else:
                if not match_string(member.email, email):
                    ok, message, message_type = False, 'Email does not match', 'warning'
                else:
                    user = get_user(user_name=user_name)
                    if user and user.member_id != member.id:
                        ok, message, message_type = False, 'User name already in use', 'warning'
                    else:
                        if role == UserRole.member:
                            ok, message, message_type = member.check_credentials(user_name, password)
                            if not ok:
                                return ok, user_id, message, message_type
                        if not member.user:
                            user = User(user_name=user_name, member_id=member.id)
                        else:
                            user = member.user
                            user.user_name = user_name
                        if not user.check_password(password):
                            ok, message, message_type = True, 'Password updated', 'success'
                        else:
                            if two_phase:
                                token, expires = user.get_token(current_app)
                                send_mail(
                                    to=member.email,
                                    sender='*****@*****.**',
                                    cc=[],
                                    subject='Dons Trust Members - registration',
                                    message=render_template('activate.txt',
                                                              full_url_for=full_url_for,
                                                              member=member,
                                                              token=token,
                                                              expires=expires)
                                )
                                ok, message, message_type = True, 'Activation email sent to {}'.format(member.email), 'success'
                            else:
                                ok, message, message_type = True, 'You are now a registered user', 'success'
                        user.set_password(password)
                        if not user.role:
                            user.role = role
                        # else:
                        #     if not role in [role.role for role in user.roles]:
                        #         user.roles += [Role(role=role)]
                        save_user(user)
                        user_id = user.id
        else:
            ok, message, message_type = False, 'Cannot find your membership', 'danger'
    else:
        ok, message, message_type = False, 'Missing user name/password', 'warning'
    return ok, user_id, message, message_type