0
0
Flaskframework~10 mins

Decorator for role requirement in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Decorator for role requirement
Define decorator function
Decorator wraps target function
On function call: Check user role
Run function
Function ends
The decorator wraps a function to check user roles before running it. If the user has the right role, the function runs; otherwise, access is denied.
Execution Sample
Flask
def role_required(role):
    def decorator(func):
        def wrapper(*args, **kwargs):
            if current_user.role == role:
                return func(*args, **kwargs)
            return 'Access Denied', 403
        return wrapper
    return decorator
This code defines a decorator that checks if the current user has the required role before running the decorated function.
Execution Table
StepActionCheckResultFunction Call Outcome
1Call decorated functionN/AEnter wrapperWaiting for role check
2Check current_user.role == 'admin'current_user.role='admin'TrueCall original function
3Original function runsN/AN/AReturns function output
4Call decorated functionN/AEnter wrapperWaiting for role check
5Check current_user.role == 'admin'current_user.role='guest'FalseReturn 'Access Denied', 403
💡 Execution stops after returning function output or access denied response.
Variable Tracker
VariableStartAfter Step 2After Step 5Final
current_user.roleundefined'admin''guest'Depends on user
wrapper returnundefinedFunction output'Access Denied', 403Depends on role check
Key Moments - 2 Insights
Why does the original function only run if the role matches?
Because the wrapper checks current_user.role before calling the original function (see execution_table step 2). If the check fails, it returns access denied instead.
What happens if the user role is not set or different?
The wrapper returns 'Access Denied', 403 immediately without running the original function (see execution_table step 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the outcome at step 3 when the role matches?
AThe wrapper function exits without calling the original function
BAccess Denied error is returned
CThe original function runs and returns its output
DAn exception is raised
💡 Hint
Check the 'Function Call Outcome' column at step 3 in execution_table
At which step does the role check fail and access is denied?
AStep 5
BStep 4
CStep 2
DStep 1
💡 Hint
Look at the 'Check' and 'Result' columns in execution_table rows for step 5
If current_user.role was 'admin' but the decorator required 'guest', what would happen?
AFunction runs normally
BAccess Denied is returned
CAn error is raised
DThe wrapper skips the role check
💡 Hint
Role must match exactly for function to run, see execution_table step 2 logic
Concept Snapshot
Decorator for role requirement:
- Define a decorator that takes a role string
- Inside, define a wrapper function
- Wrapper checks current_user.role against required role
- If match, call original function
- Else, return access denied response
- Use @role_required('role') above route functions
Full Transcript
This visual execution trace shows how a Flask decorator checks user roles before running a function. The decorator wraps the function and on each call, it compares the current user's role to the required role. If they match, the original function runs and returns its output. If not, the wrapper returns an 'Access Denied' message with a 403 status code. The execution table tracks each step: entering the wrapper, checking the role, and either running the function or denying access. The variable tracker shows how current_user.role and the wrapper's return value change during execution. Key moments clarify why the function only runs on role match and what happens if roles differ. The quiz tests understanding of these steps and outcomes. This pattern helps secure Flask routes by role-based access control.