3
3
import ckan .plugins as p
4
4
import ckan .logic as logic
5
5
from ckanext .hierarchy .model import GroupTreeNode
6
+ import ckan .authz as authz
6
7
7
8
log = logging .getLogger (__name__ )
8
9
_get_or_bust = logic .get_or_bust
9
10
10
11
11
12
@logic .side_effect_free
12
13
def group_tree (context , data_dict ):
13
- '''Returns the full group tree hierarchy.
14
-
15
- :returns: list of top-level GroupTreeNodes
16
- '''
17
14
model = _get_or_bust (context , 'model' )
18
15
group_type = data_dict .get ('type' , 'group' )
19
- return [_group_tree_branch (group , type = group_type )
20
- for group in model .Group .get_top_level_groups (type = group_type )]
21
-
16
+
17
+ # Check for sysadmin status
18
+ is_sysadmin = False
19
+ user_capacities = {}
20
+ auth_user_obj = context .get ('auth_user_obj' )
21
+ user = context .get ('user' )
22
+
23
+ if user :
24
+ log .error ("Enters here as user" )
25
+ if authz .is_sysadmin (user ):
26
+ is_sysadmin = True
27
+ else :
28
+ # Only query capacities if not sysadmin
29
+ user_id = auth_user_obj .id
30
+ members = model .Session .query (model .Member ).filter (
31
+ model .Member .table_name == 'user' ,
32
+ model .Member .table_id == user_id ,
33
+ model .Member .state == 'active'
34
+ ).all ()
35
+ user_capacities = {m .group_id : m .capacity for m in members }
36
+
37
+ return [_group_tree_branch (
38
+ group ,
39
+ type = group_type ,
40
+ user_capacities = user_capacities ,
41
+ is_sysadmin = is_sysadmin
42
+ ) for group in model .Group .get_top_level_groups (type = group_type )]
22
43
23
44
@logic .side_effect_free
24
45
def group_tree_section (context , data_dict ):
@@ -30,9 +51,27 @@ def group_tree_section(context, data_dict):
30
51
:param include_siblingss: if false, excludes given group siblings
31
52
:returns: the top GroupTreeNode of the tree section
32
53
'''
33
- group_name_or_id = _get_or_bust (data_dict , 'id' )
34
54
model = _get_or_bust (context , 'model' )
55
+ group_name_or_id = _get_or_bust (data_dict , 'id' )
35
56
group = model .Group .get (group_name_or_id )
57
+
58
+ # Check for sysadmin status
59
+ is_sysadmin = False
60
+ user_capacities = {}
61
+ auth_user_obj = context .get ('auth_user_obj' )
62
+ user = context .get ('user' )
63
+ if user :
64
+ if authz .is_sysadmin (user ):
65
+ is_sysadmin = True
66
+ else :
67
+ user_id = auth_user_obj .id
68
+ members = model .Session .query (model .Member ).filter (
69
+ model .Member .table_name == 'user' ,
70
+ model .Member .table_id == user_id ,
71
+ model .Member .state == 'active'
72
+ ).all ()
73
+ user_capacities = {m .group_id : m .capacity for m in members }
74
+
36
75
if group is None :
37
76
raise p .toolkit .ObjectNotFound
38
77
group_type = data_dict .get ('type' , 'group' )
@@ -49,62 +88,109 @@ def group_tree_section(context, data_dict):
49
88
or [group ])[0 ]
50
89
else :
51
90
root_group = group
91
+
52
92
if include_siblings or root_group == group :
53
- return _group_tree_branch (root_group , highlight_group_name = group .name ,
54
- type = group_type )
93
+ return _group_tree_branch (
94
+ root_group ,
95
+ highlight_group_name = group .name ,
96
+ type = group_type ,
97
+ user_capacities = user_capacities ,
98
+ is_sysadmin = is_sysadmin
99
+ )
55
100
else :
56
- section_subtree = _group_tree_branch (group ,
57
- highlight_group_name = group .name ,
58
- type = group_type )
101
+ section_subtree = _group_tree_branch (
102
+ group ,
103
+ highlight_group_name = group .name ,
104
+ type = group_type ,
105
+ user_capacities = user_capacities ,
106
+ is_sysadmin = is_sysadmin
107
+ )
59
108
return _nest_group_tree_list (
60
109
group .get_parent_group_hierarchy (type = group_type ),
61
- section_subtree )
110
+ section_subtree ,
111
+ user_capacities = user_capacities ,
112
+ is_sysadmin = is_sysadmin
113
+ )
62
114
63
115
64
- def _nest_group_tree_list (group_tree_list , group_tree_leaf ):
116
+
117
+
118
+ def _nest_group_tree_list (group_tree_list , group_tree_leaf ,
119
+ user_capacities = None , is_sysadmin = False ):
65
120
'''Returns a tree branch composed by nesting the groups in the list.
66
121
67
122
:param group_tree_list: list of groups to build a tree, first is root
68
123
:returns: the top GroupTreeNode of the tree
69
124
'''
70
125
root_node = None
71
126
last_node = None
127
+
72
128
for group in group_tree_list :
73
- node = GroupTreeNode (
74
- {'id' : group .id ,
75
- 'name' : group .name ,
76
- 'title' : group .title })
129
+ node_dict = {
130
+ 'id' : group .id ,
131
+ 'name' : group .name ,
132
+ 'title' : group .title ,
133
+ 'type' : group .type
134
+ }
135
+
136
+ # Set capacity for sysadmin
137
+ if is_sysadmin :
138
+ node_dict ['capacity' ] = 'admin'
139
+ else :
140
+ node_dict ['capacity' ] = user_capacities .get (group .id , 'member' )
141
+
142
+ node = GroupTreeNode (node_dict )
143
+
77
144
if not root_node :
78
145
root_node = last_node = node
79
146
else :
80
147
last_node .add_child_node (node )
81
148
last_node = node
149
+
82
150
last_node .add_child_node (group_tree_leaf )
83
151
return root_node
84
152
85
153
86
- def _group_tree_branch (root_group , highlight_group_name = None , type = 'group' ):
154
+ def _group_tree_branch (root_group , highlight_group_name = None , type = 'group' , user_capacities = None , is_sysadmin = False ):
87
155
'''Returns a branch of the group tree hierarchy, rooted in the given group.
88
156
89
157
:param root_group_id: group object at the top of the part of the tree
90
158
:param highlight_group_name: group name that is to be flagged 'highlighted'
91
159
:returns: the top GroupTreeNode of the tree
92
160
'''
93
- nodes = {} # group_id: GroupTreeNode()
94
- root_node = nodes [root_group .id ] = GroupTreeNode (
95
- {'id' : root_group .id ,
96
- 'name' : root_group .name ,
97
- 'title' : root_group .title })
161
+ nodes = {}
162
+ root_dict = {
163
+ 'id' : root_group .id ,
164
+ 'name' : root_group .name ,
165
+ 'title' : root_group .title ,
166
+ 'type' : root_group .type
167
+ }
168
+ if is_sysadmin :
169
+ root_dict ['capacity' ] = 'admin'
170
+ else :
171
+ root_dict ['capacity' ] = user_capacities .get (root_group .id )
172
+ root_node = nodes [root_group .id ] = GroupTreeNode (root_dict )
98
173
if root_group .name == highlight_group_name :
99
174
nodes [root_group .id ].highlight ()
100
175
highlight_group_name = None
101
- for group_id , group_name , group_title , parent_id in \
102
- root_group .get_children_group_hierarchy (type = type ):
103
- node = GroupTreeNode ({'id' : group_id ,
104
- 'name' : group_name ,
105
- 'title' : group_title })
176
+ for group_id , group_name , group_title , parent_id in root_group .get_children_group_hierarchy (type = type ):
177
+ node_dict = {
178
+ 'id' : group_id ,
179
+ 'name' : group_name ,
180
+ 'title' : group_title ,
181
+ 'type' : type
182
+ }
183
+ if is_sysadmin :
184
+ node_dict ['capacity' ] = 'admin'
185
+ else :
186
+ capacity = user_capacities .get (group_id )
187
+ if capacity is None :
188
+ capacity = root_dict ['capacity' ]
189
+ node_dict ['capacity' ] = capacity
190
+ node = GroupTreeNode (node_dict )
106
191
nodes [parent_id ].add_child_node (node )
107
192
if highlight_group_name and group_name == highlight_group_name :
108
193
node .highlight ()
109
194
nodes [group_id ] = node
110
195
return root_node
196
+
0 commit comments