From 5f38cfbc8072936c71d3e15d98a6df75079d046c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?John=20Molakvo=C3=A6=20=28skjnldsv=29?=
 <skjnldsv@protonmail.com>
Date: Sat, 17 Mar 2018 14:47:47 +0100
Subject: [PATCH] Return groups displayname in provisioning api
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
---
 apps/provisioning_api/appinfo/routes.php      |  1 +
 .../lib/Controller/GroupsController.php       | 27 ++++++++++++++++
 .../tests/Controller/GroupsControllerTest.php | 31 +++++++++++++++++++
 3 files changed, 59 insertions(+)

diff --git a/apps/provisioning_api/appinfo/routes.php b/apps/provisioning_api/appinfo/routes.php
index aa5a30199a7..34c488a39ca 100644
--- a/apps/provisioning_api/appinfo/routes.php
+++ b/apps/provisioning_api/appinfo/routes.php
@@ -34,6 +34,7 @@ return [
 
 		// Groups
 		['root' => '/cloud', 'name' => 'Groups#getGroups', 'url' => '/groups', 'verb' => 'GET'],
+		['root' => '/cloud', 'name' => 'Groups#getGroupsDetails', 'url' => '/groups/details', 'verb' => 'GET'],
 		['root' => '/cloud', 'name' => 'Groups#getGroup', 'url' => '/groups/{groupId}', 'verb' => 'GET'],
 		['root' => '/cloud', 'name' => 'Groups#addGroup', 'url' => '/groups', 'verb' => 'POST'],
 		['root' => '/cloud', 'name' => 'Groups#deleteGroup', 'url' => '/groups/{groupId}', 'verb' => 'DELETE'],
diff --git a/apps/provisioning_api/lib/Controller/GroupsController.php b/apps/provisioning_api/lib/Controller/GroupsController.php
index 8aed50bf049..d6f2d9f3391 100644
--- a/apps/provisioning_api/lib/Controller/GroupsController.php
+++ b/apps/provisioning_api/lib/Controller/GroupsController.php
@@ -96,6 +96,33 @@ class GroupsController extends OCSController {
 		return new DataResponse(['groups' => $groups]);
 	}
 
+	/**
+	 * returns a list of groups details with ids and displaynames
+	 *
+	 * @NoAdminRequired
+	 *
+	 * @param string $search
+	 * @param int $limit
+	 * @param int $offset
+	 * @return DataResponse
+	 */
+	public function getGroupsDetails(string $search = '', $limit = null, $offset = null): DataResponse {
+		if ($limit !== null) {
+			$limit = (int)$limit;
+		}
+		if ($offset !== null) {
+			$offset = (int)$offset;
+		}
+
+		$groups = $this->groupManager->search($search, $limit, $offset);
+		$groups = array_map(function($group) {
+			/** @var IGroup $group */
+			return ['id' => $group->getGID(), 'displayname' => $group->getDisplayName()];
+		}, $groups);
+
+		return new DataResponse(['groups' => $groups]);
+	}
+
 	/**
 	 * returns an array of users in the group specified
 	 *
diff --git a/apps/provisioning_api/tests/Controller/GroupsControllerTest.php b/apps/provisioning_api/tests/Controller/GroupsControllerTest.php
index cd3dae79336..352264c7afb 100644
--- a/apps/provisioning_api/tests/Controller/GroupsControllerTest.php
+++ b/apps/provisioning_api/tests/Controller/GroupsControllerTest.php
@@ -85,6 +85,10 @@ class GroupsControllerTest extends \Test\TestCase {
 		$group
 			->method('getGID')
 			->willReturn($gid);
+		$group
+			->method('getDisplayName')
+			->willReturn($gid.'-name');
+
 		return $group;
 	}
 
@@ -165,6 +169,33 @@ class GroupsControllerTest extends \Test\TestCase {
 
 		$result = $this->api->getGroups($search, $limit, $offset);
 		$this->assertEquals(['groups' => ['group1', 'group2']], $result->getData());
+
+	}
+
+	/**
+	 * @dataProvider dataGetGroups
+	 *
+	 * @param string|null $search
+	 * @param int|null $limit
+	 * @param int|null $offset
+	 */
+	public function testGetGroupsDetails($search, $limit, $offset) {
+		$groups = [$this->createGroup('group1'), $this->createGroup('group2')];
+
+		$search = $search === null ? '' : $search;
+
+		$this->groupManager
+			->expects($this->once())
+			->method('search')
+			->with($search, $limit, $offset)
+			->willReturn($groups);
+
+		$result = $this->api->getGroupsDetails($search, $limit, $offset);
+		$this->assertEquals(['groups' => [
+			Array('id' => 'group1', 'displayname' => 'group1-name'), 
+			Array('id' => 'group2', 'displayname' => 'group2-name')
+		]], $result->getData());
+
 	}
 
 	public function testGetGroupAsSubadmin() {
-- 
GitLab