diff --git a/api/dao/base.py b/api/dao/base.py index b0ac55177..5d01b2d2a 100644 --- a/api/dao/base.py +++ b/api/dao/base.py @@ -80,12 +80,18 @@ def get_container(self, _id, projection=None, get_children=False): cont[CHILD_MAP[self.cont_name]] = children return cont - def get_children(self, _id, projection=None): + def get_children(self, _id, projection=None, uid=None): try: child_name = CHILD_MAP[self.cont_name] except KeyError: raise APINotFoundException('Children cannot be listed from the {0} level'.format(self.cont_name)) - query = {self.cont_name[:-1]: bson.ObjectId(_id)} + if not self.use_object_id: + query = {containerutil.singularize(self.cont_name): _id} + else: + query = {containerutil.singularize(self.cont_name): bson.ObjectId(_id)} + + if uid: + query['permissions'] = {'$elemMatch': {'_id': uid}} if not projection: projection = {'info': 0, 'files.info': 0, 'subject': 0, 'tags': 0} return ContainerStorage.factory(child_name).get_all_el(query, None, projection) diff --git a/api/handlers/grouphandler.py b/api/handlers/grouphandler.py index b908b7204..2b55f9fd8 100644 --- a/api/handlers/grouphandler.py +++ b/api/handlers/grouphandler.py @@ -36,6 +36,14 @@ def delete(self, _id): self.abort(404, 'Group {} not removed'.format(_id)) return result + def handle_projects(self, result): + """ + If parameter join=projects, return project id, label, permissions, and description + """ + result['projects'] = self.storage.get_children(result.get('_id'), projection={'permissions': 1, 'label': 1, 'description': 1, 'group': 1}, uid=self.uid) + self._filter_permissions(result['projects'], self.uid) + return result + def get_all(self, uid=None): projection = {'label': 1, 'created': 1, 'modified': 1, 'permissions': 1, 'tags': 1} permchecker = groupauth.list_permission_checker(self, uid) @@ -44,6 +52,9 @@ def get_all(self, uid=None): self._filter_permissions(results, self.uid) if self.is_true('join_avatars'): results = ContainerHandler.join_user_info(results) + if 'projects' in self.request.params.getall('join'): + for result in results: + result = self.handle_projects(result) return results @validators.verify_payload_exists diff --git a/test/integration_tests/python/test_groups.py b/test/integration_tests/python/test_groups.py index 30433bab4..42c457fb3 100644 --- a/test/integration_tests/python/test_groups.py +++ b/test/integration_tests/python/test_groups.py @@ -118,3 +118,9 @@ def test_groups(as_user, as_admin, data_builder): r = as_admin.get('/groups/' + group) assert r.ok assert r.json()['label'] == group_label + + # Test join=projects + project = data_builder.create_project() + r = as_admin.get('/groups', params={'join': 'projects'}) + assert r.ok + assert r.json()[1].get('projects')[0].get('_id') == project