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