Пример #1
0
def login(request, role):
    if request.method == 'POST':
        credential = request.get_json()
        # Queries for user in database. Since usernames are unique, if it exists in db it should be the first appearance
        # TODO: consider login options with other fields other that username (ex: email)
        user = User.query.filter_by(username=credential['username']).first()
        print(user.object())
        if user:
            # Check if user role matches with the page trying to login to
            user_role = Role(user.object()['roles'][0])
            if user_role.get_name() == role.get_name():
                global current_user
                # Check if password is valid
                if check_password_hash(user.password, credential['password']):
                    try:
                        remember = credential['remember']
                    except:
                        # If remember field is null, set default to false
                        remember = False
                    login_user(user, remember)
                    return jsonify(result=user.object()), 200

                return jsonify(result="Invalid password"), 401
            return jsonify(result="User trying to login to unathorized page"), 401
        return jsonify(result="User not found"), 401
    return jsonify(result="Is not a Post method, but returns"), 200
Пример #2
0
rbac.set_user_loader(lambda: current_user)
rbac.set_user_model(User)
rbac.set_role_model(Role)

login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = '/login'


@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))


# Initial role for RBAC to work
start = Role('DUMMY')
rbacDummy = User(roles=[start])

# To use this variable write global before the name in the methods
# login_manager.anonymous_user = rbacDummy

# Blueprints to import. Need to be after rbac
from RumboEx.Blueprints.logins import logins
from RumboEx.Blueprints.tasks import tasks
from RumboEx.Blueprints.courses import courses
from RumboEx.Blueprints.student_page import student_page
from RumboEx.Blueprints.appointments import appointments
from RumboEx.Blueprints.users import users

# Register blueprints
app.register_blueprint(logins)
Пример #3
0
def professorlogin():
    return login(request, Role('professor'))
Пример #4
0
def advisorlogin():
    return login(request, Role('advisor'))
Пример #5
0
def mentorlogin():
    return login(request, Role('mentor'))
Пример #6
0
def psychologistlogin():
    return login(request, Role('psychologist'))
Пример #7
0
def counselorlogin():
    return login(request, Role('counselor'))
Пример #8
0
def studentlogin():
    return login(request, Role('student'))
Пример #9
0
def adminlogin():
    return login(request, Role('admin'))