2020-09-15 07:43:36 +00:00
|
|
|
import ldap
|
2020-08-19 14:20:57 +00:00
|
|
|
from functools import wraps
|
|
|
|
from flask import session, abort
|
2020-10-21 12:04:40 +00:00
|
|
|
from canaille.models import User
|
2020-08-19 14:20:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
def current_user():
|
2020-09-15 07:43:36 +00:00
|
|
|
if "user_dn" not in session:
|
|
|
|
return None
|
|
|
|
|
2020-11-25 16:41:03 +00:00
|
|
|
user = User.get(dn=session["user_dn"])
|
|
|
|
|
|
|
|
if not user:
|
|
|
|
del session["user_dn"]
|
|
|
|
|
|
|
|
return user
|
2020-08-19 14:20:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
def user_needed():
|
|
|
|
def wrapper(view_function):
|
|
|
|
@wraps(view_function)
|
|
|
|
def decorator(*args, **kwargs):
|
2020-08-27 08:50:50 +00:00
|
|
|
user = current_user()
|
|
|
|
if not user:
|
2020-08-19 14:20:57 +00:00
|
|
|
abort(403)
|
2020-08-27 08:50:50 +00:00
|
|
|
return view_function(*args, user=user, **kwargs)
|
2020-08-19 14:20:57 +00:00
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
2020-11-02 11:13:03 +00:00
|
|
|
def moderator_needed():
|
|
|
|
def wrapper(view_function):
|
|
|
|
@wraps(view_function)
|
|
|
|
def decorator(*args, **kwargs):
|
|
|
|
user = current_user()
|
|
|
|
if not user or not user.moderator:
|
|
|
|
abort(403)
|
|
|
|
return view_function(*args, user=user, **kwargs)
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
2020-08-19 14:20:57 +00:00
|
|
|
def admin_needed():
|
|
|
|
def wrapper(view_function):
|
|
|
|
@wraps(view_function)
|
|
|
|
def decorator(*args, **kwargs):
|
|
|
|
user = current_user()
|
2020-08-26 13:37:15 +00:00
|
|
|
if not user or not user.admin:
|
2020-08-19 14:20:57 +00:00
|
|
|
abort(403)
|
2020-10-29 10:09:31 +00:00
|
|
|
return view_function(*args, user=user, **kwargs)
|
2020-08-19 14:20:57 +00:00
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
return wrapper
|