-
Notifications
You must be signed in to change notification settings - Fork 2k
/
__init__.py
55 lines (39 loc) · 1.51 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# encoding: utf-8
'''
Helper functions to be used in the auth check functions
'''
import ckan.logic as logic
import ckan.authz as authz
def _get_object(context, data_dict, name, class_name):
# return the named item if in the context, or get it from model.class_name
try:
return context[name]
except KeyError:
model = context['model']
if not data_dict:
data_dict = {}
id = data_dict.get('id', None)
if not id:
raise logic.ValidationError('Missing id, can not get {0} object'
.format(class_name))
obj = getattr(model, class_name).get(id)
if not obj:
raise logic.NotFound
# Save in case we need this again during the request
context[name] = obj
return obj
def get_package_object(context, data_dict=None):
return _get_object(context, data_dict, 'package', 'Package')
def get_resource_object(context, data_dict=None):
return _get_object(context, data_dict, 'resource', 'Resource')
def get_group_object(context, data_dict=None):
return _get_object(context, data_dict, 'group', 'Group')
def get_user_object(context, data_dict=None):
return _get_object(context, data_dict, 'user_obj', 'User')
def get_activity_object(context, data_dict=None):
return _get_object(context, data_dict, 'activity', 'Activity')
def restrict_anon(context):
if authz.auth_is_anon_user(context):
return {'success': False}
else:
return {'success': True}