def before_view(self, pkg_dict):
        # CITZEDC808
        if not record_is_viewable(pkg_dict, c.userobj):
            abort(401,
                  _('Unauthorized to read package %s') % pkg_dict.get("title"))

        return pkg_dict
Example #2
0
    def resource_read(self, id, resource_id):
        '''
        First calls ckan's default resource read to get the resource and package data.
        Then it checks if the resource can be viewed by the user
        '''

        # Need to setup for ofi here before heading to the super method
        context = {'model': model, 'session': model.Session,
                   'user': c.user or c.author,
                   'auth_user_obj': c.userobj,
                   'for_view': True}

        try:
            pkg_dict = get_action('package_show')(context, {'id': id})
        except NotFound:
            abort(404, _('Dataset not found'))
        except NotAuthorized:
            abort(401, _('Unauthorized to read dataset %s') % id)

        for resource in pkg_dict.get('resources', []):
            if resource['id'] == resource_id and u'ofi' in resource and resource[u'ofi']:
                c.ofi = _setup_ofi(pkg_dict['id'], context=context, pkg_dict=pkg_dict, open_modal=False)
                # only care about finding at least one ofi resource
                break

        # the ofi object is now in the global vars for this view, to use it in templates, call `c.ofi`
        result = super(EDCPackageController, self).resource_read(id, resource_id)

        # Check if user can view this record
        from ckanext.bcgov.util.helpers import record_is_viewable

        if not record_is_viewable(c.pkg_dict, c.userobj):
            abort(401, _('Unauthorized to read package %s') % id)

        return result
Example #3
0
    def resource_read(self, id, resource_id):
        '''
        First calls ckan's default resource read to get the resource and package data.
        Then it checks if the resource can be viewed by the user
        '''

        # Need to setup for ofi here before heading to the super method
        context = {
            'model': model,
            'session': model.Session,
            'user': c.user or c.author,
            'auth_user_obj': c.userobj,
            'for_view': True
        }

        try:
            pkg_dict = get_action('package_show')(context, {
                'id': id,
                'include_tracking': True
            })
        except NotFound:
            abort(404, _('Dataset not found'))
        except NotAuthorized:
            abort(401, _('Unauthorized to read dataset %s') % id)

        for resource in pkg_dict.get('resources', []):
            if resource[
                    'id'] == resource_id and u'ofi' in resource and resource[
                        u'ofi']:
                try:
                    c.ofi = _setup_ofi(pkg_dict['id'],
                                       context=context,
                                       pkg_dict=pkg_dict,
                                       open_modal=False)
                except OFIServiceError as e:
                    # log.error(e)
                    pass
                finally:
                    break  # only care about finding at least one ofi resource
        # end of ofi check

        # the ofi object is now in the global vars for this view, to use it in templates, call `c.ofi`
        result = super(EDCPackageController,
                       self).resource_read(id, resource_id)

        # Check if user can view this record
        from ckanext.bcgov.util.helpers import record_is_viewable

        if not record_is_viewable(c.pkg_dict, c.userobj):
            abort(401, _('Unauthorized to read package %s') % id)

        return result
Example #4
0
    def _package_show(self, context, pkg_id):
        '''
        Returns record's data with the given id only if the user is allowed to view the record.
        '''
        # FIXME: use IAuth plugin for authorization check and
        # use IPackageController to fill in extra values
        # then remove this method

        help_str = "Shows the package info with the given id. Param : id"

        return_dict = {"help": help_str}
        try:
            pkg = get_action('package_show')(context, {
                'id': pkg_id,
                'include_tracking': True
            })

            #add the top-level org to the organization
            #'organization, branch'
            org = model.Group.get(pkg['org'])
            branch = model.Group.get(pkg['sub_org'])
            org_title = ''
            branch_title = ''
            if org:
                org_title = org.title
            if branch:
                branch_title = branch.title

            if pkg['organization']:
                pkg['organization'][
                    'full_title'] = org_title + ', ' + branch_title

            from ckanext.bcgov.util.helpers import record_is_viewable

            if not record_is_viewable(pkg, c.userobj):
                return_dict['success'] = False
                return_dict['error'] = {
                    '__type': 'Authorization Error',
                    'message': _('Access denied')
                }
                return self._finish(403, return_dict, content_type='json')
            return_dict['success'] = True
            return_dict['result'] = pkg
        except NotFound, e:
            return_dict['error'] = {
                '__type': 'Not Found Error',
                'message': _('Not found')
            }
            if hasattr(e, 'extra_msg'):
                return_dict['error']['message'] += ': %s' % e.extra_msg
            return_dict['success'] = False
            return self._finish(404, return_dict, content_type='json')
