def gdpr_user_create(context, data_dict): user_dict = user_create(context, data_dict) for key, value in data_dict.items(): if key.startswith('policy-'): policy_id = int(key.replace('policy-', '')) GDPRAccept.create(user_id=user_dict['id'], policy_id=policy_id, datetime=datetime.datetime.now()) model.repo.commit() return user_dict
def mailchimp_user_create(context, data_dict): user = user_create(context, data_dict) if user is not None and data_dict is not None and data_dict.get( 'newsletter', None) == 'subscribed': split_names = name_splitter( data_dict.get('fullname', data_dict.get('name', None))) mailchimp_add_subscriber(split_names[0], split_names[1], data_dict.get('email', None), tags=["NAP-user"]) return user
def user_create(context, data_dict): """ Avoid already existing user to create a new user. Check if the email id already exists if so raise errors """ _email = data_dict.get('email', '') # User form accepts errors in terms of dict with error list errors = dict() errors['email'] = [] if hlp.email_validator(_email): _users = hlp.get_user_list_by_email(_email) if _users: errors['email'].append("Email already exists.") raise ValidationError(errors) else: errors['email'].append("Not a valid email.") raise ValidationError(errors) return create_core.user_create(context, data_dict)
def restricted_user_create_and_notify(context, data_dict): def body_from_user_dict(user_dict): body = '' for key, value in user_dict.items(): body += '* {0}: {1}\n'.format( key.upper(), value if isinstance(value, str) else str(value)) return body user_dict = user_create(context, data_dict) # Send your email, check ckan.lib.mailer for params try: name = _('CKAN System Administrator') email = config.get('email_to') if not email: raise MailerException('Missing "email-to" in config') subject = _('New Registration: {0} ({1})').format( user_dict.get('name', _(u'new user')), user_dict.get('email')) extra_vars = { 'site_title': config.get('ckan.site_title'), 'site_url': config.get('ckan.site_url'), 'user_info': body_from_user_dict(user_dict)} body = render_jinja2( 'restricted/emails/restricted_user_registered.txt', extra_vars) mail_recipient(name, email, subject, body) except MailerException as mailer_exception: log.error('Cannot send mail after registration') log.error(mailer_exception) return (user_dict)
def restricted_user_create_and_notify(context, data_dict): def body_from_user_dict(user_dict): body = u'\n' for key, value in user_dict.items(): body += ' \t - ' + key.upper() + ': ' + ( value if type(value) == str else unicode(value)) + '\n' return body user_dict = user_create(context, data_dict) # Send your email, check ckan.lib.mailer for params try: name = 'CKAN System Administrator' 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 = { 'site_title': config.get('ckan.site_title'), 'site_url': config.get('ckan.site_url'), 'user_info': body_from_user_dict(user_dict) } body = render_jinja2( 'restricted/emails/restricted_user_registered.txt', extra_vars) mail_recipient(name, email, subject, body) except MailerException as mailer_exception: log.error("Cannot send mail after registration ") log.error(mailer_exception) pass return (user_dict)
def test_user_activity(self): """Test user activity streams HTML rendering.""" # Register a new user. user_dict = {'name': 'billybeane', 'fullname': 'Billy Beane', 'about': 'General Manager, Oakland Athletics', 'email': '*****@*****.**', 'password': '******'} context = { 'model': ckan.model, 'session': ckan.model.Session, 'user': self.sysadmin_user.name, 'allow_partial_update': True, } user = user_create(context, user_dict) offset = url_for('user.activity', id=user['id']) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s signed up' % user['fullname'] in stripped, stripped # Create a new package. package = { 'name' : 'baseball_stats', 'title' : "Billy's Stats about Baseball Players", } context['user'] = user['name'] # FIXME This test use an old way to get at the schema to # recreate this we need to pretend to be using the api. We # should not be calling package_create like this we should be # going via the api or package controllers context['api_version'] = 3 context['ignore_auth'] = True package = package_create(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s created the dataset %s ' % ( user['fullname'], package['title']) in stripped, stripped # Add a resource to the package. resource = { 'url': 'http://www.example.com', 'description': "Chad Bradford's OBP Stats`", 'format': 'cvs', 'name': 'Chad Bradford Stats', } package['resources'].append(resource) request_data = { 'id': package['id'], 'resources': package['resources'], } package = package_update(context, request_data) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s added the resource %s to the dataset %s' % \ (user['fullname'], resource['name'], package['title']) \ in stripped, stripped # Update the package. package['title'] = "Billy's Updated Stats about Baseball Players" package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s updated the dataset %s' \ % (user['fullname'], package['title']) \ in stripped, stripped # Update the resource. resource = package['resources'][0] resource['name'] = 'Chad Bradford Updated Stats' resource = resource_update(context, resource) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s updated the resource %s in the dataset %s' \ % (user['fullname'], resource['name'], package['title']) \ in stripped, stripped # Delete the resource. context['allow_partial_update'] = False package['resources'] = [] package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s deleted the resource %s from the dataset %s' % \ (user['fullname'], resource['name'], package['title']) \ in stripped, stripped # Follow the package. follow_dataset(context, {'id': package['id']}) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s started following %s' % (user['fullname'], package['title']) not in stripped, stripped # Follow another user. follow_user(context, {'id': 'joeadmin'}) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s started following %s' % (user['fullname'], 'joeadmin') not in stripped, stripped # Create a new group. group = { 'name': 'baseball-stats-group', 'title': 'A Group for Datasets about Baseball' } context['allow_partial_update'] = True group = group_create(context, group) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s created the group %s' % (user['fullname'], group['title']) \ in stripped, stripped # Update the group. group['title'] = 'updated' group = group_update(context, group) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s updated the group %s' % (user['fullname'], group['title']) \ in stripped, stripped # Delete the group. group['state'] = 'deleted' group_update(context, group) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s deleted the group %s' % (user['fullname'], group['title']) \ in stripped, stripped # Add a new tag to the package. tag = {'name': 'baseball'} package['tags'].append(tag) package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s added the tag %s to the dataset %s' % \ (user['fullname'], tag['name'], package['title']) \ in stripped, stripped # Remove the tag from the package. package['tags'] = [] context['allow_partial_update'] = False package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s removed the tag %s from the dataset %s' % \ (user['fullname'], tag['name'], package['title']) \ in stripped, stripped # Add an extra to the package. package['extras'].append({'key': 'quality', 'value': '10000'}) package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s added the extra "%s" to the dataset %s' % \ (user['fullname'], 'quality', package['title']) \ in stripped, stripped # Update the extra. package['extras'][0]['value'] = 'updated' package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s changed the extra "%s" of the dataset %s' % \ (user['fullname'], 'quality', package['title']) \ in stripped, stripped # Delete the extra. del package['extras'][0] package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s deleted the extra "%s" from the dataset %s' % \ (user['fullname'], 'quality', package['title']) \ in stripped, stripped # Delete the package. # we need to get round the delete permission context['ignore_auth'] = True package_delete(context, package) del context['ignore_auth'] result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s deleted the dataset %s' % \ (user['fullname'], package['title']) \ in stripped, stripped # Update the user's profile. user['about'] = '' user_update(context, user) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s updated their profile' % user['fullname'] \ in stripped, stripped # By now we've created >15 activities, but only the latest 15 should # appear on the page. result = self.app.get(offset, status=200) assert result.body.count('<span class="actor">') \ == 15, result.body.count('<span class="actor">') # The user's dashboard page should load successfully and have the # latest 15 activities on it. offset = url_for('dashboard.index') extra_environ = {'Authorization': str(ckan.model.User.get('billybeane').apikey)} result = self.app.get(offset, extra_environ=extra_environ, status=200) assert result.body.count('<span class="actor">') == 15, \ result.body.count('<span class="actor">')
def create_organization(context, data_dict): context['ignore_auth'] = True model = context['model'] session = context['session'] destruction_secret = config.get(plugin_config_prefix + 'destruction_secret', 'changeme') client_id = data_dict.pop('client_id') client_secret = data_dict.pop('client_secret') instance_id = data_dict.pop('instance_id') # re-mapping received dict registration_uri = data_dict.pop('instance_registration_uri') organization = data_dict['organization'] user = data_dict['user'] user_dict = { 'id': user['id'], 'name': user['id'].replace('-', ''), 'email': user['email_address'], 'password': user['id'] } user_obj = model.User.get(user_dict['name']) org_dict = { 'type': 'organization', 'name': slugify(organization['name']), 'id': instance_id, 'title': organization['name'], 'user': user_dict['name'] } if not user_obj: user_create(context, user_dict) context['user'] = user_dict['name'] try: delete_uri = toolkit.url_for(host=request.host, controller='api', action='action', logic_function="delete-ozwillo-organization", ver=context['api_version'], qualified=True) organization_uri = toolkit.url_for(host=request.host, controller='organization', action='read', id=org_dict['name'], qualified=True) default_icon_url = toolkit.url_for(host=request.host, qualified=True, controller='home', action='index') + 'opendata.png' group_or_org_create(context, org_dict, is_org=True) # setting organization as active explicitely group = model.Group.get(org_dict['name']) group.state = 'active' group.image_url = default_icon_url group.save() model.repo.new_revision() model.GroupExtra(group_id=group.id, key='client_id', value=client_id).save() model.GroupExtra(group_id=group.id, key='client_secret', value=client_secret).save() # Automatically add data from data gouv dc_id = data_dict['organization']['dc_id'] siret_re = re.compile(r'\d{14}') try: organization_insee = siret_re.search(dc_id).group() after_create(group, organization_insee, user_dict['name']) except AttributeError: log.error('SIRET did not match pattern, no data will be added') session.flush() # notify about organization creation services = {'services': [{ 'local_id': 'organization', 'name': 'Open Data', 'service_uri': organization_uri + '/sso', 'description': 'Organization ' + org_dict['name'] + ' on CKAN', 'tos_uri': organization_uri, 'policy_uri': organization_uri, 'icon': group.image_url, 'payment_option': 'FREE', 'target_audience': ['PUBLIC_BODIES'], 'contacts': [organization_uri], 'redirect_uris': [organization_uri + '/callback'], 'post_logout_redirect_uris': [organization_uri + '/logout'], 'visible': False}], 'instance_id': instance_id, 'destruction_uri': delete_uri, 'destruction_secret': destruction_secret, 'needed_scopes': [{ 'scope_id': 'profile', 'motivation': 'Used to link user to the organization' }] } headers = {'Content-type': 'application/json', 'Accept': 'application/json'} requests.post(registration_uri, data=json.dumps(services), auth=(client_id, client_secret), headers=headers) except logic.ValidationError, e: log.debug('Validation error "%s" occured while creating organization' % e) raise
def test_activity(self): """Test activity streams HTML rendering.""" # Register a new user. user_dict = {'name': 'billybeane', 'fullname': 'Billy Beane', 'about': 'General Manager, Oakland Athletics', 'email': '*****@*****.**', 'password': '******'} context = { 'model': ckan.model, 'session': ckan.model.Session, 'user': self.sysadmin_user.name, 'allow_partial_update': True, 'extras_as_string': True, } user = user_create(context, user_dict) offset = url_for(controller='user', action='read', id=user['id']) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s signed up.' % user['fullname'] in stripped, stripped # Create a new package. package = { 'name' : 'baseball_stats', 'title' : "Billy's Stats about Baseball Players", } context['user'] = user['name'] package = package_create(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s created the dataset %s ' % ( user['fullname'], package['title']) in stripped, stripped # Add a resource to the package. resource = { 'url': 'http://www.example.com', 'description': "Chad Bradford's OBP Stats`", 'format': 'cvs', 'name': 'Chad Bradford Stats', } package['resources'].append(resource) request_data = { 'id': package['id'], 'resources': package['resources'], } package = package_update(context, request_data) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s added the resource %s to the dataset %s' % \ (user['fullname'], resource['name'], package['title']) \ in stripped, stripped # Update the package. package['title'] = "Billy's Updated Stats about Baseball Players" package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s updated the dataset %s' \ % (user['fullname'], package['title']) \ in stripped, stripped # Update the resource. resource = package['resources'][0] resource['name'] = 'Chad Bradford Updated Stats' resource = resource_update(context, resource) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s updated the resource %s in the dataset %s' \ % (user['fullname'], resource['name'], package['title']) \ in stripped, stripped # Delete the resource. context['allow_partial_update'] = False package['resources'] = [] package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s deleted the resource %s from the dataset %s' % \ (user['fullname'], resource['name'], package['title']) \ in stripped, stripped # Create a new group. group = { 'name': 'baseball-stats-group', 'title': 'A Group for Datasets about Baseball' } context['allow_partial_update'] = True group = group_create(context, group) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s created the group %s' % (user['fullname'], group['name']) \ in stripped, stripped # Update the group. group['title'] = 'updated' group = group_update(context, group) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s updated the group %s' % (user['fullname'], group['name']) \ in stripped, stripped # Delete the group. group['state'] = 'deleted' group_update(context, group) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s deleted the group %s' % (user['fullname'], group['name']) \ in stripped, stripped # Add a new tag to the package. tag = {'name': 'baseball'} package['tags'].append(tag) package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s added the tag %s to the dataset %s' % \ (user['fullname'], tag['name'], package['title']) \ in stripped, stripped # Remove the tag from the package. package['tags'] = [] context['allow_partial_update'] = False package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s removed the tag %s from the dataset %s' % \ (user['fullname'], tag['name'], package['title']) \ in stripped, stripped # Add an extra to the package. package['extras'].append({'key': 'quality', 'value': '10000'}) package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s added the extra "%s" to the dataset %s' % \ (user['fullname'], 'quality', package['title']) \ in stripped, stripped # Update the extra. package['extras'][0]['value'] = 'updated' package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s changed the extra "%s" of the dataset %s' % \ (user['fullname'], 'quality', package['title']) \ in stripped, stripped # Delete the extra. del package['extras'][0] package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s deleted the extra "%s" from the dataset %s' % \ (user['fullname'], 'quality', package['title']) \ in stripped, stripped # Delete the package. package_delete(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s deleted the dataset %s' % \ (user['fullname'], package['title']) \ in stripped, stripped # Update the user's profile. user['about'] = '' user_update(context, user) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s updated their profile.' % user['fullname'] \ in stripped, stripped # By now we've created >15 activities, but only the latest 15 should # appear on the page. result = self.app.get(offset, status=200) assert result.body.count('<div class="activity">') \ == 15
def create_organization(context, data_dict): context['ignore_auth'] = True model = context['model'] session = context['session'] destruction_secret = config.get( plugin_config_prefix + 'destruction_secret', 'changeme') client_id = data_dict.pop('client_id') client_secret = data_dict.pop('client_secret') instance_id = data_dict.pop('instance_id') # re-mapping received dict registration_uri = data_dict.pop('instance_registration_uri') organization = data_dict['organization'] log.info('Creating organization {} (instance id : {})'.format( organization, instance_id)) user = data_dict['user'] user_dict = { 'id': user['id'], 'name': user['id'].replace('-', ''), 'email': user['email_address'], 'password': user['id'] } user_obj = model.User.get(user_dict['name']) org_dict = { 'type': 'organization', 'name': slugify(organization['name']), 'id': instance_id, 'title': organization['name'], 'user': user_dict['name'] } if not user_obj: user_create(context, user_dict) context['user'] = user_dict['name'] try: delete_uri = url_for(controller='api', action='action', logic_function='delete-ozwillo-organization', ver=context['api_version'], qualified=True) organization_uri = url_for(controller='organization', action='read', id=org_dict['name'], qualified=True) default_icon_url = url_for( controller='home', action='index', qualified=True) + 'opendata.png' group_or_org_create(context, org_dict, is_org=True) # setting organization as active explicitly group = model.Group.get(org_dict['name']) group.state = 'active' group.image_url = default_icon_url group.save() # no model.repo.new_revision() in 2.9 like in 2.8.2, see ckan/action/create.py diff & https://pythonrepo.com/repo/ckan-ckan-python-science model.GroupExtra(group_id=group.id, key='client_id', value=client_id).save() model.GroupExtra(group_id=group.id, key='client_secret', value=client_secret).save() # Get & save SIREN/SIRET and automatically add data from data gouv dc_id = data_dict['organization']['dc_id'] siret_re = re.compile(r'\d{14}') try: organization_insee = siret_re.search(dc_id).group() model.GroupExtra(group_id=group.id, key='siret', value=organization_insee).save() model.GroupExtra(group_id=group.id, key='siren', value=organization_insee[:9]).save() model.GroupExtra(group_id=group.id, key='FDR_SIREN', value=organization_insee[:9]).save() if asbool( config.get( 'ckanext.ozwillo_organization_api.add_data_on_create', True)): after_create(group, organization_insee, user_dict['name']) except AttributeError: log.info('SIRET did not match pattern, no data will be added') session.flush() # notify about organization creation services = { 'services': [{ 'local_id': 'organization', 'name': 'Open Data - ' + org_dict['title'], 'service_uri': organization_uri + '/sso', 'description': 'Organization ' + org_dict['name'] + ' on CKAN', 'tos_uri': organization_uri, 'policy_uri': organization_uri, 'icon': group.image_url, 'payment_option': 'FREE', 'target_audience': ['PUBLIC_BODIES', 'CITIZENS', 'COMPANIES'], 'contacts': [organization_uri], 'redirect_uris': [organization_uri + '/callback'], 'post_logout_redirect_uris': [organization_uri + '/logout'], 'visible': False }], 'instance_id': instance_id, 'destruction_uri': delete_uri, 'destruction_secret': destruction_secret, 'needed_scopes': [{ 'scope_id': 'profile', 'motivation': 'Used to link user to the organization' }] } headers = { 'Content-type': 'application/json', 'Accept': 'application/json' } log.info('Confirming registration on {}'.format(registration_uri)) services_copy = services.copy() del services_copy['destruction_secret'] log.info('Registration info is {}'.format(json.dumps(services_copy))) registration_response = requests.post(registration_uri, data=json.dumps(services), auth=(client_id, client_secret), headers=headers) log.debug('Received response from kernel : {} ({})'.format( registration_response.text, registration_response.status_code)) except toolkit.ValidationError as e: log.debug( 'Validation error "%s" occurred while creating organization' % e) raise
def test_user_activity(self): """Test user activity streams HTML rendering.""" # Register a new user. user_dict = { "name": "billybeane", "fullname": "Billy Beane", "about": "General Manager, Oakland Athletics", "email": "*****@*****.**", "password": "******", } context = { "model": ckan.model, "session": ckan.model.Session, "user": self.sysadmin_user.name, "allow_partial_update": True, "extras_as_string": True, } user = user_create(context, user_dict) offset = url_for(controller="user", action="read", id=user["id"]) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert "%s signed up" % user["fullname"] in stripped, stripped # Create a new package. package = {"name": "baseball_stats", "title": "Billy's Stats about Baseball Players"} context["user"] = user["name"] # FIXME This test use an old way to get at the schema to # recreate this we need to pretend to be using the api. We # should not be calling package_create like this we should be # going via the api or package controllers context["api_version"] = 3 context["ignore_auth"] = True package = package_create(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert "%s created the dataset %s " % (user["fullname"], package["title"]) in stripped, stripped # Add a resource to the package. resource = { "url": "http://www.example.com", "description": "Chad Bradford's OBP Stats`", "format": "cvs", "name": "Chad Bradford Stats", } package["resources"].append(resource) request_data = {"id": package["id"], "resources": package["resources"]} package = package_update(context, request_data) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert ( "%s added the resource %s to the dataset %s" % (user["fullname"], resource["name"], package["title"]) in stripped ), stripped # Update the package. package["title"] = "Billy's Updated Stats about Baseball Players" package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert "%s updated the dataset %s" % (user["fullname"], package["title"]) in stripped, stripped # Update the resource. resource = package["resources"][0] resource["name"] = "Chad Bradford Updated Stats" resource = resource_update(context, resource) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert ( "%s updated the resource %s in the dataset %s" % (user["fullname"], resource["name"], package["title"]) in stripped ), stripped # Delete the resource. context["allow_partial_update"] = False package["resources"] = [] package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert ( "%s deleted the resource %s from the dataset %s" % (user["fullname"], resource["name"], package["title"]) in stripped ), stripped # Follow the package. follow_dataset(context, {"id": package["id"]}) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert "%s started following %s" % (user["fullname"], package["title"]) not in stripped, stripped # Follow another user. follow_user(context, {"id": "joeadmin"}) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert "%s started following %s" % (user["fullname"], "joeadmin") not in stripped, stripped # Create a new group. group = {"name": "baseball-stats-group", "title": "A Group for Datasets about Baseball"} context["allow_partial_update"] = True group = group_create(context, group) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert "%s created the group %s" % (user["fullname"], group["title"]) in stripped, stripped # Update the group. group["title"] = "updated" group = group_update(context, group) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert "%s updated the group %s" % (user["fullname"], group["title"]) in stripped, stripped # Delete the group. group["state"] = "deleted" group_update(context, group) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert "%s deleted the group %s" % (user["fullname"], group["title"]) in stripped, stripped # Add a new tag to the package. tag = {"name": "baseball"} package["tags"].append(tag) package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert ( "%s added the tag %s to the dataset %s" % (user["fullname"], tag["name"], package["title"]) in stripped ), stripped # Remove the tag from the package. package["tags"] = [] context["allow_partial_update"] = False package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert ( "%s removed the tag %s from the dataset %s" % (user["fullname"], tag["name"], package["title"]) in stripped ), stripped # Add an extra to the package. package["extras"].append({"key": "quality", "value": "10000"}) package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert ( '%s added the extra "%s" to the dataset %s' % (user["fullname"], "quality", package["title"]) in stripped ), stripped # Update the extra. package["extras"][0]["value"] = "updated" package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert ( '%s changed the extra "%s" of the dataset %s' % (user["fullname"], "quality", package["title"]) in stripped ), stripped # Delete the extra. del package["extras"][0] package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert ( '%s deleted the extra "%s" from the dataset %s' % (user["fullname"], "quality", package["title"]) in stripped ), stripped # Delete the package. # we need to get round the delete permission context["ignore_auth"] = True package_delete(context, package) del context["ignore_auth"] result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert "%s deleted the dataset %s" % (user["fullname"], package["title"]) in stripped, stripped # Update the user's profile. user["about"] = "" user_update(context, user) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert "%s updated their profile" % user["fullname"] in stripped, stripped # By now we've created >15 activities, but only the latest 15 should # appear on the page. result = self.app.get(offset, status=200) assert result.body.count('<div class="activity">') == 15 # The user's dashboard page should load successfully and have the # latest 15 activities on it. offset = url_for(controller="user", action="dashboard") extra_environ = {"Authorization": str(ckan.model.User.get("billybeane").apikey)} result = self.app.post(offset, extra_environ=extra_environ, status=200) assert result.body.count('<div class="activity">') == 15
def create_organization(context, data_dict): context['ignore_auth'] = True model = context['model'] session = context['session'] destruction_secret = config.get(plugin_config_prefix + 'destruction_secret', 'changeme') client_id = data_dict.pop('client_id') client_secret = data_dict.pop('client_secret') instance_id = data_dict.pop('instance_id') # re-mapping received dict registration_uri = data_dict.pop('instance_registration_uri') organization = data_dict['organization'] user = data_dict['user'] user_dict = { 'id': user['id'], 'name': user['id'].replace('-', ''), 'email': user['email_address'], 'password': user['id'] } user_obj = model.User.get(user_dict['name']) org_dict = { 'type': 'organization', 'name': slugify(organization['name']), 'id': instance_id, 'title': organization['name'], 'user': user_dict['name'] } if not user_obj: user_create(context, user_dict) context['user'] = user_dict['name'] try: delete_uri = toolkit.url_for(host=request.host, controller='api', action='action', logic_function="delete-ozwillo-organization", ver=context['api_version'], qualified=True) organization_uri = toolkit.url_for(host=request.host, controller='organization', action='read', id=org_dict['name'], qualified=True) default_icon_url = toolkit.url_for(host=request.host, qualified=True, controller='home', action='index') + 'opendata.png' group_or_org_create(context, org_dict, is_org=True) # setting organization as active explicitely group = model.Group.get(org_dict['name']) group.state = 'active' group.image_url = default_icon_url group.save() model.repo.new_revision() model.GroupExtra(group_id=group.id, key='client_id', value=client_id).save() model.GroupExtra(group_id=group.id, key='client_secret', value=client_secret).save() session.flush() # notify about organization creation services = {'services': [{ 'local_id': 'organization', 'name': 'Open Data', 'service_uri': organization_uri + '/sso', 'description': 'Organization ' + org_dict['name'] + ' on CKAN', 'tos_uri': organization_uri, 'policy_uri': organization_uri, 'icon': group.image_url, 'payment_option': 'FREE', 'target_audience': ['PUBLIC_BODIES'], 'contacts': [organization_uri], 'redirect_uris': [organization_uri + '/callback'], 'post_logout_redirect_uris': [organization_uri + '/logout'], 'visible': False}], 'instance_id': instance_id, 'destruction_uri': delete_uri, 'destruction_secret': destruction_secret, 'needed_scopes': [{ 'scope_id': 'profile', 'motivation': 'Used to link user to the organization' }] } headers = {'Content-type': 'application/json', 'Accept': 'application/json'} requests.post(registration_uri, data=json.dumps(services), auth=(client_id, client_secret), headers=headers ) except logic.ValidationError, e: log.debug('Validation error "%s" occured while creating organization' % e) raise
if 'sub' in userinfo: userobj = model.User.get(userinfo['sub']) if not userobj: user_dict = { 'id': userinfo['sub'], 'name': userinfo['sub'].replace('-', ''), 'email': userinfo['email'], 'password': userinfo['sub'] } context = { 'ignore_auth': True, 'model': model, 'session': model.Session } user_create(context, user_dict) userobj = model.User.get(userinfo['sub']) if app_admin or app_user: member_dict = { 'id': g_.id, 'object': userinfo['sub'], 'object_type': 'user', 'capacity': 'admin', } member_create_context = { 'model': model, 'user': userobj.name, 'ignore_auth': True, 'session': session
def callback(id): ''' OID callback. If it fails (OIDCError), if the session has NOT been marked as been in the context of a login_to_org() call (rather than only an sso() one), tries to sso() to the organization with the next id in the order of the ozwillo_global_login_organization_names property if configured (by calling try_sso_next_login_org()) ; else displays a specific message ("not member of this org", rather than "Login Failed") ''' # Blueprint act strangely after user is logged in once. It will skip # SSO and user/login when trying to log in from different account and # directly get here. This is a workaround to force login user if not # redirected from loging page (as it sets important values in session) if not session.get('from_login'): return sso(id) from_login = session['from_login'] session['from_login'] = False g_ = model.Group.get(session.get('organization_id', id)) client = Clients.get_client(g_) org_url = str(url_for(controller="organization", action='read', id=g_.name)) try: # Grab state from query parameter if session does not have it session['state'] = session.get('state', request.params.get('state')) userinfo, app_admin, app_user, access_token, id_token \ = client.callback(session['state'], request.args) session['access_token'] = access_token session['id_token'] = id_token session.save() except OIDCError as e: is_login_to_org = 'is_login_to_org' in session and session[ 'is_login_to_org'] log.info('OIDCError is_login_to_org', is_login_to_org, e, session) # reinit for next time : session['is_login_to_org'] = False session.save() log.info('OIDCError is_login_to_org', is_login_to_org, e, session) if not is_login_to_org: sso_ok = try_sso_next_login_org(id) if sso_ok: return sso_ok flash_error("Login failed" if not is_login_to_org else "Vous n'ĂȘtes pas membre de cette organisation") return redirect_to(org_url, qualified=True) locale = None log.info('Received userinfo: %s' % userinfo) if 'locale' in userinfo: locale = userinfo.get('locale', '') if '-' in locale: locale, country = locale.split('-') org_url = str(url_for(org_url, locale=locale, qualified=True)) if 'sub' in userinfo: userobj = model.User.get(userinfo['sub']) if not userobj: user_dict = { 'id': userinfo['sub'], 'name': userinfo['sub'].replace('-', ''), 'email': userinfo['email'], 'password': userinfo['sub'] } context = { 'ignore_auth': True, 'model': model, 'session': model.Session } user_create(context, user_dict) userobj = model.User.get(userinfo['sub']) if app_admin or app_user: member_dict = { 'id': g_.id, 'object': userinfo['sub'], 'object_type': 'user', 'capacity': 'admin', } member_create_context = { 'model': model, 'user': userobj.name, 'ignore_auth': True, 'session': session } member_create(member_create_context, member_dict) if 'given_name' in userinfo: userobj.fullname = userinfo['given_name'] if 'family_name' in userinfo: userobj.fullname += ' ' + userinfo['family_name'] userobj.save() if 'nickname' in userinfo: userobj.name = userinfo['nickname'] try: userobj.save() except Exception as e: log.warning('Error while saving user name: %s' % e) session['user'] = userobj.id session.save() return redirect_to(org_url)
def custom_user_create(context, data_dict=None): from ckan.logic.auth import create newuser = user_create(context, data_dict) # log the user in programatically # set the auth_user_obj in the context so API calls behave # like the new user is logged in context["auth_user_obj"] = model.User.get(newuser["name"]) context["user"] = newuser["name"] # stash values stash_c_user_obj = getattr(c, "user_obj", None) stash_c_user = getattr(c, "user", None) # set them during member request create c.user_obj = model.User.get(newuser["name"]) c.user = newuser["name"] # find organization check boxes membership_requested = False orgs = toolkit.get_action("get_available_organizations")({}, {}) for org in orgs: id = "org-{}".format(org["name"]) log.debug("looking for {}".format(id)) if id in data_dict: log.debug("found {}".format(id)) membership_requested = True create_dict = {} create_dict["role"] = "member" create_dict["group"] = org.get("name") create_dict["message"] = data_dict.get("request_reason", "") try: request = toolkit.get_action("member_request_create")( context, create_dict) except Exception as e: log.exception("custom_user_create") # un stash values c.user_obj = stash_c_user_obj c.user = stash_c_user # if no requests, flash notice if not membership_requested: flash_notice( _('No initiative memberships were requested. Please request access using the "Memberships" button in the top right.' )) # send welcome to the new user url = config.get("ckan.site_url", "") site_name = config.get("ckan.site_description", "") site_email = os.environ.get("BIOPLATFORMS_HELPDESK_ADDRESS", config.get("error_email_from", "")) welcome = EmailUser() welcome.email = newuser["email"] welcome.username = newuser["name"] welcome.display_name = newuser["display_name"] mail_welcome_email(welcome, site_name, site_email, url) flash_success(_("Membership created. You have been logged in.")) return newuser
class OpenidController(base.BaseController): def sso(self, id): log.info('SSO for organization "%s"' % id) session['organization_id'] = id session.save() log.info('redirecting to login page') login_url = toolkit.url_for(host=request.host, controller='user', action='login', qualified=True) redirect_to(login_url) def callback(self): g = model.Group.get(session['organization_id']) client = Clients.get_client(g) org_url = str( toolkit.url_for(controller="organization", action='read', id=g.name)) try: userinfo, app_admin, app_user, access_token, id_token \ = client.callback(session['state'], request.GET) session['access_token'] = access_token session['id_token'] = id_token session.save() except OIDCError, e: flash_error('Login failed') redirect_to(org_url, qualified=True) locale = None log.info('Received userinfo: %s' % userinfo) if 'locale' in userinfo: locale = userinfo.get('locale', '') if '-' in locale: locale, country = locale.split('-') org_url = str(toolkit.url_for(org_url, locale=locale, qualified=True)) if 'sub' in userinfo: userobj = model.User.get(userinfo['sub']) if not userobj: user_dict = { 'id': userinfo['sub'], 'name': userinfo['sub'].replace('-', ''), 'email': userinfo['email'], 'password': userinfo['sub'] } context = { 'ignore_auth': True, 'model': model, 'session': model.Session } user_create(context, user_dict) userobj = model.User.get(userinfo['sub']) if app_admin or app_user: member_dict = { 'id': g.id, 'object': userinfo['sub'], 'object_type': 'user', 'capacity': 'admin', } member_create_context = { 'model': model, 'user': userobj.name, 'ignore_auth': True, 'session': session } member_create(member_create_context, member_dict) if 'given_name' in userinfo: userobj.fullname = userinfo['given_name'] if 'family_name' in userinfo: userobj.fullname += ' ' + userinfo['family_name'] userobj.save() if 'nickname' in userinfo: userobj.name = userinfo['nickname'] try: userobj.save() except Exception, e: log.warning('Error while saving user name: %s' % e) session['user'] = userobj.id session.save()