def user_login(wags_app, next_page, app=None): if current_user.is_authenticated: return redirect(qualify_url(wags_app, next_page)) form = LoginForm() if form.is_submitted(): if form.validate_on_submit(): user = get_user(user_name=form.username.data) if user is None or not user.check_password(form.password.data): flash('Invalid username or password', 'danger') return render_template('{}/login.html'.format(wags_app), title='Sign In', form=form, wags_app=wags_app, url_for_app=url_for_app) # if wags_app not in [role.role.name for role in user.roles]: # flash('Sorry, you do not have {} access'.format(wags_app)) # return redirect(qualify_url(wags_app)) login_user(user, remember=form.remember_me.data) if not next_page: next_page = qualify_url(wags_app) else: next_page = qualify_url(wags_app, next_page) return redirect(next_page) else: form.populate() return render_template('{}/login.html'.format(wags_app), title='Sign In', form=form, wags_app=wags_app, url_for_app=url_for_app)
def user_register(wags_app, new=True): # Register user (new is True) or reset login if new and current_user.is_authenticated: return redirect(qualify_url(wags_app)) form = RegistrationForm() form_title = 'Register' if new else 'Reset login details' if not new and not form.is_submitted(): form.username.data = current_user.user_name form.email.data = current_user.member.contact.email form.email.render_kw = {'readonly': True} else: if form.validate_on_submit(): member = get_member_by_email(form.email.data) if member: if member.status not in [ MemberStatus.full_member, MemberStatus.overseas_member ]: flash('Sorry, you are not a current member', 'danger') return redirect(qualify_url(wags_app)) if not member.user: user = User(user_name=form.username.data, member_id=member.id) else: user = member.user # get_user(member.user.id) user.user_name = form.username.data user.set_password(form.password.data) role = Role( role=UserRole.admin) if wags_app == 'admin' else Role( role=UserRole.user) if role.role.value not in [r.role.value for r in user.roles]: user.roles.append(role) save_user(user) if new: flash( 'Congratulations, you are now a registered {}!'.format( wags_app), 'success') return redirect(url_for_app(wags_app, 'user_login')) else: flash('Login details reset'.format(wags_app), 'success') return redirect(url_for_app(wags_app, 'members_area')) else: flash( 'Cannot find your membership - please give your WAGS contact email address' ) return render_template('{}/register.html'.format(wags_app), title=form_title, form=form)
def user_logout(wags_app): logout_user() return redirect(qualify_url(wags_app))
from flask import request from flask_login import LoginManager from front_end import login from wags_user import app from globals import config from back_end.interface import get_user role = 'user' login_manager = LoginManager(app) login_manager.login_view = config.qualify_url(role, '/login') login_manager.login_message = 'You must be a WAGS member to access this page' @login_manager.user_loader def load_user(id): return get_user(id=int(id)) @app.route('/login', methods=['GET', 'POST']) def user_login(): next_page = request.args.get('next') return login.user_login(role, next_page, app) @app.route('/logout', methods=['GET', 'POST']) def user_logout(): return login.user_logout(role) @app.route('/register', methods=['GET', 'POST']) def user_register():