A decorator in Flask is a function that wraps another function to add extra behavior without changing the original function's code. It is often used to modify routes or add checks like authentication.
Click to reveal answer
beginner
Why use a role requirement decorator in Flask?
A role requirement decorator checks if a user has the right role before allowing access to a route. It helps protect parts of a web app so only users with certain roles can use them.
Click to reveal answer
intermediate
How does the @wraps decorator help when creating a custom decorator?
The @wraps decorator preserves the original function's name and docstring when it is wrapped by a decorator. This helps with debugging and keeps the function's identity clear.
Click to reveal answer
beginner
What happens if a user without the required role tries to access a Flask route protected by a role requirement decorator?
The decorator will stop the user from accessing the route, usually by returning a 403 Forbidden error or redirecting them to a login or error page.
Click to reveal answer
intermediate
Example: What does this Flask decorator do?
@role_required('admin')
def admin_panel():
pass
This decorator checks if the current user has the 'admin' role before running the admin_panel function. If the user is not an admin, access is denied.
Click to reveal answer
What is the main purpose of a role requirement decorator in Flask?
ATo speed up the Flask app
BTo log user activity
CTo change the URL of a route
DTo check if a user has a specific role before accessing a route
✗ Incorrect
A role requirement decorator ensures only users with the right role can access certain routes.
Which Python module helps preserve function metadata when writing decorators?
Afunctools
Bos
Csys
Djson
✗ Incorrect
The functools module provides the @wraps decorator to preserve function metadata.
If a user lacks the required role, what HTTP status code is commonly returned?
A403 Forbidden
B200 OK
C404 Not Found
D500 Internal Server Error
✗ Incorrect
403 Forbidden indicates the user is not allowed to access the resource.
In Flask, where is the current user's role usually stored for checking in decorators?
AIn the request headers
BIn the session or user object
CIn the URL parameters
DIn the database only
✗ Incorrect
User roles are typically stored in the session or user object accessible during the request.
What does the @wraps decorator do inside a custom decorator?
AChanges the function's behavior
BRuns the function twice
CPreserves the original function's name and docstring
DPrevents the function from running
✗ Incorrect
@wraps keeps the original function's metadata intact after decoration.
Explain how to create a Flask decorator that restricts access based on user roles.
Think about wrapping the route function and checking user roles before running it.
You got /5 concepts.
Describe what happens when a user without the required role tries to access a protected Flask route.
Focus on the flow of control when role validation fails.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of a decorator that checks user roles in a Flask app?
easy
A. To log user activity without restrictions
B. To speed up the execution of functions
C. To change the user role dynamically
D. To restrict access to certain functions based on user roles
Solution
Step 1: Understand the role of decorators in Flask
Decorators wrap functions to add extra behavior, like checking conditions before running the function.
Step 2: Identify the purpose of role checking
Role checking ensures only users with the right permissions can access certain parts of the app.
Final Answer:
To restrict access to certain functions based on user roles -> Option D
Quick Check:
Role check decorator = restrict access [OK]
Hint: Decorators add checks before function runs [OK]
Common Mistakes:
Thinking decorators change user roles
Assuming decorators speed up code
Confusing logging with access control
2. Which of the following is the correct syntax to define a decorator that requires a role named 'admin' in Flask?
easy
A. @require_role(admin)\ndef admin_panel():\n pass
B. def require_role('admin'):\n def admin_panel():\n pass
C. @require_role('admin')\ndef admin_panel():\n pass
D. @require_role['admin']\ndef admin_panel():\n pass
Solution
Step 1: Recognize decorator syntax in Python
Decorators use @ followed by the decorator function call with parentheses if it takes arguments.
Step 2: Check correct argument passing
The role name 'admin' must be passed as a string inside parentheses.
Final Answer:
@require_role('admin')\ndef admin_panel():\n pass -> Option C
Quick Check:
Decorator with parentheses and string argument = correct [OK]
Hint: Decorator with argument uses parentheses and quotes [OK]
Common Mistakes:
Missing parentheses for decorator with arguments
Passing role without quotes
Using square brackets instead of parentheses
3. Given this decorator code snippet, what will be printed when a user with role 'user' tries to access the decorated function?
A. Missing return statement before func call inside wrapper
B. Incorrect use of *args and **kwargs
C. current_user.role should be current_user.roles
D. Decorator should not have nested functions
Solution
Step 1: Check the wrapper function's return behavior
The wrapper calls func(*args, **kwargs) but does not return its result, so the decorated function returns None.
Step 2: Confirm correct fix
Adding 'return' before func call ensures the wrapper returns the function's output properly.
Final Answer:
Missing return statement before func call inside wrapper -> Option A
Quick Check:
Always return function result in wrapper [OK]
Hint: Wrapper must return the wrapped function's result [OK]
Common Mistakes:
Forgetting to return function output
Misusing argument unpacking
Incorrect attribute name for user role
5. You want to create a Flask decorator that allows access if the user role is either 'admin' or 'editor'. Which of these decorator implementations correctly achieves this?
hard
A. def require_roles(*roles):
def decorator(func):
def wrapper(user_role):
if user_role in roles:
return func()
return 'Access Denied'
return wrapper
return decorator
@require_roles('admin', 'editor')
def edit_page():
return 'Edit Page'