mirror of
https://github.com/StefBuwalda/WebTech.git
synced 2025-10-30 11:19:58 +00:00
Added decorator for admin
This commit is contained in:
6
app.py
6
app.py
@@ -1,15 +1,13 @@
|
||||
from application import app
|
||||
from flask import redirect, url_for
|
||||
from flask_login import current_user # type: ignore
|
||||
from flask_login import current_user, login_required # type: ignore
|
||||
|
||||
|
||||
# home route
|
||||
@app.route("/")
|
||||
@login_required
|
||||
def index():
|
||||
if current_user.is_authenticated:
|
||||
return redirect(url_for("dash.index"))
|
||||
else:
|
||||
return redirect(url_for("auth.login"))
|
||||
|
||||
|
||||
# App deployment
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from flask import Blueprint, render_template
|
||||
from flask_login import login_required # type: ignore
|
||||
from application.dash.models import Service
|
||||
from application.decorators import admin_required
|
||||
|
||||
dash_blueprint = Blueprint("dash", __name__, template_folder="templates")
|
||||
|
||||
@@ -12,3 +13,9 @@ dash_blueprint = Blueprint("dash", __name__, template_folder="templates")
|
||||
def index():
|
||||
services = Service.query.all() # type: ignore
|
||||
return render_template("dashboard.html", services=services)
|
||||
|
||||
|
||||
@dash_blueprint.route("/admin", methods=["GET", "POST"])
|
||||
# @admin_required
|
||||
def admin():
|
||||
return render_template("admin.html")
|
||||
|
||||
16
application/decorators.py
Normal file
16
application/decorators.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from flask_login import current_user
|
||||
from functools import wraps
|
||||
from flask import redirect, url_for, flash
|
||||
|
||||
|
||||
def admin_required(f):
|
||||
@wraps(f)
|
||||
def decorated_function(*args, **kwargs):
|
||||
if not current_user.is_authenticated:
|
||||
return redirect(url_for("login"))
|
||||
if not current_user.is_admin:
|
||||
flash("Admins only!")
|
||||
return redirect(url_for("index"))
|
||||
return f(*args, **kwargs)
|
||||
|
||||
return decorated_function
|
||||
@@ -30,7 +30,7 @@
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="#">Info</a>
|
||||
</li>
|
||||
{% if current_user.is_admin %}
|
||||
{% if current_user.is_authenticated %}
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="{{url_for('dash.admin')}}">Add user</a>
|
||||
</li>
|
||||
|
||||
Reference in New Issue
Block a user