-
Notifications
You must be signed in to change notification settings - Fork 2k
/
update.py
179 lines (142 loc) · 6.75 KB
/
update.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 ckan.logic import check_access_old, NotFound
from ckan.logic.auth import get_package_object, get_resource_object, get_group_object, get_authorization_group_object, \
get_user_object, get_resource_object
from ckan.logic.auth.create import check_group_auth, package_relationship_create
from ckan.authz import Authorizer
from ckan.lib.base import _
def make_latest_pending_package_active(context, data_dict):
return package_update(context, data_dict)
def package_update(context, data_dict):
model = context['model']
user = context.get('user')
package = get_package_object(context, data_dict)
check1 = check_access_old(package, model.Action.EDIT, context)
if not check1:
return {'success': False, 'msg': _('User %s not authorized to edit package %s') % (str(user), package.id)}
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 resource_update(context, data_dict):
model = context['model']
user = context.get('user')
resource = get_resource_object(context, data_dict)
# check authentication against package
query = model.Session.query(model.Package)\
.join(model.ResourceGroup)\
.join(model.Resource)\
.filter(model.ResourceGroup.id == resource.resource_group_id)
pkg = query.first()
if not pkg:
raise NotFound(_('No package found for this resource, cannot check auth.'))
pkg_dict = {'id': pkg.id}
authorized = package_update(context, pkg_dict).get('success')
if not authorized:
return {'success': False, 'msg': _('User %s not authorized to read edit %s') % (str(user), resource.id)}
else:
return {'success': True}
def package_relationship_update(context, data_dict):
return package_relationship_create(context, data_dict)
def package_change_state(context, data_dict):
model = context['model']
user = context['user']
package = get_package_object(context, data_dict)
authorized = check_access_old(package, model.Action.CHANGE_STATE, context)
if not authorized:
return {'success': False, 'msg': _('User %s not authorized to change state of package %s') % (str(user),package.id)}
else:
return {'success': True}
def package_edit_permissions(context, data_dict):
model = context['model']
user = context['user']
package = get_package_object(context, data_dict)
authorized = check_access_old(package, model.Action.EDIT_PERMISSIONS, context)
if not authorized:
return {'success': False, 'msg': _('User %s not authorized to edit permissions of package %s') % (str(user),package.id)}
else:
return {'success': True}
def group_update(context, data_dict):
model = context['model']
user = context['user']
group = get_group_object(context, data_dict)
authorized = check_access_old(group, model.Action.EDIT, context)
if not authorized:
return {'success': False, 'msg': _('User %s not authorized to edit group %s') % (str(user),group.id)}
else:
return {'success': True}
def group_change_state(context, data_dict):
model = context['model']
user = context['user']
group = get_group_object(context, data_dict)
authorized = check_access_old(group, model.Action.CHANGE_STATE, context)
if not authorized:
return {'success': False, 'msg': _('User %s not authorized to change state of group %s') % (str(user),group.id)}
else:
return {'success': True}
def group_edit_permissions(context, data_dict):
model = context['model']
user = context['user']
group = get_group_object(context, data_dict)
authorized = check_access_old(group, model.Action.EDIT_PERMISSIONS, context)
if not authorized:
return {'success': False, 'msg': _('User %s not authorized to edit permissions of group %s') % (str(user),group.id)}
else:
return {'success': True}
def authorization_group_update(context, data_dict):
model = context['model']
user = context['user']
authorization_group = get_authorization_group_object(context, data_dict)
authorized = check_access_old(authorization_group, model.Action.EDIT, context)
if not authorized:
return {'success': False, 'msg': _('User %s not authorized to edit permissions of authorization group %s') % (str(user),authorization_group.id)}
else:
return {'success': True}
def authorization_group_edit_permissions(context, data_dict):
model = context['model']
user = context['user']
authorization_group = get_authorization_group_object(context, data_dict)
authorized = check_access_old(authorization_group, model.Action.EDIT_PERMISSIONS, context)
if not authorized:
return {'success': False, 'msg': _('User %s not authorized to edit permissions of authorization group %s') % (str(user),authorization_group.id)}
else:
return {'success': True}
def user_update(context, data_dict):
model = context['model']
user = context['user']
user_obj = get_user_object(context, data_dict)
if not (Authorizer().is_sysadmin(unicode(user)) or user == user_obj.name) and \
not ('reset_key' in data_dict and data_dict['reset_key'] == user_obj.reset_key):
return {'success': False, 'msg': _('User %s not authorized to edit user %s') % (str(user), user_obj.id)}
return {'success': True}
def revision_change_state(context, data_dict):
model = context['model']
user = context['user']
authorized = Authorizer().is_authorized(user, model.Action.CHANGE_STATE, model.Revision)
if not authorized:
return {'success': False, 'msg': _('User %s not authorized to change state of revision' ) % str(user)}
else:
return {'success': True}
def task_status_update(context, data_dict):
model = context['model']
user = context['user']
if 'ignore_auth' in context and context['ignore_auth']:
return {'success': True}
authorized = Authorizer().is_sysadmin(unicode(user))
if not authorized:
return {'success': False, 'msg': _('User %s not authorized to update task_status table') % str(user)}
else:
return {'success': True}
## Modifications for rest api
def package_update_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 edit a package')}
return package_update(context, data_dict)
def group_update_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 edit a group')}
return group_update(context, data_dict)