File size: 1,019 Bytes
428a607
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from flask_login import LoginManager, current_user
from models.user import User
from functools import wraps
from firebase_config import firebase_db
from flask import redirect, url_for


login_manager = LoginManager()

def init_middleware(app):
    login_manager.init_app(app)
    

@login_manager.user_loader
def load_user(id):    
    user_doc = firebase_db.collection('users').document(id).get()

    if user_doc.exists:
        user_data = user_doc.to_dict()
        return User(id=id, nama=user_data['nama'], email=user_data['email'], role=user_data['role'], registered_at=user_data['registered_at'], inactive=user_data['inactive'])
    
    return None


def role_required(*required_roles):
    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            if not current_user.is_authenticated or current_user.role not in required_roles:
                return redirect(url_for('unauthorized'))
            return func(*args, **kwargs)
        return wrapper
    
    return decorator