def invite_new_user(email): user_manager = current_app.user_manager db_adapter = user_manager.db_adapter user, user_email = user_manager.find_user_by_email(email) if user: raise UserAlreadyRegisteredError( '{} is already registered'.format(email)) # the following is copied from flask_user.views.invite from flask_user import signals, emails user_invite = db_adapter.add_object( db_adapter.UserInvitationClass, email=email) db_adapter.commit() token = user_manager.generate_token(user_invite.id) accept_invite_link = url_for('user.register', token=token, _external=True) # Store token if hasattr(db_adapter.UserInvitationClass, 'token'): user_invite.token = token db_adapter.commit() try: # Send 'invite' email emails.send_invite_email(user_invite, accept_invite_link) except Exception as e: # delete new User object if send fails db_adapter.delete_object(user_invite) db_adapter.commit() raise signals\ .user_sent_invitation\ .send(current_app._get_current_object(), user_invite=user_invite)
def invite(): """ Allows users to send invitations to register an account """ user_manager = current_app.user_manager next = request.args.get('next', _endpoint_url(user_manager.after_invite_endpoint)) user_role = Role.query.filter_by(name='user').first() invite_form = MyInviteForm(request.form) invite_form.role_id.choices = [(text_type(r.id), text_type(r.name)) for r in db.session.query(Role).filter(and_(Role.name != 'cron', Role.name != 'admin')).order_by('name')] invite_form.role_id.default = text_type(user_role.id) if text_type(invite_form.role_id.data) == 'None': invite_form.role_id.data = text_type(user_role.id) if request.method=='POST' and invite_form.validate(): email = invite_form.email.data the_role_id = None for role in Role.query.order_by('id'): if role.id == int(invite_form.role_id.data) and role.name != 'admin' and role.name != 'cron': the_role_id = role.id if the_role_id is None: the_role_id = user_role.id user, user_email = user_manager.find_user_by_email(email) if user: flash(word("A user with that e-mail has already registered"), "error") return redirect(url_for('invite')) else: user_invite = MyUserInvitation(email=email, role_id=the_role_id, invited_by_user_id=current_user.id) db.session.add(user_invite) db.session.commit() token = user_manager.generate_token(user_invite.id) accept_invite_link = url_for('user.register', token=token, _external=True) user_invite.token = token db.session.commit() #docassemble.webapp.daredis.clear_user_cache() try: logmessage("Trying to send e-mail to " + text_type(user_invite.email)) emails.send_invite_email(user_invite, accept_invite_link) except Exception as e: logmessage("Failed to send e-mail") db.session.delete(user_invite) db.session.commit() #docassemble.webapp.daredis.clear_user_cache() flash(word('Unable to send e-mail. Error was: ') + text_type(e), 'error') return redirect(url_for('invite')) flash(word('Invitation has been sent.'), 'success') return redirect(next) return render_template('flask_user/invite.html', version_warning=None, bodyclass='adminbody', page_title=word('Invite User'), tab_title=word('Invite User'), form=invite_form)
def invite(): """ Allows users to send invitations to register an account """ user_manager = current_app.user_manager next = request.args.get('next', _endpoint_url(user_manager.after_invite_endpoint)) user_role = Role.query.filter_by(name='user').first() invite_form = MyInviteForm(request.form) invite_form.role_id.choices = [(text_type(r.id), text_type(r.name)) for r in db.session.query(Role).filter(and_(Role.name != 'cron', Role.name != 'admin')).order_by('name')] invite_form.role_id.default = text_type(user_role.id) if text_type(invite_form.role_id.data) == 'None': invite_form.role_id.data = text_type(user_role.id) if request.method=='POST' and invite_form.validate(): email = invite_form.email.data the_role_id = None for role in Role.query.order_by('id'): if role.id == int(invite_form.role_id.data) and role.name != 'admin' and role.name != 'cron': the_role_id = role.id if the_role_id is None: the_role_id = user_role.id user, user_email = user_manager.find_user_by_email(email) if user: flash(word("A user with that e-mail has already registered"), "error") return redirect(url_for('invite')) else: user_invite = MyUserInvitation(email=email, role_id=the_role_id, invited_by_user_id=current_user.id) db.session.add(user_invite) db.session.commit() token = user_manager.generate_token(user_invite.id) accept_invite_link = url_for('user.register', token=token, _external=True) user_invite.token = token db.session.commit() #docassemble.webapp.daredis.clear_user_cache() try: logmessage("Trying to send e-mail to " + text_type(user_invite.email)) emails.send_invite_email(user_invite, accept_invite_link) except Exception as e: logmessage("Failed to send e-mail") db.session.delete(user_invite) db.session.commit() #docassemble.webapp.daredis.clear_user_cache() flash(word('Unable to send e-mail. Error was: ') + text_type(e), 'error') return redirect(url_for('invite')) flash(word('Invitation has been sent.'), 'success') return redirect(next) return render_template('flask_user/invite.html', version_warning=None, bodyclass='daadminbody', page_title=word('Invite User'), tab_title=word('Invite User'), form=invite_form)
def invite(): """ Allows users to send invitations to register an account """ user_manager = current_app.user_manager next = request.args.get('next', _endpoint_url(user_manager.after_invite_endpoint)) user_role = Role.query.filter_by(name='user').first() invite_form = MyInviteForm(request.form) invite_form.role_id.choices = [ (text_type(r.id), text_type(r.name)) for r in db.session.query(Role).filter( and_(Role.name != 'cron', Role.name != 'admin')).order_by('name') ] invite_form.role_id.default = text_type(user_role.id) if text_type(invite_form.role_id.data) == 'None': invite_form.role_id.data = text_type(user_role.id) if request.method == 'POST' and invite_form.validate(): email_addresses = list() for email_address in re.split(r'[\n\r]+', invite_form.email.data.strip()): (part_one, part_two) = email.utils.parseaddr(email_address) email_addresses.append(part_two) the_role_id = None for role in Role.query.order_by('id'): if role.id == int( invite_form.role_id.data ) and role.name != 'admin' and role.name != 'cron': the_role_id = role.id if the_role_id is None: the_role_id = user_role.id has_error = False for email_address in email_addresses: user, user_email = user_manager.find_user_by_email(email_address) if user: flash( word("A user with that e-mail has already registered") + " (" + email_address + ")", "error") has_error = True continue else: user_invite = MyUserInvitation( email=email_address, role_id=the_role_id, invited_by_user_id=current_user.id) db.session.add(user_invite) db.session.commit() token = user_manager.generate_token(user_invite.id) accept_invite_link = url_for('user.register', token=token, _external=True) user_invite.token = token db.session.commit() #docassemble.webapp.daredis.clear_user_cache() try: logmessage("Trying to send e-mail to " + text_type(user_invite.email)) emails.send_invite_email(user_invite, accept_invite_link) except Exception as e: try: logmessage("Failed to send e-mail: " + text_type(e)) except: logmessage("Failed to send e-mail") db.session.delete(user_invite) db.session.commit() #docassemble.webapp.daredis.clear_user_cache() flash( word('Unable to send e-mail. Error was: ') + text_type(e), 'error') has_error = True if has_error: return redirect(url_for('invite')) if len(email_addresses) > 1: flash(word('Invitations have been sent.'), 'success') else: flash(word('Invitation has been sent.'), 'success') return redirect(next) response = make_response( render_template('flask_user/invite.html', version_warning=None, bodyclass='daadminbody', page_title=word('Invite User'), tab_title=word('Invite User'), form=invite_form), 200) response.headers[ 'Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0' return response