Skip to content
This repository has been archived by the owner on Jan 10, 2022. It is now read-only.

Commit

Permalink
Merge pull request #4 from m431m/master
Browse files Browse the repository at this point in the history
Fixed bugs + CKAN 2.7 compatibility. Thanks a lot for your contribution and for keeping it compatible with previous versions :)
  • Loading branch information
espona authored Sep 5, 2017
2 parents d65a272 + d55a1b7 commit bd5f652
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 33 deletions.
21 changes: 15 additions & 6 deletions ckanext/restricted/action.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
from ckan.lib.base import render_jinja2
from ckan.lib.mailer import mail_recipient
from ckan.lib.mailer import MailerException
import ckan.logic
from ckan.logic.action.create import user_create
from ckan.logic import side_effect_free, check_access
from ckan.logic.action.get import package_show, resource_show, resource_view_list, resource_search, package_search
import ckan.logic.auth as logic_auth
import ckan.authz as authz

from ckanext.restricted import helpers
from ckanext.restricted import logic
from ckanext.restricted import auth

from pylons import config
try:
# CKAN 2.7 and later
from ckan.common import config
except ImportError:
# CKAN 2.6 and earlier
from pylons import config

from logging import getLogger
log = getLogger(__name__)

NotFound = ckan.logic.NotFound

_get_or_bust = ckan.logic.get_or_bust

def restricted_user_create_and_notify(context, data_dict):
Expand All @@ -30,7 +40,7 @@ def body_from_user_dict(user_dict):
email = config.get('email_to')
if not email:
raise MailerException('Missing "email-to" in config')

subject = u'New Registration: ' + user_dict.get('name', 'new user') + ' (' + user_dict.get('email') + ')'

