Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- encoding: UTF-8 -*-
- #
- import datetime
- from models.db import User, session as db_session
- from models.forms import LoginForm
- from flask import render_template, request, url_for, session, Module, redirect
- auth = Module(__name__)
- SESSION_KEY = 'auth_session'
- UNKNOWN_EMAIL_MESSAGE = 'Unknown e-mail address'
- INCORRECT_PASSWORD_MESSAGE = 'Incorrect password'
- def check_credentials(email, password):
- """
- Verifies credentials for email and password.
- Returns user id on successful login else returns the error message.
- """
- try:
- user = User.query.filter_by(email=email).one()
- except: # no user found
- return UNKNOWN_EMAIL_MESSAGE
- if user._encrypt_password(password) == user.password: # select the first record and encrypt pass
- return user.id # successful login
- else:
- return INCORRECT_PASSWORD_MESSAGE
- def check_auth(func):
- """
- A decorator which checks whether the user is logged in or not.
- """
- def wrap_f(*args, **kwargs):
- if session.get(SESSION_KEY, None): # user is logged in
- func(*args, **kwargs)
- else:
- return redirect(url_for('auth.login', redir=request.url))
- return wrap_f
- def auth_conditions(*conditions):
- """
- Specify some conditions for the auth
- """
- def wrap(func):
- def wrap_f(*args, **kwargs):
- for condition in conditions:
- if not condition():
- abort(403)
- func(*args, **kwargs)
- return wrap_f
- return wrap
- def user_id():
- """
- Return the user id
- """
- return session.get(SESSION_KEY) # get userid from session
- # Conditions are callables that return True
- # if the user fulfills the conditions they define, False otherwise
- #
- def is_admin():
- """
- Tell if the user is an administrator
- """
- return User.get_by(id=session[SESSION_KEY]).is_admin
- def account_not_expired():
- """
- Make sure that the account hasn't yet expired
- """
- return User.get_by(id=session[SESSION_KEY]).days_left() is not 0
- ### The controller functions
- def on_login(user_id):
- """Called on successful login"""
- user = User.get_by(id=user_id)
- user.last_login = datetime.datetime.now() # set the last_login date/time
- db_session.commit()
- def on_logout(user_id):
- """Called on logout"""
- @auth.route('/login', methods=['POST', 'GET'])
- def login():
- """
- Log the user in.
- """
- form = LoginForm()
- if form.validate_on_submit():
- check = check_credentials(form.email.data, form.password.data)
- if check == UNKNOWN_EMAIL_MESSAGE:
- form.email.errors.append(UNKNOWN_EMAIL_MESSAGE)
- elif check == INCORRECT_PASSWORD_MESSAGE:
- form.password.errors.append(INCORRECT_PASSWORD_MESSAGE)
- else:
- session[SESSION_KEY] = check # put the user id
- if form.remember_me.data == True: # if remember_me is set
- session[SESSION_KEY].permanent = True # set cookie for 31 days
- on_login(check)
- return redirect(request.args.get('redir', url_for('site.index')))
- return render_template('auth/login.html', form=form)
- @auth.route('/logout')
- def logout():
- """
- Log the user out deleting the session key.
- """
- session.pop(SESSION_KEY, None)
- on_logout(user_id)
- return redirect(url_for('site.index')) # redirect to homepage
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement