# Path: extrapypi/models/user.py
# class User(db.Model):
#     ROLES = [
#         'admin',
#         'developer',
#         'installer',
#         'maintainer'
#     ]
# 
#     id = db.Column(db.Integer, primary_key=True)
#     username = db.Column(db.String(255), nullable=False, unique=True)
#     email = db.Column(db.String(255), nullable=False, unique=True)
#     password_hash = db.Column(db.String(255), nullable=False)
#     is_active = db.Column(db.Boolean, default=True)
#     role = db.Column(db.String(80), nullable=True)
# 
#     @validates('role')
#     def validate_role(self, key, role):
#         assert role in self.ROLES or role is None
#         return role
# 
#     @property
#     def is_authenticated(self):
#         return True
# 
#     @property
#     def is_anonymous(self):
#         return False
# 
#     @property
#     def is_admin(self):
#         return self.role == 'admin'
# 
#     def get_id(self):
#         try:
#             return unicode(self.id)
#         except NameError:
#             return str(self.id)
# 
#     def __repr__(self):
#         return "<User {0.username}>".format(self)


from passlib.apps import custom_app_context
from flask import request, redirect, abort, url_for, current_app as app
from flask_principal import (
    identity_loaded,
    RoleNeed,
    UserNeed,
    Identity,
    identity_changed
)
from extrapypi.models import User
"""Module handling all Flask-Login logic and handlers
"""



def user_loader(user_id):
    """Default user handler, from Flask-Login documentation
    """
    return User.query.get(user_id)


def unauthorized():
    if request.blueprint == 'dashboard':
        return redirect(url_for('dashboard.login', next=request.endpoint))
    abort(401)


@identity_loaded.connect
def on_identity_loaded(sender, identity):
    """Load rights for flask-principal

    Handle only role need and user need
    """

# Path: extrapypi/models/user.py
# class User(db.Model):
#     ROLES = [
#         'admin',
#         'developer',
#         'installer',
#         'maintainer'
#     ]
# 
#     id = db.Column(db.Integer, primary_key=True)
#     username = db.Column(db.String(255), nullable=False, unique=True)
#     email = db.Column(db.String(255), nullable=False, unique=True)
#     password_hash = db.Column(db.String(255), nullable=False)
#     is_active = db.Column(db.Boolean, default=True)
#     role = db.Column(db.String(80), nullable=True)
# 
#     @validates('role')
#     def validate_role(self, key, role):
#         assert role in self.ROLES or role is None
#         return role
# 
#     @property
#     def is_authenticated(self):
#         return True
# 
#     @property
#     def is_anonymous(self):
#         return False
# 
#     @property
#     def is_admin(self):
#         return self.role == 'admin'
# 
#     def get_id(self):
#         try:
#             return unicode(self.id)
#         except NameError:
#             return str(self.id)
# 
#     def __repr__(self):
#         return "<User {0.username}>".format(self)


# Path: extrapypi/commons/login.py
from passlib.apps import custom_app_context
from flask import request, redirect, abort, url_for, current_app as app
from flask_principal import (
    identity_loaded,
    RoleNeed,
    UserNeed,
    Identity,
    identity_changed
)
from extrapypi.models import User

"""Module handling all Flask-Login logic and handlers
"""



def user_loader(user_id):
    """Default user handler, from Flask-Login documentation
    """
    return User.query.get(user_id)


def unauthorized():
    if request.blueprint == 'dashboard':
        return redirect(url_for('dashboard.login', next=request.endpoint))
    abort(401)


@identity_loaded.connect
def on_identity_loaded(sender, identity):
    """Load rights for flask-principal

    Handle only role need and user need
    """

    user = User.query.get(identity.id)