Skip to content

Commit 04a1513

Browse files
committed
Add user capacity to group levels
1 parent 91035b6 commit 04a1513

File tree

1 file changed

+116
-30
lines changed

1 file changed

+116
-30
lines changed

ckanext/hierarchy/logic/action.py

Lines changed: 116 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,43 @@
33
import ckan.plugins as p
44
import ckan.logic as logic
55
from ckanext.hierarchy.model import GroupTreeNode
6+
import ckan.authz as authz
67

78
log = logging.getLogger(__name__)
89
_get_or_bust = logic.get_or_bust
910

1011

1112
@logic.side_effect_free
1213
def group_tree(context, data_dict):
13-
'''Returns the full group tree hierarchy.
14-
15-
:returns: list of top-level GroupTreeNodes
16-
'''
1714
model = _get_or_bust(context, 'model')
1815
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)]
2243

2344
@logic.side_effect_free
2445
def group_tree_section(context, data_dict):
@@ -30,9 +51,27 @@ def group_tree_section(context, data_dict):
3051
:param include_siblingss: if false, excludes given group siblings
3152
:returns: the top GroupTreeNode of the tree section
3253
'''
33-
group_name_or_id = _get_or_bust(data_dict, 'id')
3454
model = _get_or_bust(context, 'model')
55+
group_name_or_id = _get_or_bust(data_dict, 'id')
3556
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+
3675
if group is None:
3776
raise p.toolkit.ObjectNotFound
3877
group_type = data_dict.get('type', 'group')
@@ -49,62 +88,109 @@ def group_tree_section(context, data_dict):
4988
or [group])[0]
5089
else:
5190
root_group = group
91+
5292
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+
)
55100
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+
)
59108
return _nest_group_tree_list(
60109
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+
)
62114

63115

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):
65120
'''Returns a tree branch composed by nesting the groups in the list.
66121
67122
:param group_tree_list: list of groups to build a tree, first is root
68123
:returns: the top GroupTreeNode of the tree
69124
'''
70125
root_node = None
71126
last_node = None
127+
72128
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+
77144
if not root_node:
78145
root_node = last_node = node
79146
else:
80147
last_node.add_child_node(node)
81148
last_node = node
149+
82150
last_node.add_child_node(group_tree_leaf)
83151
return root_node
84152

85153

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):
87155
'''Returns a branch of the group tree hierarchy, rooted in the given group.
88156
89157
:param root_group_id: group object at the top of the part of the tree
90158
:param highlight_group_name: group name that is to be flagged 'highlighted'
91159
:returns: the top GroupTreeNode of the tree
92160
'''
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)
98173
if root_group.name == highlight_group_name:
99174
nodes[root_group.id].highlight()
100175
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)
106191
nodes[parent_id].add_child_node(node)
107192
if highlight_group_name and group_name == highlight_group_name:
108193
node.highlight()
109194
nodes[group_id] = node
110195
return root_node
196+

0 commit comments

Comments
 (0)