function get_admin_user
Retrieves an admin user from a Neo4j database by querying for a specific email address and returns a DocUser object for document creation operations.
/tf/active/vicechatdev/CDocs/FC_sync.py
51 - 79
simple
Purpose
This function is designed to fetch a designated admin user (specifically 'wim@vicebio.com') from a Neo4j graph database for use in document creation workflows. It serves as a utility to obtain a privileged user account when system-level document operations need to be performed without a specific user context. The function handles database query errors gracefully and provides logging for debugging purposes.
Source Code
def get_admin_user() -> Optional[DocUser]:
"""
Get an admin user for document creation.
Returns:
DocUser: Admin user object or None if no admin found
"""
try:
# Query for admin user
result = db.run_query(
"""
MATCH (u:User {Mail : 'wim@vicebio.com'})
RETURN u.UID as uid, u.Name as name
LIMIT 1
"""
)
if result and len(result) > 0:
admin_uid = result[0]['uid']
admin_user = DocUser(uid=admin_uid)
logger.info(f"Using admin user: {result[0]['name']} (UID: {admin_uid})")
return admin_user
else:
logger.error("No admin user found in the database")
return None
except Exception as e:
logger.error(f"Error getting admin user: {e}")
return None
Return Value
Type: Optional[DocUser]
Returns an Optional[DocUser] object. If successful, returns a DocUser instance initialized with the admin user's UID from the database. Returns None if no admin user is found in the database or if an exception occurs during the query. The DocUser object represents a user in the document management system and can be used for document creation and version control operations.
Dependencies
loggingtypingCDocs.db.db_operationsCDocs.models.user_extensions
Required Imports
from typing import Optional
from CDocs.db import db_operations as db
from CDocs.models.user_extensions import DocUser
import logging
Usage Example
# Ensure logger is configured
import logging
logger = logging.getLogger(__name__)
# Ensure database connection is established
from CDocs.db import db_operations as db
from CDocs.models.user_extensions import DocUser
from typing import Optional
# Call the function to get admin user
admin_user = get_admin_user()
if admin_user:
print(f"Admin user retrieved successfully: {admin_user.uid}")
# Use admin_user for document operations
# e.g., create_document(admin_user, document_data)
else:
print("Failed to retrieve admin user")
# Handle the case where no admin user is available
Best Practices
- Always check if the returned value is None before using the admin user object
- Ensure the database connection is properly configured before calling this function
- The function is hardcoded to retrieve a specific admin email ('wim@vicebio.com') - consider parameterizing this for better reusability
- Monitor logs for 'No admin user found' or 'Error getting admin user' messages to detect configuration issues
- This function should only be used for system-level operations where a privileged user context is required
- Consider implementing caching if this function is called frequently to reduce database queries
- The function assumes the DocUser class can be instantiated with just a uid parameter
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_users 66.3% similar
-
function init_admin_role 62.7% similar
-
function get_user_by_id 58.1% similar
-
function get_all_documents 55.7% similar
-
function update_user_v1 51.7% similar