Completed
Push — master ( 7c83a5...58b404 )
by
unknown
31:06
created
lib/private/Teams/TeamManager.php 1 patch
Indentation   +153 added lines, -153 removed lines patch added patch discarded remove patch
@@ -22,157 +22,157 @@
 block discarded – undo
22 22
 
23 23
 class TeamManager implements ITeamManager {
24 24
 
25
-	/** @var ?ITeamResourceProvider[] */
26
-	private ?array $providers = null;
27
-
28
-	public function __construct(
29
-		private Coordinator $bootContext,
30
-		private IURLGenerator $urlGenerator,
31
-		private ?CirclesManager $circlesManager,
32
-	) {
33
-	}
34
-
35
-	public function hasTeamSupport(): bool {
36
-		return $this->circlesManager !== null;
37
-	}
38
-
39
-	public function getProviders(): array {
40
-		if (!$this->hasTeamSupport()) {
41
-			return [];
42
-		}
43
-
44
-		if ($this->providers !== null) {
45
-			return $this->providers;
46
-		}
47
-
48
-		$this->providers = [];
49
-		foreach ($this->bootContext->getRegistrationContext()->getTeamResourceProviders() as $providerRegistration) {
50
-			try {
51
-				/** @var ITeamResourceProvider $provider */
52
-				$provider = Server::get($providerRegistration->getService());
53
-				$this->providers[$provider->getId()] = $provider;
54
-			} catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
55
-			}
56
-		}
57
-		return $this->providers;
58
-	}
59
-
60
-	public function getProvider(string $providerId): ITeamResourceProvider {
61
-		$providers = $this->getProviders();
62
-		if (isset($providers[$providerId])) {
63
-			return $providers[$providerId];
64
-		}
65
-
66
-		throw new \RuntimeException('No provider found for id ' . $providerId);
67
-	}
68
-
69
-	public function getSharedWith(string $teamId, string $userId): array {
70
-		if (!$this->hasTeamSupport()) {
71
-			return [];
72
-		}
73
-
74
-		if ($this->getTeam($teamId, $userId) === null) {
75
-			return [];
76
-		}
77
-
78
-		$resources = [];
79
-
80
-		foreach ($this->getProviders() as $provider) {
81
-			array_push($resources, ...$provider->getSharedWith($teamId));
82
-		}
83
-
84
-		return array_values($resources);
85
-	}
86
-
87
-	public function getSharedWithList(array $teams, string $userId): array {
88
-		if (!$this->hasTeamSupport()) {
89
-			return [];
90
-		}
91
-
92
-		$resources = [];
93
-		foreach ($this->getProviders() as $provider) {
94
-			if (method_exists($provider, 'getSharedWithList')) {
95
-				$resources[] = $provider->getSharedWithList($teams, $userId);
96
-			} else {
97
-				foreach ($teams as $team) {
98
-					$resources[] = [$team => $provider->getSharedWith($team)];
99
-				}
100
-			}
101
-		}
102
-
103
-		return array_merge_recursive(...$resources);
104
-	}
105
-
106
-	public function getTeamsForResource(string $providerId, string $resourceId, string $userId): array {
107
-		if (!$this->hasTeamSupport()) {
108
-			return [];
109
-		}
110
-
111
-		$provider = $this->getProvider($providerId);
112
-		return array_map(function (Circle $team) {
113
-			return new Team(
114
-				$team->getSingleId(),
115
-				$team->getDisplayName(),
116
-				$this->urlGenerator->linkToRouteAbsolute('contacts.contacts.directcircle', ['singleId' => $team->getSingleId()]),
117
-			);
118
-		}, $this->getTeams($provider->getTeamsForResource($resourceId), $userId));
119
-	}
120
-
121
-	private function getTeam(string $teamId, string $userId): ?Circle {
122
-		if (!$this->hasTeamSupport()) {
123
-			return null;
124
-		}
125
-
126
-		try {
127
-			$federatedUser = $this->circlesManager->getFederatedUser($userId, Member::TYPE_USER);
128
-			$this->circlesManager->startSession($federatedUser);
129
-			return $this->circlesManager->getCircle($teamId);
130
-		} catch (CircleNotFoundException) {
131
-			return null;
132
-		}
133
-	}
134
-
135
-	/**
136
-	 * @return string[]
137
-	 */
138
-	public function getMembersOfTeam(string $teamId, string $userId): array {
139
-		$team = $this->getTeam($teamId, $userId);
140
-		if ($team === null) {
141
-			return [];
142
-		}
143
-		$members = $team->getInheritedMembers();
144
-		return array_map(fn ($member) => $member->getUserId(), $members);
145
-	}
146
-
147
-	/**
148
-	 * @return Circle[]
149
-	 */
150
-	private function getTeams(array $teams, string $userId): array {
151
-		if (!$this->hasTeamSupport()) {
152
-			return [];
153
-		}
154
-
155
-		$federatedUser = $this->circlesManager->getFederatedUser($userId, Member::TYPE_USER);
156
-		$this->circlesManager->startSession($federatedUser);
157
-		return $this->circlesManager->getCirclesByIds($teams);
158
-	}
159
-
160
-	public function getTeamsForUser(string $userId): array {
161
-		if (!$this->hasTeamSupport()) {
162
-			return [];
163
-		}
164
-
165
-		$federatedUser = $this->circlesManager->getFederatedUser($userId, Member::TYPE_USER);
166
-		$this->circlesManager->startSession($federatedUser);
167
-		$teams = [];
168
-		foreach ($this->circlesManager->probeCircles() as $team) {
169
-			$teams[] = new Team(
170
-				$team->getSingleId(),
171
-				$team->getDisplayName(),
172
-				$this->urlGenerator->linkToRouteAbsolute('contacts.contacts.directcircle', ['singleId' => $team->getSingleId()]),
173
-			);
174
-		}
175
-
176
-		return $teams;
177
-	}
25
+    /** @var ?ITeamResourceProvider[] */
26
+    private ?array $providers = null;
27
+
28
+    public function __construct(
29
+        private Coordinator $bootContext,
30
+        private IURLGenerator $urlGenerator,
31
+        private ?CirclesManager $circlesManager,
32
+    ) {
33
+    }
34
+
35
+    public function hasTeamSupport(): bool {
36
+        return $this->circlesManager !== null;
37
+    }
38
+
39
+    public function getProviders(): array {
40
+        if (!$this->hasTeamSupport()) {
41
+            return [];
42
+        }
43
+
44
+        if ($this->providers !== null) {
45
+            return $this->providers;
46
+        }
47
+
48
+        $this->providers = [];
49
+        foreach ($this->bootContext->getRegistrationContext()->getTeamResourceProviders() as $providerRegistration) {
50
+            try {
51
+                /** @var ITeamResourceProvider $provider */
52
+                $provider = Server::get($providerRegistration->getService());
53
+                $this->providers[$provider->getId()] = $provider;
54
+            } catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
55
+            }
56
+        }
57
+        return $this->providers;
58
+    }
59
+
60
+    public function getProvider(string $providerId): ITeamResourceProvider {
61
+        $providers = $this->getProviders();
62
+        if (isset($providers[$providerId])) {
63
+            return $providers[$providerId];
64
+        }
65
+
66
+        throw new \RuntimeException('No provider found for id ' . $providerId);
67
+    }
68
+
69
+    public function getSharedWith(string $teamId, string $userId): array {
70
+        if (!$this->hasTeamSupport()) {
71
+            return [];
72
+        }
73
+
74
+        if ($this->getTeam($teamId, $userId) === null) {
75
+            return [];
76
+        }
77
+
78
+        $resources = [];
79
+
80
+        foreach ($this->getProviders() as $provider) {
81
+            array_push($resources, ...$provider->getSharedWith($teamId));
82
+        }
83
+
84
+        return array_values($resources);
85
+    }
86
+
87
+    public function getSharedWithList(array $teams, string $userId): array {
88
+        if (!$this->hasTeamSupport()) {
89
+            return [];
90
+        }
91
+
92
+        $resources = [];
93
+        foreach ($this->getProviders() as $provider) {
94
+            if (method_exists($provider, 'getSharedWithList')) {
95
+                $resources[] = $provider->getSharedWithList($teams, $userId);
96
+            } else {
97
+                foreach ($teams as $team) {
98
+                    $resources[] = [$team => $provider->getSharedWith($team)];
99
+                }
100
+            }
101
+        }
102
+
103
+        return array_merge_recursive(...$resources);
104
+    }
105
+
106
+    public function getTeamsForResource(string $providerId, string $resourceId, string $userId): array {
107
+        if (!$this->hasTeamSupport()) {
108
+            return [];
109
+        }
110
+
111
+        $provider = $this->getProvider($providerId);
112
+        return array_map(function (Circle $team) {
113
+            return new Team(
114
+                $team->getSingleId(),
115
+                $team->getDisplayName(),
116
+                $this->urlGenerator->linkToRouteAbsolute('contacts.contacts.directcircle', ['singleId' => $team->getSingleId()]),
117
+            );
118
+        }, $this->getTeams($provider->getTeamsForResource($resourceId), $userId));
119
+    }
120
+
121
+    private function getTeam(string $teamId, string $userId): ?Circle {
122
+        if (!$this->hasTeamSupport()) {
123
+            return null;
124
+        }
125
+
126
+        try {
127
+            $federatedUser = $this->circlesManager->getFederatedUser($userId, Member::TYPE_USER);
128
+            $this->circlesManager->startSession($federatedUser);
129
+            return $this->circlesManager->getCircle($teamId);
130
+        } catch (CircleNotFoundException) {
131
+            return null;
132
+        }
133
+    }
134
+
135
+    /**
136
+     * @return string[]
137
+     */
138
+    public function getMembersOfTeam(string $teamId, string $userId): array {
139
+        $team = $this->getTeam($teamId, $userId);
140
+        if ($team === null) {
141
+            return [];
142
+        }
143
+        $members = $team->getInheritedMembers();
144
+        return array_map(fn ($member) => $member->getUserId(), $members);
145
+    }
146
+
147
+    /**
148
+     * @return Circle[]
149
+     */
150
+    private function getTeams(array $teams, string $userId): array {
151
+        if (!$this->hasTeamSupport()) {
152
+            return [];
153
+        }
154
+
155
+        $federatedUser = $this->circlesManager->getFederatedUser($userId, Member::TYPE_USER);
156
+        $this->circlesManager->startSession($federatedUser);
157
+        return $this->circlesManager->getCirclesByIds($teams);
158
+    }
159
+
160
+    public function getTeamsForUser(string $userId): array {
161
+        if (!$this->hasTeamSupport()) {
162
+            return [];
163
+        }
164
+
165
+        $federatedUser = $this->circlesManager->getFederatedUser($userId, Member::TYPE_USER);
166
+        $this->circlesManager->startSession($federatedUser);
167
+        $teams = [];
168
+        foreach ($this->circlesManager->probeCircles() as $team) {
169
+            $teams[] = new Team(
170
+                $team->getSingleId(),
171
+                $team->getDisplayName(),
172
+                $this->urlGenerator->linkToRouteAbsolute('contacts.contacts.directcircle', ['singleId' => $team->getSingleId()]),
173
+            );
174
+        }
175
+
176
+        return $teams;
177
+    }
178 178
 }
