Consider this Flask route that checks user permissions before allowing access:
from flask import Flask, abort
app = Flask(__name__)
@app.route('/admin')
def admin_panel():
user = get_current_user()
if not user.has_permission('admin'):
abort(403)
return 'Welcome to admin panel'What will the user see if they do not have the 'admin' permission?
from flask import Flask, abort app = Flask(__name__) @app.route('/admin') def admin_panel(): user = get_current_user() if not user.has_permission('admin'): abort(403) return 'Welcome to admin panel'
Think about what abort(403) does in Flask.
The abort(403) function immediately stops the request and returns a 403 Forbidden HTTP error to the user. This means the user will see a 403 error page if they lack the required permission.
Look at this Flask route code snippet:
@app.route('/dashboard')
def dashboard():
user = get_current_user()
if user.has_permission('view_dashboard')
return 'Dashboard content'
else:
abort(403)What is the syntax error here?
@app.route('/dashboard') def dashboard(): user = get_current_user() if user.has_permission('view_dashboard') return 'Dashboard content' else: abort(403)
Check the syntax of the if statement line.
In Python, an if statement must end with a colon ':'. The line if user.has_permission('view_dashboard') is missing this colon, causing a syntax error.
Given this Flask route:
@app.route('/settings')
def settings():
user = get_current_user()
if user.has_permission('edit_settings'):
return 'Settings page', 200
else:
abort(403)What HTTP status code will the client receive if the user has the 'edit_settings' permission?
@app.route('/settings') def settings(): user = get_current_user() if user.has_permission('edit_settings'): return 'Settings page', 200 else: abort(403)
Look at the return statement when permission is granted.
The route returns the string 'Settings page' with status code 200 when the user has permission. So the client receives HTTP 200 OK.
Examine this Flask route:
@app.route('/profile')
def profile():
user = get_current_user()
if user.has_permission('view_profile') == False:
return 'Access denied', 403
return 'User profile'Users report they can access the profile page even without permission. Why?
@app.route('/profile') def profile(): user = get_current_user() if user.has_permission('view_profile') == False: return 'Access denied', 403 return 'User profile'
Consider how Python evaluates boolean expressions and the return value of has_permission.
If has_permission returns a falsy value other than exactly False (like None), the condition user.has_permission('view_profile') == False will be False, allowing access incorrectly. It's safer to use if not user.has_permission(...).
You want to protect several Flask routes so only users with 'editor' permission can access them. Which approach is best to avoid repeating permission checks in every route?
Think about code reuse and clear permission enforcement per route.
A custom decorator lets you write the permission check once and reuse it on any route needing that permission. This keeps code clean and consistent. Using before_request globally may block unrelated routes. Manual checks cause repetition. Assuming permissions is insecure.