Skip to content

Commit

Permalink
[logic] #1536 Fix group deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
David Read committed Dec 8, 2011
1 parent 63f4f6c commit 39b745f
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 5 deletions.
4 changes: 2 additions & 2 deletions ckan/lib/navl/dictization_functions.py
Expand Up @@ -167,7 +167,7 @@ def convert(converter, key, converted_data, errors, context):
return
except TypeError, e:
## hack to make sure the type error was caused by the wrong
## number of arguements given.
## number of arguments given.
if not converter.__name__ in str(e):
raise
except Invalid, e:
Expand All @@ -182,7 +182,7 @@ def convert(converter, key, converted_data, errors, context):
return
except TypeError, e:
## hack to make sure the type error was caused by the wrong
## number of arguements given.
## number of arguments given.
if not converter.__name__ in str(e):
raise

Expand Down
7 changes: 4 additions & 3 deletions ckan/logic/schema.py
Expand Up @@ -19,7 +19,8 @@
tag_name_validator,
tag_string_convert,
duplicate_extras_key,
ignore_not_admin,
ignore_not_package_admin,
ignore_not_group_admin,
no_http,
tag_not_uppercase,
user_name_validator,
Expand Down Expand Up @@ -95,7 +96,7 @@ def default_package_schema():
'notes': [ignore_missing, unicode],
'url': [ignore_missing, unicode],#, URL(add_http=False)],
'version': [ignore_missing, unicode, package_version_validator],
'state': [ignore_not_admin, ignore_missing],
'state': [ignore_not_package_admin, ignore_missing],
'__extras': [ignore],
'__junk': [empty],
'resources': default_resource_schema(),
Expand Down Expand Up @@ -158,7 +159,7 @@ def default_group_schema():
'name': [not_empty, unicode, name_validator, group_name_validator],
'title': [ignore_missing, unicode],
'description': [ignore_missing, unicode],
'state': [ignore],
'state': [ignore_not_group_admin, ignore_missing],
'created': [ignore],
'extras': default_extras_schema(),
'__extras': [ignore],
Expand Down
28 changes: 28 additions & 0 deletions ckan/logic/validators.py
Expand Up @@ -213,6 +213,11 @@ def tag_string_convert(key, data, errors, context):
tag_name_validator(tag, context)

def ignore_not_admin(key, data, errors, context):
# Deprecated in favour of ignore_not_package_admin
return ignore_not_package_admin(key, data, errors, context)

def ignore_not_package_admin(key, data, errors, context):
'''Ignore if the user is not allowed to administer the package specified.'''

model = context['model']
user = context.get('user')
Expand All @@ -234,6 +239,29 @@ def ignore_not_admin(key, data, errors, context):

data.pop(key)

def ignore_not_group_admin(key, data, errors, context):
'''Ignore if the user is not allowed to administer for the group specified.'''

model = context['model']
user = context.get('user')

if user and Authorizer.is_sysadmin(user):
return

authorized = False
group = context.get('group')
if group:
try:
check_access('group_change_state',context)
authorized = True
except NotAuthorized:
authorized = False

if (user and group and authorized):
return

data.pop(key)

def user_name_validator(key, data, errors, context):
model = context["model"]
session = context["session"]
Expand Down
1 change: 1 addition & 0 deletions ckan/templates/group/read.html
Expand Up @@ -23,6 +23,7 @@ <h3>Administrators</h3>
</py:match>

<py:match path="content">
<h3 py:if="c.group['state'] != 'active'">State: ${c.group['state']}</h3>
<div class="notes" py:if="str(c.group_description_formatted).strip()">
${c.group_description_formatted}
</div>
Expand Down
24 changes: 24 additions & 0 deletions ckan/tests/functional/test_group.py
Expand Up @@ -242,6 +242,30 @@ def test_edit_non_existent(self):
offset = url_for(controller='group', action='edit', id=name)
res = self.app.get(offset, status=404)

def test_delete(self):
group_name = 'deletetest'
CreateTestData.create_groups([{'name': group_name,
'packages': [self.packagename]}],
admin_user_name='russianfan')

group = model.Group.by_name(group_name)
offset = url_for(controller='group', action='edit', id=group_name)
res = self.app.get(offset, status=200, extra_environ={'REMOTE_USER': 'russianfan'})
main_res = self.main_div(res)
assert 'Edit: %s' % group.title in main_res, main_res
assert 'value="active" selected' in main_res, main_res

# delete
form = res.forms['group-edit']
form['state'] = 'deleted'
res = form.submit('save', status=302, extra_environ={'REMOTE_USER': 'russianfan'})

group = model.Group.by_name(group_name)
assert_equal(group.state, 'deleted')
res = self.app.get(offset, status=302)
res = res.follow()
assert res.request.url.startswith('/user/login'), res.request.url

class TestNew(FunctionalTestCase):
groupname = u'david'

Expand Down

0 comments on commit 39b745f

Please sign in to comment.