Please login to merge, or discard this patch.
tests/Core/Controller/ContactsMenuControllerTest.php 1 patch
Indentation   +106 added lines, -106 removed lines patch added patch discarded remove patch
@@ -18,110 +18,110 @@
 block discarded – undo
18 18
 use Test\TestCase;
19 19
 
20 20
 class ContactsMenuControllerTest extends TestCase {
21
-	private IUserSession&MockObject $userSession;
22
-	private Manager&MockObject $contactsManager;
23
-	private TeamManager&MockObject $teamManager;
24
-
25
-	private ContactsMenuController $controller;
26
-
27
-	protected function setUp(): void {
28
-		parent::setUp();
29
-
30
-		$request = $this->createMock(IRequest::class);
31
-		$this->userSession = $this->createMock(IUserSession::class);
32
-		$this->contactsManager = $this->createMock(Manager::class);
33
-		$this->teamManager = $this->createMock(TeamManager::class);
34
-
35
-		$this->controller = new ContactsMenuController(
36
-			$request,
37
-			$this->userSession,
38
-			$this->contactsManager,
39
-			$this->teamManager,
40
-		);
41
-	}
42
-
43
-	public function testIndex(): void {
44
-		$user = $this->createMock(IUser::class);
45
-		$entries = [
46
-			$this->createMock(IEntry::class),
47
-			$this->createMock(IEntry::class),
48
-		];
49
-		$this->userSession->expects($this->once())
50
-			->method('getUser')
51
-			->willReturn($user);
52
-		$this->contactsManager->expects($this->once())
53
-			->method('getEntries')
54
-			->with($this->equalTo($user), $this->equalTo(null))
55
-			->willReturn($entries);
56
-
57
-		$response = $this->controller->index();
58
-
59
-		$this->assertEquals($entries, $response);
60
-	}
61
-
62
-	public function testIndex_withTeam(): void {
63
-		$user = $this->createMock(IUser::class);
64
-		$user->method('getUID')
65
-			->willReturn('current-user');
66
-
67
-		$entries = [
68
-			$this->createMock(IEntry::class),
69
-			$this->createMock(IEntry::class),
70
-		];
71
-		$entries[0]->method('getProperty')
72
-			->with('UID')
73
-			->willReturn('member1');
74
-		$entries[0]->method('getProperty')
75
-			->with('UID')
76
-			->willReturn('member2');
77
-
78
-		$this->userSession->expects($this->atLeastOnce())
79
-			->method('getUser')
80
-			->willReturn($user);
81
-		$this->contactsManager->expects($this->once())
82
-			->method('getEntries')
83
-			->with($this->equalTo($user), $this->equalTo(null))
84
-			->willReturn(['contacts' => $entries]);
85
-
86
-		$this->teamManager->expects($this->once())
87
-			->method('getMembersOfTeam')
88
-			->with('team-id', 'current-user')
89
-			->willReturn(['member1', 'member3']);
90
-
91
-		$response = $this->controller->index(teamId: 'team-id');
92
-
93
-		$this->assertEquals([$entries[0]], $response['contacts']);
94
-	}
95
-
96
-	public function testFindOne(): void {
97
-		$user = $this->createMock(IUser::class);
98
-		$entry = $this->createMock(IEntry::class);
99
-		$this->userSession->expects($this->once())
100
-			->method('getUser')
101
-			->willReturn($user);
102
-		$this->contactsManager->expects($this->once())
103
-			->method('findOne')
104
-			->with($this->equalTo($user), $this->equalTo(42), $this->equalTo('test-search-phrase'))
105
-			->willReturn($entry);
106
-
107
-		$response = $this->controller->findOne(42, 'test-search-phrase');
108
-
109
-		$this->assertEquals($entry, $response);
110
-	}
111
-
112
-	public function testFindOne404(): void {
113
-		$user = $this->createMock(IUser::class);
114
-		$this->userSession->expects($this->once())
115
-			->method('getUser')
116
-			->willReturn($user);
117
-		$this->contactsManager->expects($this->once())
118
-			->method('findOne')
119
-			->with($this->equalTo($user), $this->equalTo(42), $this->equalTo('test-search-phrase'))
120
-			->willReturn(null);
121
-
122
-		$response = $this->controller->findOne(42, 'test-search-phrase');
123
-
124
-		$this->assertEquals([], $response->getData());
125
-		$this->assertEquals(404, $response->getStatus());
126
-	}
21
+    private IUserSession&MockObject $userSession;
22
+    private Manager&MockObject $contactsManager;
23
+    private TeamManager&MockObject $teamManager;
24
+
25
+    private ContactsMenuController $controller;
26
+
27
+    protected function setUp(): void {
28
+        parent::setUp();
29
+
30
+        $request = $this->createMock(IRequest::class);
31
+        $this->userSession = $this->createMock(IUserSession::class);
32
+        $this->contactsManager = $this->createMock(Manager::class);
33
+        $this->teamManager = $this->createMock(TeamManager::class);
34
+
35
+        $this->controller = new ContactsMenuController(
36
+            $request,
37
+            $this->userSession,
38
+            $this->contactsManager,
39
+            $this->teamManager,
40
+        );
41
+    }
42
+
43
+    public function testIndex(): void {
44
+        $user = $this->createMock(IUser::class);
45
+        $entries = [
46
+            $this->createMock(IEntry::class),
47
+            $this->createMock(IEntry::class),
48
+        ];
49
+        $this->userSession->expects($this->once())
50
+            ->method('getUser')
51
+            ->willReturn($user);
52
+        $this->contactsManager->expects($this->once())
53
+            ->method('getEntries')
54
+            ->with($this->equalTo($user), $this->equalTo(null))
55
+            ->willReturn($entries);
56
+
57
+        $response = $this->controller->index();
58
+
59
+        $this->assertEquals($entries, $response);
60
+    }
61
+
62
+    public function testIndex_withTeam(): void {
63
+        $user = $this->createMock(IUser::class);
64
+        $user->method('getUID')
65
+            ->willReturn('current-user');
66
+
67
+        $entries = [
68
+            $this->createMock(IEntry::class),
69
+            $this->createMock(IEntry::class),
70
+        ];
71
+        $entries[0]->method('getProperty')
72
+            ->with('UID')
73
+            ->willReturn('member1');
74
+        $entries[0]->method('getProperty')
75
+            ->with('UID')
76
+            ->willReturn('member2');
77
+
78
+        $this->userSession->expects($this->atLeastOnce())
79
+            ->method('getUser')
80
+            ->willReturn($user);
81
+        $this->contactsManager->expects($this->once())
82
+            ->method('getEntries')
83
+            ->with($this->equalTo($user), $this->equalTo(null))
84
+            ->willReturn(['contacts' => $entries]);
85
+
86
+        $this->teamManager->expects($this->once())
87
+            ->method('getMembersOfTeam')
88
+            ->with('team-id', 'current-user')
89
+            ->willReturn(['member1', 'member3']);
90
+
91
+        $response = $this->controller->index(teamId: 'team-id');
92
+
93
+        $this->assertEquals([$entries[0]], $response['contacts']);
94
+    }
95
+
96
+    public function testFindOne(): void {
97
+        $user = $this->createMock(IUser::class);
98
+        $entry = $this->createMock(IEntry::class);
99
+        $this->userSession->expects($this->once())
100
+            ->method('getUser')
101
+            ->willReturn($user);
102
+        $this->contactsManager->expects($this->once())
103
+            ->method('findOne')
104
+            ->with($this->equalTo($user), $this->equalTo(42), $this->equalTo('test-search-phrase'))
105
+            ->willReturn($entry);
106
+
107
+        $response = $this->controller->findOne(42, 'test-search-phrase');
108
+
109
+        $this->assertEquals($entry, $response);
110
+    }
111
+
112
+    public function testFindOne404(): void {
113
+        $user = $this->createMock(IUser::class);
114
+        $this->userSession->expects($this->once())
115
+            ->method('getUser')
116
+            ->willReturn($user);
117
+        $this->contactsManager->expects($this->once())
118
+            ->method('findOne')
119
+            ->with($this->equalTo($user), $this->equalTo(42), $this->equalTo('test-search-phrase'))
120
+            ->willReturn(null);
121
+
122
+        $response = $this->controller->findOne(42, 'test-search-phrase');
123
+
124
+        $this->assertEquals([], $response->getData());
125
+        $this->assertEquals(404, $response->getStatus());
126
+    }
127 127
 }
