-
Notifications
You must be signed in to change notification settings - Fork 2k
/
update.py
148 lines (110 loc) · 5.45 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
from ckan.logic.auth import get_package_object, get_group_object, \
get_user_object, get_resource_object
from ckan.logic.auth.publisher import _groups_intersect
from ckan.logic.auth.publisher.create import package_relationship_create
from ckan.authz import Authorizer
from ckan.lib.base import _
# FIXME: Which is worse, 'from module import foo' or duplicating these
# functions in this module?
from ckan.logic.auth.update import vocabulary_update
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)
if Authorizer().is_sysadmin(unicode(user)):
return { 'success': True }
userobj = model.User.get( user )
if not userobj or \
not _groups_intersect( userobj.get_groups('publisher'), package.get_groups('publisher') ):
return {'success': False,
'msg': _('User %s not authorized to edit packages in these groups') % str(user)}
return {'success': True}
def resource_update(context, data_dict):
"""
Update resource permission checks the user is in a group that the resource's
package is also a member of.
"""
model = context['model']
user = context.get('user')
resource = get_resource_object(context, data_dict)
userobj = model.User.get( user )
if not userobj:
return {'success': False, 'msg': _('User %s not authorized to edit resources in this package') % str(user)}
if not _groups_intersect( userobj.get_groups('publisher'), resource.resource_group.package.get_groups('publisher') ):
return {'success': False, 'msg': _('User %s not authorized to edit resources in this package') % str(user)}
return {'success': True}
def package_relationship_update(context, data_dict):
return package_relationship_create(context, data_dict)
def package_change_state(context, data_dict):
return package_update( context, data_dict )
def package_edit_permissions(context, data_dict):
return {'success': False,
'msg': _('Package edit permissions is not available')}
def group_update(context, data_dict):
"""
Group edit permission. Checks that a valid user is supplied and that the user is
a member of the group currently with any capacity.
"""
model = context['model']
user = context.get('user','')
group = get_group_object(context, data_dict)
if not user:
return {'success': False, 'msg': _('Only members of this group are authorized to edit this group')}
# Sys admins should be allowed to update groups
if Authorizer().is_sysadmin(unicode(user)):
return { 'success': True }
# Only allow package update if the user and package groups intersect
userobj = model.User.get( user )
if not userobj:
return { 'success' : False, 'msg': _('Could not find user %s') % str(user) }
# Only admins of this group should be able to update this group
if not _groups_intersect( userobj.get_groups( 'publisher', 'admin' ), [group] ):
return { 'success': False, 'msg': _('User %s not authorized to edit this group') % str(user) }
return { 'success': True }
def group_change_state(context, data_dict):
return group_update(context, data_dict)
def group_edit_permissions(context, data_dict):
return {'success': False, 'msg': _('Group edit permissions is not implemented')}
def authorization_group_update(context, data_dict):
return {'success': False, 'msg': _('Authorization group update not implemented')}
def authorization_group_edit_permissions(context, data_dict):
return {'success': False, 'msg': _('Authorization group update not implemented')}
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_sysadmin(unicode(user))
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']
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)