Role-based access control helps decide who can do what in a web app. It keeps things safe by letting only the right people use certain parts.
Role-based access control in Flask
from flask import Flask, redirect, url_for, request from functools import wraps app = Flask(__name__) # Example user data with roles users = { 'alice': 'admin', 'bob': 'editor', 'carol': 'viewer' } # Decorator to check user role def role_required(role): def decorator(f): @wraps(f) def decorated_function(*args, **kwargs): username = request.args.get('user') if not username or users.get(username) != role: return "Access denied", 403 return f(*args, **kwargs) return decorated_function return decorator @app.route('/admin') @role_required('admin') def admin_panel(): return "Welcome to the admin panel!" if __name__ == '__main__': app.run()
The role_required function is a decorator that checks if the user has the right role.
We get the username from the URL query for simplicity, but real apps use login sessions.
admin_page so only users with the 'admin' role can see it.@role_required('admin') def admin_page(): return "Admin only content"
edit_page.@role_required('editor') def edit_page(): return "Editor content"
@role_required('viewer') def view_page(): return "Viewer content"
This Flask app has three pages: admin, edit, and view. Each page is protected by a role check. The user role is checked from the URL query parameter ?user=alice. Only users with the matching role can access the page.
from flask import Flask, request from functools import wraps app = Flask(__name__) users = { 'alice': 'admin', 'bob': 'editor', 'carol': 'viewer' } def role_required(role): def decorator(f): @wraps(f) def decorated_function(*args, **kwargs): username = request.args.get('user') if not username: return "No user provided", 400 user_role = users.get(username) if user_role != role: return "Access denied", 403 return f(*args, **kwargs) return decorated_function return decorator @app.route('/admin') @role_required('admin') def admin_panel(): return "Welcome to the admin panel!" @app.route('/edit') @role_required('editor') def edit_panel(): return "Welcome to the editor panel!" @app.route('/view') @role_required('viewer') def view_panel(): return "Welcome to the viewer panel!" if __name__ == '__main__': app.run(debug=True)
In real apps, user roles come from logged-in user sessions, not URL parameters.
Use HTTPS to keep user info safe when sending requests.
Role checks can be combined to allow multiple roles if needed.
Role-based access control limits what users can do based on their role.
Use decorators in Flask to protect routes easily.
Always check user identity securely before allowing access.
Practice
Solution
Step 1: Understand RBAC concept
RBAC means users can only do things allowed by their roles, like admin or user.Step 2: Identify RBAC purpose in Flask
In Flask, RBAC limits access to routes or actions based on roles.Final Answer:
To restrict user actions based on their assigned roles -> Option CQuick Check:
RBAC = restrict actions by role [OK]
- Confusing RBAC with performance optimization
- Thinking RBAC manages UI generation
- Mixing RBAC with password security
Solution
Step 1: Identify decorators for access control
@login_required ensures user is logged in but does not check role.Step 2: Find decorator checking specific role
@role_required('admin') restricts access to users with 'admin' role.Final Answer:
@role_required('admin') -> Option DQuick Check:
Role check decorator = @role_required [OK]
- Using @login_required alone for role checks
- Confusing @app.route with access control
- Mistaking @csrf_protect for role restriction
@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'?
Solution
Step 1: Understand role_required decorator effect
@role_required('editor') allows only users with 'editor' role to access the route.Step 2: Check user role 'viewer' against required 'editor'
User role 'viewer' does not match 'editor', so access is denied or redirected.Final Answer:
They get redirected or denied access -> Option AQuick Check:
Role mismatch = access denied [OK]
- Assuming wrong role still shows content
- Expecting server error on role mismatch
- Thinking partial access is automatic
@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?
Solution
Step 1: Check decorator usage
role_required must be imported to work; missing import causes failure.Step 2: Verify other options
@app.route order is flexible; returning string is valid; function definition syntax is correct.Final Answer:
Not importing the role_required decorator -> Option AQuick Check:
Missing import breaks decorator [OK]
- Assuming decorator order always matters
- Confusing return type with access control
- Syntax errors in function definition
/settings that only users with roles 'admin' or 'manager' can access. Which code snippet correctly implements this role-based access control?Solution
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.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.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.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.Final Answer:
Option B code with manual role check -> Option BQuick Check:
Manual role list check allows multiple roles [OK]
- Stacking decorators for multiple roles (AND instead of OR)
- Passing list directly to decorator without support
- Using 'or' inside decorator argument incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