Example #5
0
    def resource_read(self, id, resource_id):
        '''
        First calls ckan's default resource read to get the resource and package data.
        Then it checks if the resource can be viewed by the user
        '''
        result = super(EDCPackageController, self).resource_read(id, resource_id)

        #Check if user can view this record
        from ckanext.bcgov.util.helpers import record_is_viewable

        if not record_is_viewable(c.pkg_dict, c.userobj) :
            abort(401, _('Unauthorized to read package %s') % id)

        return result
Example #6
0
    def resource_read(self, id, resource_id):
        '''
        First calls ckan's default resource read to get the resource and package data.
        Then it checks if the resource can be viewed by the user
        '''
        result = super(EDCPackageController,
                       self).resource_read(id, resource_id)

        #Check if user can view this record
        from ckanext.bcgov.util.helpers import record_is_viewable

        if not record_is_viewable(c.pkg_dict, c.userobj):
            abort(401, _('Unauthorized to read package %s') % id)

        return result
Example #7
0
    def _package_show(self, context, pkg_id):
        '''
        Returns record's data with the given id only if the user is allowed to view the record.
        '''
        # FIXME: use IAuth plugin for authorization check and
        # use IPackageController to fill in extra values
        # then remove this method

        help_str = "Shows the package info with the given id. Param : id"

        return_dict = {"help": help_str}
        try :
            pkg = get_action('package_show')(context, {'id' : pkg_id})

            #add the top-level org to the organization
            #'organization, branch'
#            orgs = get_all_orgs()
            org = model.Group.get(pkg['org'])
#            org=orgs[pkg['org']]
            branch = model.Group.get(pkg['sub_org'])
#            branch=orgs[pkg['sub_org']]
#            org_title = org['title']
#            branch_title = branch['title']
            org_title = ''
            branch_title = ''
            if org :
                org_title = org.title
            if branch :
                branch_title = branch.title

            if pkg['organization']:
                pkg['organization']['full_title'] = org_title + ', ' + branch_title

            from ckanext.bcgov.util.helpers import record_is_viewable

            if not record_is_viewable(pkg, c.userobj) :
                return_dict['success'] = False
                return_dict['error'] = {'__type': 'Authorization Error', 'message': _('Access denied')}
                return self._finish(403, return_dict, content_type='json')
            return_dict['success'] = True
            return_dict['result'] = pkg
        except NotFound, e:
            return_dict['error'] = {'__type': 'Not Found Error',
                                    'message': _('Not found')}
            if hasattr(e, 'extra_msg'):
                return_dict['error']['message'] += ': %s' % e.extra_msg
            return_dict['success'] = False
            return self._finish(404, return_dict, content_type='json')
Example #8
0
    def read(self, id):
        '''
        First calls ckan's default read to get package data.
        Then it checks if the package can be viewed by the user
        '''
        result = super(EDCPackageController, self).read(id)

        #Check if user can view this record
        from ckanext.bcgov.util.helpers import record_is_viewable
        if not record_is_viewable(c.pkg_dict, c.userobj):
            abort(401, _('Unauthorized to read package %s') % id)

        #TODO: find out if/why comparing times is neccessary? - @deniszgonjanin
        metadata_modified_time = from_utc(c.pkg_dict['metadata_modified'])
        revision = get_action('revision_show')({}, {
            'id': c.pkg_dict['revision_id']
        })
        revision_timestamp_time = from_utc(revision['timestamp'])

        if (metadata_modified_time >= revision_timestamp_time):
            timestamp = metadata_modified_time.strftime(
                '%a, %d %b %Y %H:%M:%S GMT')
        else:
            timestamp = revision_timestamp_time.strftime(
                '%a, %d %b %Y %H:%M:%S GMT')

        #we only want Google Search Appliance to re-index records that have changed
        if ('gsa-crawler' in request.user_agent):
            response.headers['Last-Modified'] = timestamp
            response.headers['Cache-Control'] = 'public, max-age=31536000'
            response.headers['Pragma'] = 'cache'

            if ('If-Modified-Since' in request.headers
                    and request.headers['If-Modified-Since']):
                if_modified = request.headers.get('If-Modified-Since')
                if (timestamp > if_modified):
                    response.status = 200
                else:
                    response.status = 304
        else:
            response.headers['Pragma'] = 'no-cache'
            response.headers['Expires'] = '0'
            response.headers[
                'Cache-Control'] = 'no-cache, no-store, must-revalidate'
            response.status = 200

        return result
