0
0
Flaskframework~10 mins

Decorator for role requirement in Flask - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the decorator function.

Flask
from functools import [1]
Drag options to blanks, or click blank then click option'
Arequest
Bredirect
Cwraps
Dsession
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'request' or 'redirect' instead of 'wraps'.
Forgetting to import 'wraps' causes decorator metadata loss.
2fill in blank
medium

Complete the decorator definition to accept a role parameter.

Flask
def role_required([1]):
    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            # role check logic here
            return func(*args, **kwargs)
        return wrapper
    return decorator
Drag options to blanks, or click blank then click option'
Arole
Bfunc
Cuser
Dpermission
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'func' as parameter instead of 'role'.
Not accepting any parameter in the outer function.
3fill in blank
hard

Fix the error in accessing the current user's roles from Flask's session.

Flask
from flask import session

@role_required('admin')
def admin_panel():
    if 'admin' in session.get([1], []):
        return 'Welcome admin'
    return 'Access denied'
Drag options to blanks, or click blank then click option'
A'roles'
B'permissions'
C'user'
D'user_roles'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong session key like 'user_roles' or 'permissions'.
Forgetting to provide a default empty list in get.
4fill in blank
hard

Fill both blanks to complete the role check and redirect unauthorized users.

Flask
from flask import redirect, url_for

def role_required(role):
    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            if role not in session.get([1], []):
                return redirect(url_for([2]))
            return func(*args, **kwargs)
        return wrapper
    return decorator
Drag options to blanks, or click blank then click option'
A'roles'
B'user_roles'
C'login'
D'home'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong session keys or redirecting to the wrong page.
Not returning the redirect response.
5fill in blank
hard

Fill all three blanks to create a complete role-required decorator with session check and redirect.

Flask
from functools import wraps
from flask import session, redirect, url_for

def role_required([1]):
    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            if [2] not in session.get([3], []):
                return redirect(url_for('login'))
            return func(*args, **kwargs)
        return wrapper
    return decorator
Drag options to blanks, or click blank then click option'
Arole
C'roles'
D'user_roles'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for the role parameter and check.
Using wrong session keys like 'user_roles'.