function get_document_access
Retrieves document access information for a specific user, including permissions (write access, owner, reviewer, approver status) and share URL.
/tf/active/vicechatdev/CDocs/controllers/api_handler.py
30 - 88
moderate
Purpose
This function serves as an API endpoint to query and return comprehensive access control information for a controlled document. It validates the document and user existence, retrieves the current document version, and determines the user's permission levels and access URL. It's designed for document management systems that require role-based access control and sharing capabilities.
Source Code
def get_document_access(doc_id: str, user_id: str) -> Dict[str, Any]:
"""
API endpoint to get document access information.
Args:
doc_id: Document UID
user_id: User UID
Returns:
Dict: Access information
"""
try:
logger.debug(f"Getting access info for document {doc_id}, user {user_id}")
if not doc_id or not user_id:
return {
'success': False,
'message': 'Missing document ID or user ID'
}
# Get document
document = ControlledDocument(uid=doc_id)
if not document:
return {
'success': False,
'message': 'Document not found'
}
# Get current version
current_version = document.current_version
if not current_version:
return {
'success': False,
'message': 'Document has no current version'
}
# Get user's access information
result = get_user_access_url(current_version, user_id)
# Return simplified access info
return {
'success': True,
'document_id': doc_id,
'user_id': user_id,
'write_access': result.get('write_access', False),
'is_owner': result.get('is_owner', False),
'is_reviewer': result.get('is_reviewer', False),
'is_approver': result.get('is_approver', False),
'share_url': result.get('share_url', '')
}
except Exception as e:
logger.error(f"Error getting document access: {e}")
import traceback
logger.error(traceback.format_exc())
return {
'success': False,
'message': f'Error: {str(e)}'
}
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
doc_id |
str | - | positional_or_keyword |
user_id |
str | - | positional_or_keyword |
Parameter Details
doc_id: String containing the unique identifier (UID) of the document to check access for. Must be a non-empty string representing a valid document in the system.
user_id: String containing the unique identifier (UID) of the user whose access permissions are being queried. Must be a non-empty string representing a valid user in the system.
Return Value
Type: Dict[str, Any]
Returns a dictionary with access information. On success, contains: 'success' (bool: True), 'document_id' (str: the doc_id), 'user_id' (str: the user_id), 'write_access' (bool: whether user can edit), 'is_owner' (bool: whether user owns the document), 'is_reviewer' (bool: whether user is a reviewer), 'is_approver' (bool: whether user is an approver), 'share_url' (str: URL for sharing the document). On failure, contains: 'success' (bool: False) and 'message' (str: error description).
Dependencies
loggingtypingpaneltraceback
Required Imports
import logging
from typing import Dict, Any
from CDocs.models.document import ControlledDocument
from CDocs.controllers.share_controller import get_user_access_url
Conditional/Optional Imports
These imports are only needed under specific conditions:
import traceback
Condition: only when an exception occurs during execution for detailed error logging
OptionalUsage Example
# Assuming logger is configured and CDocs is set up
import logging
from typing import Dict, Any
from CDocs.models.document import ControlledDocument
from CDocs.controllers.share_controller import get_user_access_url
logger = logging.getLogger(__name__)
# Check access for a specific document and user
doc_id = "doc_12345"
user_id = "user_67890"
access_info = get_document_access(doc_id, user_id)
if access_info['success']:
print(f"User has write access: {access_info['write_access']}")
print(f"User is owner: {access_info['is_owner']}")
print(f"Share URL: {access_info['share_url']}")
else:
print(f"Error: {access_info['message']}")
Best Practices
- Always check the 'success' field in the returned dictionary before accessing other fields
- Ensure both doc_id and user_id are non-empty strings before calling this function
- Handle the case where the document or user might not exist in the system
- The function includes comprehensive error handling and logging, but callers should still implement appropriate error handling
- This function depends on external models (ControlledDocument) and controllers (get_user_access_url) being properly configured
- The function returns False for all permission flags if access cannot be determined, following a secure-by-default approach
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_document_permissions 85.1% similar
-
function get_user_access_url 75.8% similar
-
function manage_user_share_access_v2 70.4% similar
-
function manage_user_share_access 68.0% similar
-
function get_document_v1 67.5% similar