(Ich verwende Flask mit Sitzung)
Mit dieser Funktion oben,
aktualisiert, (beschleunigte Überarbeitung nach Tim Roberts Vorschlag):
Code: Select all
db = SQL("sqlite:///images.db")
def chkAdmin(adm):
print("debug chkAdmin", adm)
if adm[0] is None:
return redirect(url_for("login"))
username= getUsername(adm[0])
path = adm[1]
oldpath = path
if db.execute("SELECT username FROM users WHERE id = ?", 1):
if username == "admin":
if path == "/profile":
path = "/admin_p"
if path == "/register":
print("adminPage")
path = "/admin"
print(f"debug: Admin on 1 OK! ({oldpath} {path}) \n adm: {adm}, {username}, {path}")
return path
crateTabls()
return "/admin"
Code: Select all
@app.route("/profile", methods=["GET", "POST"])
@login_required
def profile():
user_id = session["user_id"]
# - - - - -
adm = [user_id, "/profile"] # session check
chk = chkAdmin(adm)
if chk != adm[1]:
return redirect(chk) # no main session
#...
Code: Select all
# - - - - -
adm = [user_id, "/profile"] # here adm[1]
if chkAdmin(adm) != adm[1]: # (1) here chkAdmin(adm)
return redirect(chkAdmin(adm))
(1)Kann ich etwas schreiben, bei dem ich die Funktion chkAdmin(adm) nicht zweimal eingeben muss?
Mobile version