📖 Scenario: You are building a simple web app where users have different roles like 'admin' and 'user'. You want to control which pages each role can see.
🎯 Goal: Create a Flask app that stores user roles, checks the role before showing a page, and only allows admins to see the admin page.
📋 What You'll Learn
Create a dictionary called users with usernames as keys and roles as values
Create a variable called current_user to simulate the logged-in user
Write a function check_access(role) that returns true if current_user has the given role
Use @app.route to create two pages: '/' for all users and '/admin' only for admins
💡 Why This Matters
🌍 Real World
Role-based access control is used in websites and apps to show or hide pages based on who is logged in, like admins managing content and users viewing it.
💼 Career
Understanding role-based access control is important for backend developers and web developers to secure parts of applications and manage user permissions.
Progress0 / 4 steps
1
Set up user roles dictionary
Create a dictionary called users with these exact entries: 'alice': 'admin', 'bob': 'user', 'carol': 'user'.
Flask
Hint
Think of users as a list of people with their roles.
2
Simulate logged-in user
Create a variable called current_user and set it to the string 'alice' to represent the logged-in user.
Flask
Hint
This variable tells the app who is currently using it.
3
Create access check function
Write a function called check_access(role) that returns True if the current_user has the given role, otherwise False. Use the users dictionary to find the role.
Flask
Hint
Use users.get(current_user) to get the role of the logged-in user.
4
Create Flask routes with role checks
Import Flask and abort from flask. Create a Flask app called app. Add two routes: @app.route('/') that returns 'Welcome, user!' for all users, and @app.route('/admin') that returns 'Welcome, admin!' only if check_access('admin') is true, otherwise abort with 403.
Flask
Hint
Use abort(403) to block access if the user is not an admin.
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
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 C
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
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 D
Quick Check:
Role check decorator = @role_required [OK]
Hint: Use @role_required to check user roles on routes [OK]
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
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 A
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?