2013-04-29 11:05:09 +02:00
|
|
|
from floatapp import app, login_manager
|
|
|
|
from flask import request, abort
|
|
|
|
from flask_login import current_user, UserMixin
|
|
|
|
from functools import wraps
|
|
|
|
|
2018-11-30 01:28:24 +01:00
|
|
|
|
2013-04-29 11:05:09 +02:00
|
|
|
class User(UserMixin):
|
2018-11-30 01:28:24 +01:00
|
|
|
def __init__(self, id, admin=False):
|
|
|
|
self.admin = admin
|
|
|
|
self.id = id
|
2013-04-29 11:05:09 +02:00
|
|
|
|
|
|
|
photo_user = User("user")
|
|
|
|
admin_user = User("admin", True)
|
|
|
|
|
2018-11-30 01:28:24 +01:00
|
|
|
|
2013-04-29 11:05:09 +02:00
|
|
|
@login_manager.user_loader
|
|
|
|
def load_user(id):
|
2018-11-30 01:28:24 +01:00
|
|
|
if id == "user":
|
|
|
|
return photo_user
|
|
|
|
elif id == "admin":
|
|
|
|
return admin_user
|
|
|
|
return None
|
|
|
|
|
2013-04-29 11:05:09 +02:00
|
|
|
|
|
|
|
@login_manager.unauthorized_handler
|
|
|
|
def unauthorized():
|
2018-11-30 01:28:24 +01:00
|
|
|
return abort(403)
|
|
|
|
|
2013-04-29 11:05:09 +02:00
|
|
|
|
|
|
|
def login_required(fn):
|
2018-11-30 01:28:24 +01:00
|
|
|
@wraps(fn)
|
|
|
|
def decorated_view(*args, **kwargs):
|
|
|
|
if (query_is_admin_user(request.args) or
|
|
|
|
query_is_photo_user(request.args) or
|
|
|
|
current_user.is_authenticated):
|
|
|
|
return fn(*args, **kwargs)
|
|
|
|
return app.login_manager.unauthorized()
|
|
|
|
return decorated_view
|
|
|
|
|
2013-04-29 11:05:09 +02:00
|
|
|
|
|
|
|
def admin_required(fn):
|
2018-11-30 01:28:24 +01:00
|
|
|
@wraps(fn)
|
|
|
|
def decorated_view(*args, **kwargs):
|
|
|
|
if (query_is_admin_user(request.args) or
|
|
|
|
(current_user.is_authenticated and current_user.admin)):
|
|
|
|
return fn(*args, **kwargs)
|
|
|
|
return app.login_manager.unauthorized()
|
|
|
|
return decorated_view
|
|
|
|
|
2013-04-29 11:05:09 +02:00
|
|
|
|
|
|
|
def query_is_photo_user(query):
|
2018-11-30 01:28:24 +01:00
|
|
|
username = query.get("username", None)
|
|
|
|
password = query.get("password", None)
|
|
|
|
return username == (app.config["PHOTO_USERNAME"] and
|
|
|
|
password == app.config["PHOTO_PASSWORD"])
|
|
|
|
|
2013-04-29 11:05:09 +02:00
|
|
|
|
|
|
|
def query_is_admin_user(query):
|
2018-11-30 01:28:24 +01:00
|
|
|
username = query.get("username", None)
|
|
|
|
password = query.get("password", None)
|
|
|
|
return username == (app.config["ADMIN_USERNAME"] and
|
|
|
|
password == app.config["ADMIN_PASSWORD"])
|
|
|
|
|
2013-04-29 11:05:09 +02:00
|
|
|
|
|
|
|
def is_authenticated():
|
2018-11-30 01:28:24 +01:00
|
|
|
return (query_is_admin_user(request.args) or
|
|
|
|
query_is_photo_user(request.args) or
|
|
|
|
current_user.is_authenticated)
|