|
| 1 | +# Define a decorator function that takes a function as input |
| 2 | +def login_required(func): |
| 3 | + # Define a new function that wraps the original function |
| 4 | + def wrapper(*args, **kwargs): |
| 5 | + # Check if the user is logged in |
| 6 | + if user_is_logged_in(): |
| 7 | + # If the user is logged in, call the original function |
| 8 | + return func(*args, **kwargs) |
| 9 | + else: |
| 10 | + # If the user is not logged in, raise an exception or redirect to login page |
| 11 | + raise Exception("User not logged in") |
| 12 | + # Return the new function |
| 13 | + return wrapper |
| 14 | + |
| 15 | +# Define a function that requires login |
| 16 | +@login_required |
| 17 | +def do_something_secure(): |
| 18 | + # This function can only be called if the user is logged in |
| 19 | + pass |
| 20 | + |
| 21 | +def delete_account(user_id): |
| 22 | + if current_user_id() == user_id: |
| 23 | + delete_user_account(user_id) |
| 24 | + else: |
| 25 | + raise Exception("You can only delete your own account") |
| 26 | + |
| 27 | +# Define a global variable to store the current user ID |
| 28 | +current_user = None |
| 29 | + |
| 30 | +# Define a function to log in a user |
| 31 | +def login(user_id): |
| 32 | + global current_user |
| 33 | + current_user = user_id |
| 34 | + |
| 35 | +# Define a function to log out the current user |
| 36 | +def logout(): |
| 37 | + global current_user |
| 38 | + current_user = None |
| 39 | + |
| 40 | +# Define a function to check if the user is logged in |
| 41 | +def user_is_logged_in(): |
| 42 | + global current_user |
| 43 | + return current_user is not None |
| 44 | + |
| 45 | +# Define a function to get the ID of the currently logged in user |
| 46 | +def current_user_id(): |
| 47 | + global current_user |
| 48 | + return current_user |
| 49 | + |
| 50 | +# Define a function to delete a user account |
| 51 | +def delete_user_account(user_id): |
| 52 | + global current_user |
| 53 | + if current_user == user_id: |
| 54 | + print(f"Deleting user account with ID {user_id}") |
| 55 | + current_user = None |
| 56 | + else: |
| 57 | + raise Exception("You can only delete your own account") |
| 58 | + |
| 59 | +# Define the login_required decorator |
| 60 | +def login_required(func): |
| 61 | + def wrapper(*args, **kwargs): |
| 62 | + if user_is_logged_in(): |
| 63 | + return func(*args, **kwargs) |
| 64 | + else: |
| 65 | + raise Exception("User not logged in") |
| 66 | + return wrapper |
| 67 | + |
| 68 | +# Define a function that requires login |
| 69 | +@login_required |
| 70 | +def delete_account(user_id): |
| 71 | + if current_user_id() == user_id: |
| 72 | + delete_user_account(user_id) |
| 73 | + else: |
| 74 | + raise Exception("You can only delete your own account") |
| 75 | + |
| 76 | +# Test the delete_account function |
| 77 | +login(1) |
| 78 | +delete_account(1) # Should print "Deleting user account with ID 1" |
| 79 | +logout() |
| 80 | + |
| 81 | +try: |
| 82 | + delete_account(2) # Should raise an exception |
| 83 | +except Exception as e: |
| 84 | + print(e) |
| 85 | + |
| 86 | +login(2) |
| 87 | + |
| 88 | +try: |
| 89 | + delete_account(1) # Should raise an exception |
| 90 | +except Exception as e: |
| 91 | + print(e) |
| 92 | + |
| 93 | +delete_account(2) # Should print "Deleting user account with ID 2" |
| 94 | +logout() |
| 95 | + |
0 commit comments