Python Forum
function-decorator , which is checking an access according to USERNAME
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function-decorator , which is checking an access according to USERNAME
#1
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 wrapper
I would like to ask for helping to correct the code
Reply
#2
Without checking the results, you call def has_access(func)
before you have defined it.
P.
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
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
Reply
#4
(Feb-16-2024, 10:52 AM)DPaul Wrote: Without checking the results, you call def has_access(func)
before you have defined it.
P.

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 defined
I'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 database
To 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?
Reply
#5
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.
Reply
#6
(Feb-16-2024, 01:15 PM)deanhystad Wrote: 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

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
Reply
#7
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python code tp determin a userName and print message jackAmin 4 1,819 Nov-20-2022, 12:03 AM
Last Post: rob101
  Help! Chatting App - Changing the "IP & Port" into Username Rowan99 0 1,403 Dec-20-2021, 07:02 AM
Last Post: Rowan99
  Decorator for a function with argument(s) banidjamali 1 1,829 Feb-09-2021, 11:55 AM
Last Post: Gribouillis
  i got a decorator question yosef 3 2,568 Aug-25-2020, 04:14 PM
Last Post: DeaD_EyE
  Decorator is using in class,but not working mbilalshafiq 2 2,111 Jul-04-2020, 08:53 PM
Last Post: mbilalshafiq
  Decorator and namespace. JayIvhen 2 2,766 Oct-26-2018, 03:56 PM
Last Post: nilamo
  Password and Username Verification AlwaysNew 4 17,164 Nov-12-2017, 11:51 AM
Last Post: wavic
  Username and password Steve2017 13 9,758 Sep-03-2017, 09:17 PM
Last Post: Steve2017
  python decorator alfredocabrera 0 3,132 Feb-22-2017, 07:04 AM
Last Post: alfredocabrera

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020