-
Notifications
You must be signed in to change notification settings - Fork 2k
/
create.py
178 lines (135 loc) · 5.5 KB
/
create.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from pylons import config
from paste.deploy.converters import asbool
import ckan.logic as logic
from ckan.authz import Authorizer
from ckan.lib.base import _
import ckan.new_authz as new_authz
def package_create(context, data_dict=None):
model = context['model']
user = context['user']
check1 = logic.check_access_old(model.System(), model.Action.PACKAGE_CREATE, context)
if not check1:
return {'success': False, 'msg': _('User %s not authorized to create packages') % str(user)}
else:
check2 = _check_group_auth(context,data_dict)
if not check2:
return {'success': False, 'msg': _('User %s not authorized to edit these groups') % str(user)}
return {'success': True}
def related_create(context, data_dict=None):
'''Users must be logged-in to create related items.
To create a featured item the user must be a sysadmin.
'''
model = context['model']
user = context['user']
userobj = model.User.get( user )
if userobj:
if data_dict.get('featured', 0) != 0:
return {'success': False,
'msg': _('You must be a sysadmin to create a featured '
'related item')}
return {'success': True}
return {'success': False, 'msg': _('You must be logged in to add a related item')}
def resource_create(context, data_dict):
# resource_create runs through package_update, no need to
# check users eligibility to add resource to package here.
model = context['model']
user = context['user']
check1 = logic.check_access_old(model.System(), model.Action.PACKAGE_CREATE, context)
if not check1:
return {'success': False, 'msg': _('User %s not authorized to create packages') % str(user)}
else:
check2 = _check_group_auth(context,data_dict)
if not check2:
return {'success': False, 'msg': _('User %s not authorized to edit these groups') % str(user)}
return {'success': True}
def package_relationship_create(context, data_dict):
model = context['model']
user = context['user']
id = data_dict['subject']
id2 = data_dict['object']
pkg1 = model.Package.get(id)
pkg2 = model.Package.get(id2)
authorized = Authorizer().\
authorized_package_relationship(\
user, pkg1, pkg2, action=model.Action.EDIT)
if not authorized:
return {'success': False, 'msg': _('User %s not authorized to edit these packages') % str(user)}
else:
return {'success': True}
def group_create(context, data_dict=None):
user = context['user']
user = new_authz.get_user_id_for_username(user, allow_none=True)
if user and asbool(config.get('ckan.auth.user_create_groups', False)):
return {'success': True}
return {'success': False,
'msg': _('User %s not authorized to create groups') % user}
def organization_create(context, data_dict=None):
user = context['user']
user = new_authz.get_user_id_for_username(user, allow_none=True)
if user and asbool(config.get('ckan.auth.user_create_organizations', False)):
return {'success': True}
return {'success': False,
'msg': _('User %s not authorized to create organizations') % user}
def rating_create(context, data_dict):
# No authz check in the logic function
return {'success': True}
def user_create(context, data_dict=None):
model = context['model']
user = context['user']
authorized = logic.check_access_old(model.System(), model.Action.USER_CREATE, context)
if not authorized:
return {'success': False, 'msg': _('User %s not authorized to create users') % str(user)}
else:
return {'success': True}
def _check_group_auth(context, data_dict):
if not data_dict:
return True
model = context['model']
pkg = context.get("package")
api_version = context.get('api_version') or '1'
group_blobs = data_dict.get("groups", [])
groups = set()
for group_blob in group_blobs:
# group_blob might be a dict or a group_ref
if isinstance(group_blob, dict):
if api_version == '1':
id = group_blob.get('name')
else:
id = group_blob.get('id')
if not id:
continue
else:
id = group_blob
grp = model.Group.get(id)
if grp is None:
raise logic.NotFound(_('Group was not found.'))
groups.add(grp)
if pkg:
pkg_groups = pkg.get_groups()
groups = groups - set(pkg_groups)
for group in groups:
if not logic.check_access_old(group, model.Action.EDIT, context):
return False
return True
## Modifications for rest api
def package_create_rest(context, data_dict):
model = context['model']
user = context['user']
if user in (model.PSEUDO_USER__VISITOR, ''):
return {'success': False, 'msg': _('Valid API key needed to create a package')}
return package_create(context, data_dict)
def group_create_rest(context, data_dict):
model = context['model']
user = context['user']
if user in (model.PSEUDO_USER__VISITOR, ''):
return {'success': False, 'msg': _('Valid API key needed to create a group')}
return group_create(context, data_dict)
def vocabulary_create(context, data_dict):
# sysadmins only
return {'success': False}
def activity_create(context, data_dict):
# sysadmins only
return {'success': False}
def tag_create(context, data_dict):
# sysadmins only
return {'success': False}