Bird
Raised Fist0
Flaskframework~20 mins

Role-based access control in Flask - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
RBAC Mastery
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 that checks user roles before allowing access. What will be the response if the user role is 'guest'?

Flask
from flask import Flask, abort, request
app = Flask(__name__)

@app.route('/dashboard')
def dashboard():
    user_role = request.args.get('role')
    if user_role != 'admin':
        abort(403)
    return 'Welcome to admin dashboard!'

# Simulate request with role='guest'
AHTTP 403 Forbidden error
BWelcome to admin dashboard!
CHTTP 404 Not Found error
DHTTP 500 Internal Server Error
Attempts:
2 left
💡 Hint

Think about what happens when the role is not 'admin'.

❓ state_output
intermediate
1:30remaining
What is the value of 'access_granted' after this role check?

Given this snippet, what is the value of access_granted after execution?

Flask
user_roles = ['editor', 'contributor']
required_role = 'admin'
access_granted = required_role in user_roles
Atrue
Bnull
C'admin'
Dfalse
Attempts:
2 left
💡 Hint

Check if 'admin' is inside the list user_roles.

📝 Syntax
advanced
2:30remaining
Which option correctly defines a Flask decorator to restrict access by role?

Which code snippet correctly implements a Flask decorator @role_required('admin') that aborts with 403 if the user role is not 'admin'?

A
def role_required(role):
    def decorator(f):
        if request.args.get('role') != role:
            abort(403)
        return f
    return decorator
B
def role_required(role):
    def decorator(f):
        def wrapper(*args, **kwargs):
            if request.args.get('role') != role:
                abort(403)
            return f(*args, **kwargs)
        return wrapper
    return decorator
C
def role_required(role):
    def wrapper(f):
        if request.args.get('role') != role:
            abort(403)
        return f
    return wrapper
D
def role_required(role):
    def decorator(f):
        def wrapper():
            if request.args.get('role') != role:
                abort(403)
            return f()
        return wrapper
    return decorator
Attempts:
2 left
💡 Hint

Remember a decorator returns a function that wraps the original function and accepts any arguments.

🔧 Debug
advanced
2:00remaining
Why does this Flask role check code raise a RuntimeError?

Given this Flask route, why does it raise a RuntimeError: Working outside of request context?

Flask
from flask import Flask, abort, request
app = Flask(__name__)

user_role = request.args.get('role')

@app.route('/profile')
def profile():
    if user_role != 'user':
        abort(403)
    return 'User profile page'
ABecause 'request.args.get' is called outside a request context, before any request is handled.
BBecause 'abort' is used incorrectly inside the route function.
CBecause the route function 'profile' is missing a return statement.
DBecause Flask app is not run with debug=true.
Attempts:
2 left
💡 Hint

Think about when Flask's request object is available.

🧠 Conceptual
expert
1:30remaining
Which statement best describes role-based access control (RBAC) in Flask apps?

Choose the most accurate description of how RBAC is typically implemented in Flask applications.

ARBAC automatically encrypts user passwords to secure roles in Flask apps.
BRBAC requires Flask to use class-based views exclusively for role checks.
CRBAC restricts access by checking user roles stored in session or token before executing route logic.
DRBAC is handled by Flask's built-in user interface components.
Attempts:
2 left
💡 Hint

Think about how roles are checked before allowing access.

Practice

(1/5)
1. What is the main purpose of role-based access control (RBAC) in a Flask application?
easy
A. To automatically generate user interfaces
B. To speed up the Flask server response time
C. To restrict user actions based on their assigned roles
D. To store user passwords securely

Solution

  1. Step 1: Understand RBAC concept

    RBAC means users can only do things allowed by their roles, like admin or user.
  2. Step 2: Identify RBAC purpose in Flask

    In Flask, RBAC limits access to routes or actions based on roles.
  3. Final Answer:

    To restrict user actions based on their assigned roles -> Option C
  4. Quick Check:

    RBAC = restrict actions by role [OK]
Hint: RBAC controls what users can do by their role [OK]
Common Mistakes:
  • Confusing RBAC with performance optimization
  • Thinking RBAC manages UI generation
  • Mixing RBAC with password security
2. Which Flask decorator is commonly used to protect a route so only logged-in users with a specific role can access it?
easy
A. @csrf_protect
B. @login_required
C. @app.route
D. @role_required('admin')

