function-decorator , which is checking an access according to USERNAME - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: function-decorator , which is checking an access according to USERNAME (/thread-41604.html) |
function-decorator , which is checking an access according to USERNAME - Liki - Feb-16-2024 Hallo, I'm asking for help for followed task: """ Realize a function-decorator , which is checking an access according to USERNAME All USERNAMEs are saved as global in the List USERS If User is agree for authorisation - he should input his username (saved as global) Funktion should use two decorators: one for checking of authorisation, second - for check of access """ Why is error outputed?: USERS = ['admin', 'guest', 'director', 'root', 'superstar'] yesno = input(""" Input Y if you want to authorisate yourself or input N if you want to work as an anonyme User """) auth = yesno == "Y" if auth: username = input("Input your username:") def is_auth(func): def wrapper(): if auth: print("User is authorised") func() else: print("User is not authorised. Function will not performed") return wrapper @is_auth @has_access def from_db(): print("some data from database") from_db() ###: def has_access(func): def wrapper(): if username in USERS: print("Authorised as", username) func() else: print("Access for user", username, "is not allowed") return wrapperI would like to ask for helping to correct the code RE: function-decorator , which is checking an access according to USERNAME - DPaul - Feb-16-2024 Without checking the results, you call def has_access(func) before you have defined it. P. RE: function-decorator , which is checking an access according to USERNAME - deanhystad - Feb-16-2024 This code belongs inside your is_auth() decorator yesno = input(""" Input Y if you want to authorisate yourself or input N if you want to work as an anonyme User """) auth = yesno == "Y" if auth: username = input("Input your username:")This code should be checking if the entered username is in the list of authorized users. def is_auth(func): def wrapper(): if auth: print("User is authorised") func() else: print("User is not authorised. Function will not performed") return wrapper RE: function-decorator , which is checking an access according to USERNAME - Liki - Feb-16-2024 (Feb-16-2024, 10:52 AM)DPaul Wrote: Without checking the results, you call def has_access(func) Thank you. the error Input Y if you want to authorisate yourself or input N if you want to work as an anonyme User Y Input your username:admin Traceback (most recent call last): File "C:/Users/..........py", line 35, in <module> @has_access NameError: name 'has_access' is not definedI'm trying to check the results: a) I inputed "Y" to autorise myself. The code taked it and b) goes to futher step: I inputed username from the list "admin" and the error-message shows on line 35: @has_access In the following code-variante I'm placing def has_access(func):before I calling @has_access: USERS = ['admin', 'guest', 'director', 'root', 'superstar'] yesno = input(""" Input Y if you want to authorisate yourself or iput N if you want to work as an anonyme User """) auth = yesno == "Y" def is_auth(func): def wrapper(): if auth: print("User is authorised") func() else: print("User is not authorised. Function will not performed") return wrapper if auth: username = input("Input your username:") ###: def has_access(func): def wrapper(): if username in USERS: print("Authorised as", username) func() else: print("Access for user", username, "don't allowed") return wrapper @is_auth @has_access def from_db(): print("some data from database") from_db()Is the code now correct ? The code is performed: Input Y if you want to authorisate yourself or iput N if you want to work as an anonyme User Y Input your username:admin User is authorised Authorised as admin some data from databaseTo unterstand in full... my futher question to def from_db(): print("some data from database") from_db()what should hier happen? Should this function output an symbolical text as if some data from database was printed after authorisation "Y" and after input of username "admin" from the list? RE: function-decorator , which is checking an access according to USERNAME - deanhystad - Feb-16-2024 No, this code really needs to be in your is_auth() decorator. yesno = input(""" Input Y if you want to authorisate yourself or iput N if you want to work as an anonyme User """) auth = yesno == "Y" if auth: username = input("Input your username:")The only code that should not be in your decorator is this: USERS = ['admin', 'guest', 'director', 'root', 'superstar'] @is_auth @has_access def from_db(): print("some data from database") from_db()I would drop the prompt asking if the user wants to enter a username. I would write is_auth to ask the user to enter a username. They can give consent by entering a name or decline by not entering a name. The is_auth decorator only calls the wrapped function if a username is entered. RE: function-decorator , which is checking an access according to USERNAME - Liki - Feb-16-2024 (Feb-16-2024, 01:15 PM)deanhystad Wrote: This code belongs inside your is_auth() decorator Thank you. this code works correct also if it is outside of is_auth() decorator Isn't it?: USERS = ['admin', 'guest', 'director', 'root', 'superstar'] yesno = input(""" Input Y if you want to authorisate yourself or iput N if you want to work as an anonyme User """) auth = yesno == "Y" def is_auth(func): def wrapper(): if auth: print("User is authorised") func() else: print("User is not authorised. Function will not performed") return wrapper if auth: username = input("Input your username:") ###: def has_access(func): def wrapper(): if username in USERS: print("Authorised as", username) func() else: print("Access for user", username, "don't allowed") return wrapper @is_auth @has_access def from_db(): print("some data from database") from_db() Input Y if you want to authorisate yourself or iput N if you want to work as an anonyme User Y Input your username:root User is authorised Authorised as root some data from database RE: function-decorator , which is checking an access according to USERNAME - deanhystad - Feb-17-2024 No, it does not work correctly. Try this: @is_auth @has_access def from_db(): print("some data from database") @has_access def open_forum(): print("log onto the forum") from_db() open_forum()calling from_db() and open_forum() should each ask if you want to enter credentials. Your code works for one test, it needs to work for all tests. |