Example #9
0
    def read(self, id, format='html'):
        '''
        First calls ckan's default read to get package data.
        Then it checks if the package can be viewed by the user
        '''
        result = super(EDCPackageController, self).read(id, format)

        #Check if user can view this record
        from ckanext.bcgov.util.helpers import record_is_viewable
        if not record_is_viewable(c.pkg_dict, c.userobj) :
            abort(401, _('Unauthorized to read package %s') % id)

        #TODO: find out if/why comparing times is neccessary? - @deniszgonjanin
        metadata_modified_time = from_utc(c.pkg_dict['metadata_modified'])
        revision = get_action('revision_show')(
            {}, {'id': c.pkg_dict['revision_id']}
        )
        revision_timestamp_time = from_utc(revision['timestamp'])

        if (metadata_modified_time >= revision_timestamp_time):
            timestamp = metadata_modified_time.strftime('%a, %d %b %Y %H:%M:%S GMT')
        else:
            timestamp = revision_timestamp_time.strftime('%a, %d %b %Y %H:%M:%S GMT')

        #we only want Google Search Appliance to re-index records that have changed
        if ('gsa-crawler' in request.user_agent):
            response.headers['Last-Modified']  = timestamp
            response.headers['Cache-Control'] = 'public, max-age=31536000'
            response.headers['Pragma'] = 'cache'

            if ('If-Modified-Since' in request.headers and request.headers['If-Modified-Since']):
                if_modified = request.headers.get('If-Modified-Since')
                if (timestamp > if_modified):
                    response.status = 200
                else:
                    response.status = 304
        else:
            response.headers['Pragma'] = 'no-cache'
            response.headers['Expires'] = '0'
            response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
            response.status = 200

        return result
Example #10
0
    def read(self, id):
        '''
        First calls ckan's default read to get package data.
        Then it checks if the package can be viewed by the user
        '''
        # Need to setup for ofi here before heading to the super method
        context = {
            'model': model,
            'session': model.Session,
            'user': c.user or c.author,
            'auth_user_obj': c.userobj,
            'for_view': True
        }

        try:
            pkg_dict = get_action('package_show')(context, {
                'id': id,
                'include_tracking': True
            })
        except NotFound:
            abort(404, _('Dataset not found'))
        except NotAuthorized:
            abort(401, _('Unauthorized to read dataset %s') % id)

        for resource in pkg_dict.get('resources', []):
            if u'ofi' in resource and resource[u'ofi']:
                # only care if there's at least one ofi resource
                try:
                    c.ofi = _setup_ofi(pkg_dict['id'],
                                       context=context,
                                       pkg_dict=pkg_dict,
                                       open_modal=False)
                except OFIServiceError as e:
                    # log.error(e)
                    pass
                finally:
                    break
        # end ofi check

        # the ofi object is now in the global vars for this view, to use it in templates, call `c.ofi`
        result = super(EDCPackageController, self).read(id)

        # Check if user can view this record
        from ckanext.bcgov.util.helpers import record_is_viewable
        if not record_is_viewable(c.pkg_dict, c.userobj):
            abort(401, _('Unauthorized to read package %s') % id)

        # TODO: find out if/why comparing times is neccessary? - @deniszgonjanin
        metadata_modified_time = from_utc(c.pkg_dict['metadata_modified'])
        revision = get_action('revision_show')({}, {
            'id': c.pkg_dict['revision_id']
        })
        revision_timestamp_time = from_utc(revision['timestamp'])

        if (metadata_modified_time >= revision_timestamp_time):
            timestamp = metadata_modified_time.strftime(
                '%a, %d %b %Y %H:%M:%S GMT')
        else:
            timestamp = revision_timestamp_time.strftime(
                '%a, %d %b %Y %H:%M:%S GMT')

        # we only want Google Search Appliance to re-index records that have changed
        if ('gsa-crawler' in request.user_agent):
            response.headers['Last-Modified'] = timestamp
            response.headers['Cache-Control'] = 'public, max-age=31536000'
            response.headers['Pragma'] = 'cache'

            if ('If-Modified-Since' in request.headers
                    and request.headers['If-Modified-Since']):
                if_modified = request.headers.get('If-Modified-Since')
                if (timestamp > if_modified):
                    response.status = 200
                else:
                    response.status = 304
        else:
            response.headers['Pragma'] = 'no-cache'
            response.headers['Expires'] = '0'
            response.headers[
                'Cache-Control'] = 'no-cache, no-store, must-revalidate'
            response.status = 200

        return result