Solution

  1. Step 1: Identify decorators for access control

    @login_required ensures user is logged in but does not check role.
  2. Step 2: Find decorator checking specific role

    @role_required('admin') restricts access to users with 'admin' role.
  3. Final Answer:

    @role_required('admin') -> Option D
  4. Quick Check:

    Role check decorator = @role_required [OK]
Hint: Use @role_required to check user roles on routes [OK]
Common Mistakes:
  • Using @login_required alone for role checks
  • Confusing @app.route with access control
  • Mistaking @csrf_protect for role restriction
3. Given this Flask route code snippet:
@app.route('/dashboard')
@role_required('editor')
def dashboard():
    return 'Welcome Editor'

What happens if a logged-in user with role 'viewer' tries to access '/dashboard'?
medium
A. They get redirected or denied access
B. They can access but with limited features
C. They get a server error
D. They see 'Welcome Editor' message

Solution

  1. Step 1: Understand role_required decorator effect

    @role_required('editor') allows only users with 'editor' role to access the route.
  2. Step 2: Check user role 'viewer' against required 'editor'

    User role 'viewer' does not match 'editor', so access is denied or redirected.
  3. Final Answer:

    They get redirected or denied access -> Option A
  4. Quick Check:

    Role mismatch = access denied [OK]
Hint: Role mismatch blocks access, not error or partial view [OK]
Common Mistakes:
  • Assuming wrong role still shows content
  • Expecting server error on role mismatch
  • Thinking partial access is automatic
4. Consider this Flask code snippet:
@app.route('/admin')
@role_required('admin')
def admin_panel():
    return 'Admin Panel'

Which of the following is a common mistake that would cause this role check to fail?
medium
A. Not importing the role_required decorator
B. Using @app.route after @role_required
C. Returning a string instead of JSON
D. Defining the function without parentheses

Solution

  1. Step 1: Check decorator usage

    role_required must be imported to work; missing import causes failure.
  2. Step 2: Verify other options

    @app.route order is flexible; returning string is valid; function definition syntax is correct.
  3. Final Answer:

    Not importing the role_required decorator -> Option A
  4. Quick Check:

    Missing import breaks decorator [OK]
Hint: Always import decorators before use [OK]
Common Mistakes:
  • Assuming decorator order always matters
  • Confusing return type with access control
  • Syntax errors in function definition
5. You want to create a Flask route /settings that only users with roles 'admin' or 'manager' can access. Which code snippet correctly implements this role-based access control?
hard
A.
@app.route('/settings')
@role_required('admin')
@role_required('manager')
def settings():
    return 'Settings Page'
B.
@app.route('/settings')
def settings():
    if current_user.role in ['admin', 'manager']:
        return 'Settings Page'
    return 'Access Denied'
C.
@app.route('/settings')
@role_required(['admin', 'manager'])
def settings():
    return 'Settings Page'
D.
@app.route('/settings')
@role_required('admin' or 'manager')
def settings():
    return 'Settings Page'

Solution

  1. Step 1: Analyze multiple role checks

    @app.route('/settings')
    @role_required('admin')
    @role_required('manager')
    def settings():
        return 'Settings Page'
    stacks decorators, which requires both roles simultaneously, not either/or.
  2. Step 2: Check list role check in decorator

    @app.route('/settings')
    @role_required(['admin', 'manager'])
    def settings():
        return 'Settings Page'
    assumes role_required accepts list, which may not be supported by default.
  3. Step 3: Evaluate logical or misuse

    @app.route('/settings')
    @role_required('admin' or 'manager')
    def settings():
        return 'Settings Page'
    uses 'admin' or 'manager' which evaluates to 'admin' always, incorrect.
  4. Step 4: Confirm manual role check

    @app.route('/settings')
    def settings():
        if current_user.role in ['admin', 'manager']:
            return 'Settings Page'
        return 'Access Denied'
    manually checks if user role is in allowed list, correctly allowing either role.
  5. Final Answer:

    Option B code with manual role check -> Option B
  6. Quick Check:

    Manual role list check allows multiple roles [OK]
Hint: Use if-check for multiple roles if decorator lacks list support [OK]
Common Mistakes:
  • Stacking decorators for multiple roles (AND instead of OR)
  • Passing list directly to decorator without support
  • Using 'or' inside decorator argument incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard