def before_edit_check_permissions(document, original): # Allow admin users to do whatever they want. # TODO: possibly move this into the check_permissions function. if user_has_role(u'admin'): return check_permissions('projects', original, request.method)
def before_delete_project(document): """Checks permissions before we allow deletion""" # Allow admin users to do whatever they want. # TODO: possibly move this into the check_permissions function. if user_has_role(u'admin'): return check_permissions('projects', document, request.method)
def check_put_access(request, lookup): """Only allow PUT to the current user, or all users if admin.""" if user_has_role(u'admin'): return current_user = g.get('current_user') if not current_user: raise Forbidden() if str(lookup['_id']) != str(current_user['user_id']): raise Forbidden()
def before_inserting_projects(items): """Strip unwanted properties, that will be assigned after creation. Also, verify permission to create a project (check quota, check role). :param items: List of project docs that have been inserted (normally one) """ # Allow admin users to do whatever they want. if user_has_role(u'admin'): return for item in items: item.pop('url', None)
def check_user_access(request, lookup): """Modifies the lookup dict to limit returned user info.""" # No access when not logged in. current_user = g.get('current_user') current_user_id = current_user['user_id'] if current_user else None # Admins can do anything and get everything, except the 'auth' block. if user_has_role(u'admin'): return if not lookup and not current_user: raise Forbidden() # Add a filter to only return the current user. if '_id' not in lookup: lookup['_id'] = current_user['user_id']
def protect_sensitive_fields(document, original): """When not logged in as admin, prevents update to certain fields.""" # Allow admin users to do whatever they want. if user_has_role(u'admin'): return def revert(name): if name not in original: try: del document[name] except KeyError: pass return document[name] = original[name] revert('url') revert('status') revert('category') revert('user')
def after_fetching_user(user): # Deny access to auth block; authentication stuff is managed by # custom end-points. user.pop('auth', None) current_user = g.get('current_user') current_user_id = current_user['user_id'] if current_user else None # Admins can do anything and get everything, except the 'auth' block. if user_has_role(u'admin'): return # Only allow full access to the current user. if str(user['_id']) == str(current_user_id): return # Remove all fields except public ones. public_fields = {'full_name', 'email'} for field in list(user.keys()): if field not in public_fields: del user[field]
def process_file(gcs, file_id, local_file): """Process the file by creating thumbnails, sending to Zencoder, etc. :param file_id: '_id' key of the file :type file_id: ObjectId or str :param local_file: locally stored file, or None if no local processing is needed. :type local_file: file """ file_id = ObjectId(file_id) # Fetch the src_file document from MongoDB. files = current_app.data.driver.db['files'] src_file = files.find_one(file_id) if not src_file: log.warning('process_file(%s): no such file document found, ignoring.') return src_file = utils.remove_private_keys(src_file) # Update the 'format' field from the content type. # TODO: overrule the content type based on file extention & magic numbers. mime_category, src_file['format'] = src_file['content_type'].split('/', 1) # Prevent video handling for non-admins. if not user_has_role(u'admin') and mime_category == 'video': if src_file['format'].startswith('x-'): xified = src_file['format'] else: xified = 'x-' + src_file['format'] src_file['content_type'] = 'application/%s' % xified mime_category = 'application' log.info('Not processing video file %s for non-admin user', file_id) # Run the required processor, based on the MIME category. processors = { 'image': _process_image, 'video': _process_video, } try: processor = processors[mime_category] except KeyError: log.info( "POSTed file %s was of type %r, which isn't thumbnailed/encoded.", file_id, mime_category) src_file['status'] = 'complete' else: log.debug('process_file(%s): marking file status as "processing"', file_id) src_file['status'] = 'processing' update_file_doc(file_id, status='processing') try: processor(gcs, file_id, local_file, src_file) except Exception: log.warning( 'process_file(%s): error when processing file, resetting status to ' '"queued_for_processing"', file_id, exc_info=True) update_file_doc(file_id, status='queued_for_processing') return # Update the original file with additional info, e.g. image resolution r, _, _, status = put_internal('files', src_file, _id=file_id) if status not in (200, 201): log.warning( 'process_file(%s): status %i when saving processed file info to MongoDB: %s', file_id, status, r)
def process_file(gcs, file_id, local_file): """Process the file by creating thumbnails, sending to Zencoder, etc. :param file_id: '_id' key of the file :type file_id: ObjectId or str :param local_file: locally stored file, or None if no local processing is needed. :type local_file: file """ file_id = ObjectId(file_id) # Fetch the src_file document from MongoDB. files = current_app.data.driver.db['files'] src_file = files.find_one(file_id) if not src_file: log.warning('process_file(%s): no such file document found, ignoring.') return src_file = utils.remove_private_keys(src_file) # Update the 'format' field from the content type. # TODO: overrule the content type based on file extention & magic numbers. mime_category, src_file['format'] = src_file['content_type'].split('/', 1) # Prevent video handling for non-admins. if not user_has_role(u'admin') and mime_category == 'video': if src_file['format'].startswith('x-'): xified = src_file['format'] else: xified = 'x-' + src_file['format'] src_file['content_type'] = 'application/%s' % xified mime_category = 'application' log.info('Not processing video file %s for non-admin user', file_id) # Run the required processor, based on the MIME category. processors = { 'image': _process_image, 'video': _process_video, } try: processor = processors[mime_category] except KeyError: log.info("POSTed file %s was of type %r, which isn't thumbnailed/encoded.", file_id, mime_category) src_file['status'] = 'complete' else: log.debug('process_file(%s): marking file status as "processing"', file_id) src_file['status'] = 'processing' update_file_doc(file_id, status='processing') try: processor(gcs, file_id, local_file, src_file) except Exception: log.warning('process_file(%s): error when processing file, resetting status to ' '"queued_for_processing"', file_id, exc_info=True) update_file_doc(file_id, status='queued_for_processing') return # Update the original file with additional info, e.g. image resolution r, _, _, status = put_internal('files', src_file, _id=file_id) if status not in (200, 201): log.warning('process_file(%s): status %i when saving processed file info to MongoDB: %s', file_id, status, r)