Consider this Flask route that checks user roles before allowing access. What will be the response if the user role is 'guest'?
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'
Think about what happens when the role is not 'admin'.
The route aborts with a 403 error if the user role is anything other than 'admin'. Since the role is 'guest', access is denied.
Given this snippet, what is the value of access_granted after execution?
user_roles = ['editor', 'contributor'] required_role = 'admin' access_granted = required_role in user_roles
Check if 'admin' is inside the list user_roles.
The list user_roles does not contain 'admin', so the expression evaluates to false.
Which code snippet correctly implements a Flask decorator @role_required('admin') that aborts with 403 if the user role is not 'admin'?
Remember a decorator returns a function that wraps the original function and accepts any arguments.
Option B correctly defines a decorator factory that returns a decorator, which returns a wrapper function accepting any arguments and performs the role check before calling the original function.
Given this Flask route, why does it raise a RuntimeError: Working outside of request context?
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'
Think about when Flask's request object is available.
The request object is only available during an active HTTP request. Accessing it at the module level (outside any route) causes a RuntimeError.
Choose the most accurate description of how RBAC is typically implemented in Flask applications.
Think about how roles are checked before allowing access.
RBAC in Flask usually involves checking user roles stored in session data or tokens before allowing access to certain routes or functions.