Create package_adquire auth function for the future. Fix some bugs

This commit is contained in:
Aitor Magán 2014-07-14 11:50:59 +02:00
parent 18c434b722
commit 1dcb8098b8
6 changed files with 29 additions and 4 deletions

View File

@ -22,7 +22,7 @@ Since each service can send notifications in a different way, the extension allo
If you want to create your own parser, you have to:
1. Create a class with a method called `parse_notification`. This method will recieve one argument that will include the notification body.
2. Parse the notification as you like. You can raise a CKAN's default exception (`DataError`, `ValidationError`, `ObjectNotFound`, `NotAuthorized`, `ValidationError`, `SearchError`, `SearchQueryError` or `SearchIndexError`) if you find an error parsing the notification.
2. Parse the notification as you like. You can raise a CKAN's default exception (`ValidationError`, `ObjectNotFound`, `NotAuthorized`, `ValidationError`, `SearchError`, `SearchQueryError` or `SearchIndexError`) if you find an error parsing the notification.
3. Return a dictionary with the structure attached below. The `users_datasets` is the lists of datasets available for each user (each element of this list is a dictionary with two fields: `user` and `datasets`).
```

View File

@ -13,6 +13,9 @@ def package_adquired(context, request_data):
log.info('Notification Request received')
# Check access
plugins.toolkit.check_access(constants.PACKAGE_ADQUIRED, context, request_data)
# Get the parser from the configuration
class_path = config.get(PARSER_CONFIG_PROP, '')

View File

@ -107,3 +107,9 @@ def resource_show(context, data_dict):
return {'success': False, 'msg': _('User %s not authorized to read resource %s') % (user, resource.id)}
else:
return {'success': True}
@tk.auth_allow_anonymous_access
def package_adquired(context, data_dict):
# TODO: Improve security
return {'success': True}

View File

@ -3,3 +3,4 @@ ALLOWED_USERS_STR = 'allowed_users_str'
SEARCHABLE = 'searchable'
ADQUIRE_URL = 'adquire_url'
CONTEXT_CALLBACK = 'updating_via_cb'
PACKAGE_ADQUIRED = 'package_adquired'

View File

@ -11,13 +11,25 @@ class FiWareNotificationParser(object):
my_host = request.host
fields = ['resources', 'customer_name']
for field in fields:
if not field in request_data:
raise tk.ValidationError({'message': '%s not found in the request' % field})
# Parse the body
resources = request_data['resources']
user_name = request_data['customer_name']
datasets = []
if not isinstance(resources, list):
raise tk.ValidationError({'message': 'Invalid resources format'})
if not isinstance(user_name, basestring):
raise tk.ValidationError({'message': 'Invalid customer_name format'})
for resource in resources:
if 'url' in resource:
if isinstance(resource, dict) and 'url' in resource:
parsed_url = urlparse(resource['url'])
dataset_name = re.findall('^/dataset/([^/]+).*$', parsed_url.path)
@ -27,5 +39,7 @@ class FiWareNotificationParser(object):
else:
raise tk.ValidationError({'message': 'Dataset %s is associated with the CKAN instance located at %s'
% (dataset_name[0], parsed_url.netloc)})
else:
raise tk.ValidationError({'message': 'Invalid resource format'})
return {'users_datasets': [{'user': user_name, 'datasets': datasets}]}

View File

@ -82,7 +82,8 @@ class PrivateDatasets(p.SingletonPlugin, tk.DefaultDatasetForm):
def get_auth_functions(self):
return {'package_show': auth.package_show,
'package_update': auth.package_update,
'resource_show': auth.resource_show}
'resource_show': auth.resource_show,
constants.PACKAGE_ADQUIRED: auth.package_adquired}
######################################################################
############################ ICONFIGURER #############################
@ -113,7 +114,7 @@ class PrivateDatasets(p.SingletonPlugin, tk.DefaultDatasetForm):
######################################################################
def get_actions(self):
return {'package_adquired': actions.package_adquired}
return {constants.PACKAGE_ADQUIRED: actions.package_adquired}
######################################################################
######################### IPACKAGECONTROLLER #########################