0
0
Flaskframework~20 mins

Decorator for role requirement in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Role Requirement Decorator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Flask route with role check?
Consider this Flask route decorated with a role requirement decorator. What will be the HTTP response status code if the user role is 'guest'?
Flask
from flask import Flask, request, abort
app = Flask(__name__)

def role_required(role):
    def decorator(f):
        def wrapper(*args, **kwargs):
            user_role = request.headers.get('Role')
            if user_role != role:
                abort(403)
            return f(*args, **kwargs)
        return wrapper
    return decorator

@app.route('/admin')
@role_required('admin')
def admin_panel():
    return 'Welcome Admin!'

# Assume a request to /admin with header Role: guest
A200 with body 'Welcome Admin!'
B403 Forbidden error
C404 Not Found error
D500 Internal Server Error
Attempts:
2 left
💡 Hint
Think about what happens when the user role does not match the required role.
📝 Syntax
intermediate
2:00remaining
Which decorator syntax correctly preserves function metadata?
You want to create a role_required decorator that preserves the original function's name and docstring. Which option correctly uses functools.wraps?
Flask
from functools import wraps

def role_required(role):
    def decorator(f):
        # Choose the correct wrapper implementation
        pass
    return decorator
A
def wrapper(*args, **kwargs):
    if user_role != role:
        abort(403)
    return f(*args, **kwargs)

return wrapper
B
def wrapper(*args, **kwargs):
    if user_role != role:
        abort(403)
    @wraps(f)
    return f(*args, **kwargs)

return wrapper
C
def wrapper(*args, **kwargs):
    @wraps(f)
    if user_role != role:
        abort(403)
    return f(*args, **kwargs)

return wrapper
D
@wraps(f)
def wrapper(*args, **kwargs):
    if user_role != role:
        abort(403)
    return f(*args, **kwargs)

return wrapper
Attempts:
2 left
💡 Hint
functools.wraps is used as a decorator on the wrapper function.
🔧 Debug
advanced
2:00remaining
Why does this role_required decorator cause a runtime error?
Examine this decorator code. What causes the runtime error when calling the decorated function?
Flask
def role_required(role):
    def decorator(f):
        def wrapper(*args, **kwargs):
            user_role = request.headers.get('Role')
            if user_role != role:
                abort(403)
            return f
        return wrapper
    return decorator
AThe decorator does not accept *args and **kwargs, causing a TypeError.
BThe abort function is called without importing it, causing a NameError.
CThe wrapper returns the function object f instead of calling it, causing a TypeError later.
DThe user_role variable is not defined, causing a NameError.
Attempts:
2 left
💡 Hint
Look at what the wrapper returns after the role check.
🧠 Conceptual
advanced
1:30remaining
What is the main purpose of using a decorator for role requirement in Flask?
Why do developers use decorators like role_required in Flask applications?
ATo add reusable access control logic to routes without repeating code.
BTo automatically generate HTML templates for different user roles.
CTo improve database query performance by caching role data.
DTo replace Flask's built-in routing system with custom routing.
Attempts:
2 left
💡 Hint
Think about how decorators help with code reuse and separation of concerns.
state_output
expert
2:30remaining
What is the output of this Flask app with nested role_required decorators?
Given this Flask app code, what will be the response body when a request with header Role: admin accesses /dashboard?
Flask
from flask import Flask, request, abort
app = Flask(__name__)

def role_required(role):
    def decorator(f):
        def wrapper(*args, **kwargs):
            user_role = request.headers.get('Role')
            if user_role != role:
                abort(403)
            return f(*args, **kwargs)
        return wrapper
    return decorator

@app.route('/dashboard')
@role_required('admin')
@role_required('user')
def dashboard():
    return 'Dashboard Accessed'

# Request header: Role: admin
A403 Forbidden error
B200 OK with body 'Dashboard Accessed'
C500 Internal Server Error
D404 Not Found error
Attempts:
2 left
💡 Hint
Consider the order decorators are applied and how role checks happen.