extra_vars = {
Expand Down Expand Up @@ -87,7 +97,7 @@ def restricted_resource_search(context, data_dict):
resource_search_result = resource_search(context, data_dict)

restricted_resource_search_result = {}

for key,value in resource_search_result.items():
if key == 'results':
restricted_resource_search_result[key] = _restricted_resource_list_url(context, value)
Expand All @@ -101,7 +111,7 @@ def restricted_package_search(context, data_dict):
package_search_result = package_search(context, data_dict)

restricted_package_search_result = {}

for key,value in package_search_result.items():
if key == 'results':
restricted_package_search_result_list = []
Expand All @@ -122,4 +132,3 @@ def _restricted_resource_list_url(context, resource_list):
restricted_resource['url'] = 'Not Authorized'
restricted_resources_list += [restricted_resource]
return restricted_resources_list

27 changes: 16 additions & 11 deletions ckanext/restricted/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@
from ckan.lib.base import render_jinja2

from logging import getLogger
from pylons import config

try:
# CKAN 2.7 and later
from ckan.common import config
except ImportError:
# CKAN 2.6 and earlier
from pylons import config

from email.header import Header

import simplejson as json
Expand Down Expand Up @@ -38,10 +45,10 @@ def _send_request_mail(self, data):
success = False
try:

resource_link = toolkit.url_for(controller='package', action='resource_read',
resource_link = toolkit.url_for(controller='package', action='resource_read',
id=data.get('package_name'), resource_id=data.get('resource_id'))
resource_edit_link = toolkit.url_for(controller='package', action='resource_edit',

resource_edit_link = toolkit.url_for(controller='package', action='resource_edit',
id=data.get('package_name') , resource_id=data.get('resource_id'))

extra_vars = {
Expand All @@ -68,15 +75,15 @@ def _send_request_mail(self, data):
## CC doesn't work and mailer cannot send to multiple addresses
for email, name in email_dict.iteritems():
mailer.mail_recipient(name, email, subject, body, headers)

## Special copy for the user (no links)
email = data.get('user_email')
name = data.get('user_name','User')

extra_vars['resource_link'] = '[...]'
extra_vars['resource_edit_link'] = '[...]'
body = render_jinja2('restricted/emails/restricted_access_request.txt', extra_vars)
body_user = "Please find below a copy of the access request mail sent. \n\n >> {0}".format( body.replace("\n", "\n >> "))
body_user = u"Please find below a copy of the access request mail sent. \n\n >> {0}".format( body.replace("\n", "\n >> "))
mailer.mail_recipient(name, email, 'Fwd: ' + subject, body_user, headers)
success=True

Expand Down Expand Up @@ -129,10 +136,10 @@ def restricted_request_access_form(self, package_id, resource_id, data=None, err
'''Redirects to form
'''
user_id = toolkit.c.user

if not user_id:
toolkit.abort(401, _('Access request form is available to logged in users only.'))

context = {
'model': model,
'session': model.Session,
Expand Down Expand Up @@ -217,5 +224,3 @@ def _get_contact_details(self, pkg_dict):
contact_email = config.get('email_to', 'email_to_undefined')
contact_name = "CKAN Admin"
return {'contact_email':contact_email, 'contact_name':contact_name}


30 changes: 15 additions & 15 deletions ckanext/restricted/logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@
from ckan.lib.base import render_jinja2
import ckan.plugins.toolkit as toolkit

from pylons import config
try:
# CKAN 2.7 and later
from ckan.common import config
except ImportError:
# CKAN 2.6 and earlier
from pylons import config

from logging import getLogger
log = getLogger(__name__)

def restricted_check_user_resource_access(user, resource_dict, package_dict):
restricted_level = 'public'
allowed_users = []

# check in resource_dict
if resource_dict:
extras = resource_dict.get('extras',{})
Expand All @@ -31,7 +36,7 @@ def restricted_check_user_resource_access(user, resource_dict, package_dict):
if restricted:
restricted_level = restricted.get('level', 'public')
allowed_users = restricted.get('allowed_users', '').split(',')

# Public resources (DEFAULT)
if not restricted_level or restricted_level == 'public':
return {'success': True }
Expand Down Expand Up @@ -89,22 +94,22 @@ def restricted_mail_allowed_user(user_id, resource):
resource_name = resource.get('name', resource['id'])

# maybe check user[activity_streams_email_notifications]==True

mail_body = restricted_allowed_user_mail_body(user, resource)
mail_subject = 'Access granted to resource {0}'.format(resource_name)

# Send mail to user
mailer.mail_recipient(user_name, user_email, mail_subject, mail_body)

# Sendo copy to admin
mailer.mail_recipient('CKAN Admin', config.get('email_to'), 'Fwd: ' + mail_subject, mail_body)

except:
log.warning('restricted_mail_allowed_user: Failed to send mail to "{0}"'.format(user_id))

def restricted_allowed_user_mail_body(user, resource):
resource_link = toolkit.url_for(controller='package', action='resource_read',

resource_link = toolkit.url_for(controller='package', action='resource_read',
id=resource.get('package_id'), resource_id=resource.get('id'))
extra_vars = {
'site_title': config.get('ckan.site_title'),
Expand All @@ -123,9 +128,9 @@ def _safe_json_loads(json_string, default={}):
return json.loads(json_string)
except:
return default

previous_restricted = _safe_json_loads(previous_value)
updated_restricted = _safe_json_loads(updated_resource.get('restricted', ''))
updated_restricted = _safe_json_loads(updated_resource.get('restricted', ''))

# compare restricted users_allowed values
updated_allowed_users = Set(updated_restricted.get('allowed_users','').split(','))
Expand All @@ -134,8 +139,3 @@ def _safe_json_loads(json_string, default={}):
for user_id in updated_allowed_users:
if user_id not in previous_allowed_users:
restricted_mail_allowed_user(user_id, updated_resource)





2 changes: 1 addition & 1 deletion ckanext/restricted/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class RestrictedPlugin(plugins.SingletonPlugin):
plugins.implements(plugins.IAuthFunctions)
plugins.implements(plugins.IRoutes, inherit=True)
plugins.implements(plugins.IResourceController, inherit=True)

# IConfigurer

def update_config(self, config_):
Expand Down

0 comments on commit bd5f652

Please sign in to comment.