Please login to merge, or discard this patch.
core/Controller/ContactsMenuController.php 1 patch
Indentation   +48 added lines, -48 removed lines patch added patch discarded remove patch
@@ -19,56 +19,56 @@
 block discarded – undo
19 19
 use OCP\Teams\ITeamManager;
20 20
 
21 21
 class ContactsMenuController extends Controller {
22
-	public function __construct(
23
-		IRequest $request,
24
-		private IUserSession $userSession,
25
-		private Manager $manager,
26
-		private ITeamManager $teamManager,
27
-	) {
28
-		parent::__construct('core', $request);
29
-	}
22
+    public function __construct(
23
+        IRequest $request,
24
+        private IUserSession $userSession,
25
+        private Manager $manager,
26
+        private ITeamManager $teamManager,
27
+    ) {
28
+        parent::__construct('core', $request);
29
+    }
30 30
 
31
-	/**
32
-	 * @return \JsonSerializable[]
33
-	 * @throws Exception
34
-	 */
35
-	#[NoAdminRequired]
36
-	#[FrontpageRoute(verb: 'POST', url: '/contactsmenu/contacts')]
37
-	public function index(?string $filter = null, ?string $teamId = null): array {
38
-		$entries = $this->manager->getEntries($this->userSession->getUser(), $filter);
39
-		if ($teamId !== null) {
40
-			/** @var \OC\Teams\TeamManager */
41
-			$teamManager = $this->teamManager;
42
-			$memberIds = $teamManager->getMembersOfTeam($teamId, $this->userSession->getUser()->getUID());
43
-			$entries['contacts'] = array_filter(
44
-				$entries['contacts'],
45
-				fn (IEntry $entry) => in_array($entry->getProperty('UID'), $memberIds, true)
46
-			);
47
-		}
48
-		return $entries;
49
-	}
31
+    /**
32
+     * @return \JsonSerializable[]
33
+     * @throws Exception
34
+     */
35
+    #[NoAdminRequired]
36
+    #[FrontpageRoute(verb: 'POST', url: '/contactsmenu/contacts')]
37
+    public function index(?string $filter = null, ?string $teamId = null): array {
38
+        $entries = $this->manager->getEntries($this->userSession->getUser(), $filter);
39
+        if ($teamId !== null) {
40
+            /** @var \OC\Teams\TeamManager */
41
+            $teamManager = $this->teamManager;
42
+            $memberIds = $teamManager->getMembersOfTeam($teamId, $this->userSession->getUser()->getUID());
43
+            $entries['contacts'] = array_filter(
44
+                $entries['contacts'],
45
+                fn (IEntry $entry) => in_array($entry->getProperty('UID'), $memberIds, true)
46
+            );
47
+        }
48
+        return $entries;
49
+    }
50 50
 
51
-	/**
52
-	 * @return JSONResponse|\JsonSerializable
53
-	 * @throws Exception
54
-	 */
55
-	#[NoAdminRequired]
56
-	#[FrontpageRoute(verb: 'POST', url: '/contactsmenu/findOne')]
57
-	public function findOne(int $shareType, string $shareWith) {
58
-		$contact = $this->manager->findOne($this->userSession->getUser(), $shareType, $shareWith);
51
+    /**
52
+     * @return JSONResponse|\JsonSerializable
53
+     * @throws Exception
54
+     */
55
+    #[NoAdminRequired]
56
+    #[FrontpageRoute(verb: 'POST', url: '/contactsmenu/findOne')]
57
+    public function findOne(int $shareType, string $shareWith) {
58
+        $contact = $this->manager->findOne($this->userSession->getUser(), $shareType, $shareWith);
59 59
 
60
-		if ($contact) {
61
-			return $contact;
62
-		}
63
-		return new JSONResponse([], Http::STATUS_NOT_FOUND);
64
-	}
60
+        if ($contact) {
61
+            return $contact;
62
+        }
63
+        return new JSONResponse([], Http::STATUS_NOT_FOUND);
64
+    }
65 65
 
66
-	/**
67
-	 * @return \JsonSerializable[]
68
-	 */
69
-	#[NoAdminRequired]
70
-	#[FrontpageRoute(verb: 'GET', url: '/contactsmenu/teams')]
71
-	public function getTeams(): array {
72
-		return $this->teamManager->getTeamsForUser($this->userSession->getUser()->getUID());
73
-	}
66
+    /**
67
+     * @return \JsonSerializable[]
68
+     */
69
+    #[NoAdminRequired]
70
+    #[FrontpageRoute(verb: 'GET', url: '/contactsmenu/teams')]
71
+    public function getTeams(): array {
72
+        return $this->teamManager->getTeamsForUser($this->userSession->getUser()->getUID());
73
+    }
74 74
 }
Please login to merge, or discard this patch.