function is_admin_user
Checks if the currently authenticated user has administrator privileges by comparing their email against a hardcoded list of admin emails.
/tf/active/vicechatdev/docchat/app.py
107 - 113
simple
Purpose
This function provides role-based access control (RBAC) for a Flask web application. It verifies whether the current session user is an administrator by checking if their email address exists in a predefined list of admin emails. The function is designed to work with Flask's session management and a custom authentication configuration system. It returns False if authentication is not required, no user is in session, or the user's email is not in the admin list.
Source Code
def is_admin_user():
"""Check if the current user is an admin"""
if not config.REQUIRE_AUTH or 'user' not in session:
return False
user_email = session['user'].get('email', '').lower()
admin_emails = ['wim@vicebio.com'] # Add more admin emails as needed
return user_email in admin_emails
Return Value
Returns a boolean value: True if the current user's email is in the admin_emails list and authentication is enabled with a valid user session; False if REQUIRE_AUTH is disabled, no user exists in the session, or the user's email is not in the admin list.
Dependencies
flaskconfig
Required Imports
from flask import session
import config
Usage Example
from flask import Flask, session
import config
app = Flask(__name__)
app.secret_key = 'your-secret-key'
# Configure authentication
config.REQUIRE_AUTH = True
# Simulate user login
with app.test_request_context():
session['user'] = {'email': 'wim@vicebio.com'}
if is_admin_user():
print('User has admin privileges')
# Allow access to admin features
else:
print('User does not have admin privileges')
# Restrict access
# Example as a route decorator check
@app.route('/admin/dashboard')
def admin_dashboard():
if not is_admin_user():
return 'Access denied', 403
return 'Welcome to admin dashboard'
Best Practices
- The admin emails list is hardcoded - consider moving this to a configuration file or database for better maintainability
- Email comparison is case-insensitive (uses .lower()) which is good practice
- This function should be used in conjunction with proper authentication middleware
- Consider creating a decorator function that wraps this check for easier route protection
- The function safely handles missing 'user' key in session and missing 'email' key in user dict
- For production use, consider implementing a more robust role management system with database-backed roles
- Always use this check on server-side routes; never rely solely on client-side checks for security
- Consider logging admin access attempts for security auditing purposes
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function is_authenticated 66.8% similar
-
function get_current_user 60.0% similar
-
function get_user_email 59.7% similar
-
function login_required 56.8% similar
-
function require_auth_v1 56.5% similar