Example #11
0
    def before_view(self, pkg_dict):
        # CITZEDC808
        if not record_is_viewable(pkg_dict, c.userobj):
            abort(401, _('Unauthorized to read package %s') % pkg_dict.get("title"))

        return pkg_dict
Example #12
0
    def read(self, id):
        '''
        First calls ckan's default read to get package data.
        Then it checks if the package can be viewed by the user
        '''
        # Need to setup for ofi here before heading to the super method
        context = {'model': model, 'session': model.Session,
                   'user': c.user or c.author,
                   'auth_user_obj': c.userobj,
                   'for_view': True}

        try:
            pkg_dict = get_action('package_show')(context, {'id': id, 'include_tracking':True})
        except NotFound:
            abort(404, _('Dataset not found'))
        except NotAuthorized:
            abort(401, _('Unauthorized to read dataset %s') % id)

        for resource in pkg_dict.get('resources', []):
            if u'ofi' in resource and resource[u'ofi']:
                # only care if there's at least one ofi resource
                try:
                    c.ofi = _setup_ofi(pkg_dict['id'], context=context, pkg_dict=pkg_dict, open_modal=False)
                except OFIServiceError as e:
                    # log.error(e)
                    pass
                finally:
                    break
        # end ofi check

        # the ofi object is now in the global vars for this view, to use it in templates, call `c.ofi`
        result = super(EDCPackageController, self).read(id)

        # Check if user can view this record
        from ckanext.bcgov.util.helpers import record_is_viewable
        if not record_is_viewable(c.pkg_dict, c.userobj):
            abort(401, _('Unauthorized to read package %s') % id)

        # TODO: find out if/why comparing times is neccessary? - @deniszgonjanin
        metadata_modified_time = from_utc(c.pkg_dict['metadata_modified'])
        revision = get_action('revision_show')(
            {}, {'id': c.pkg_dict['revision_id']}
        )
        revision_timestamp_time = from_utc(revision['timestamp'])

        if (metadata_modified_time >= revision_timestamp_time):
            timestamp = metadata_modified_time.strftime('%a, %d %b %Y %H:%M:%S GMT')
        else:
            timestamp = revision_timestamp_time.strftime('%a, %d %b %Y %H:%M:%S GMT')

        # we only want Google Search Appliance to re-index records that have changed
        if ('gsa-crawler' in request.user_agent):
            response.headers['Last-Modified'] = timestamp
            response.headers['Cache-Control'] = 'public, max-age=31536000'
            response.headers['Pragma'] = 'cache'

            if ('If-Modified-Since' in request.headers and request.headers['If-Modified-Since']):
                if_modified = request.headers.get('If-Modified-Since')
                if (timestamp > if_modified):
                    response.status = 200
                else:
                    response.status = 304
        else:
            response.headers['Pragma'] = 'no-cache'
            response.headers['Expires'] = '0'
            response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
            response.status = 200

        return result