Skip to content

Commit 23715fa

Browse files
stediefanCopilot
andcommitted
feat(dashboard): add team dashboard with ITeamWidget support
Add ITeamManager::isMemberOfTeam, reusing the member-only Circles probe, and authorize team membership before resolving team-capable widgets while preserving the personal dashboard paths. Isolate applicability, metadata and item failures per provider: log a warning and omit only the broken widget from the response. Move the team dashboard UI into the dashboard app as an embeddable `dashboard-team` widget. It shares widget loading, headings, picker tiles and item rendering with the personal dashboard. Assisted-by: Copilot:gpt-6.1-sol Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Stefan Dietrich <stefan.dietrich@dataport.de>
1 parent c6ce5a9 commit 23715fa

21 files changed

Lines changed: 1875 additions & 179 deletions

‎apps/dashboard/lib/Controller/DashboardApiController.php‎

Lines changed: 127 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
1717
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
1818
use OCP\AppFramework\Http\DataResponse;
19+
use OCP\AppFramework\OCS\OCSBadRequestException;
20+
use OCP\AppFramework\OCS\OCSNotFoundException;
1921
use OCP\AppFramework\OCSController;
2022
use OCP\AppFramework\Services\IAppConfig;
2123
use OCP\Config\IUserConfig;
@@ -26,11 +28,14 @@
2628
use OCP\Dashboard\IManager;
2729
use OCP\Dashboard\IOptionWidget;
2830
use OCP\Dashboard\IReloadableWidget;
31+
use OCP\Dashboard\ITeamWidget;
2932
use OCP\Dashboard\IWidget;
3033
use OCP\Dashboard\Model\WidgetButton;
3134
use OCP\Dashboard\Model\WidgetItem;
3235
use OCP\Dashboard\Model\WidgetOptions;
3336
use OCP\IRequest;
37+
use OCP\Teams\ITeamManager;
38+
use Psr\Log\LoggerInterface;
3439

3540
/**
3641
* @psalm-import-type DashboardWidget from ResponseDefinitions
@@ -47,6 +52,8 @@ public function __construct(
4752
private IUserConfig $userConfig,
4853
private ?string $userId,
4954
private DashboardService $service,
55+
private ITeamManager $teamManager,
56+
private LoggerInterface $logger,
5057
) {
5158
parent::__construct($appName, $request);
5259
}
@@ -69,6 +76,44 @@ static function (IWidget $widget) use ($widgetIds) {
6976
);
7077
}
7178

79+
private function validateTeamAccess(?string $teamId): ?string {
80+
if ($teamId === null) {
81+
return null;
82+
}
83+
if ($teamId === '') {
84+
throw new OCSBadRequestException('Empty team ID');
85+
}
86+
if ($this->userId === null || !$this->teamManager->isMemberOfTeam($teamId, $this->userId)) {
87+
throw new OCSNotFoundException('Team not found');
88+
}
89+
return $teamId;
90+
}
91+
92+
/**
93+
* @param list<string> $widgetIds
94+
* @return array<string, ITeamWidget>
95+
*/
96+
private function getTeamWidgets(string $teamId, array $widgetIds): array {
97+
$widgets = [];
98+
foreach ($this->dashboardManager->getWidgets() as $id => $widget) {
99+
if (!$widget instanceof ITeamWidget || ($widgetIds !== [] && !in_array($id, $widgetIds, true))) {
100+
continue;
101+
}
102+
try {
103+
if ($widget->isEnabledForTeam($teamId, $this->userId)) {
104+
$widgets[$id] = $widget;
105+
}
106+
} catch (\Throwable $e) {
107+
$this->logger->warning('Failed to check dashboard widget applicability for team', [
108+
'widget' => $id,
109+
'team' => $teamId,
110+
'exception' => $e,
111+
]);
112+
}
113+
}
114+
return $widgets;
115+
}
116+
72117
/**
73118
* Get the items for the widgets
74119
*
@@ -104,15 +149,34 @@ public function getWidgetItems(array $sinceIds = [], int $limit = 7, array $widg
104149
* @param int $limit Limit number of result items per widget, not more than 30 are allowed
105150
* @psalm-param int<1, 30> $limit
106151
* @param list<string> $widgets Limit results to specific widgets
152+
* @param ?string $teamId Team to show widgets for; omit for the personal dashboard
107153
* @return DataResponse<Http::STATUS_OK, array<string, DashboardWidgetItems>, array{}>
154+
* @throws OCSNotFoundException Team not found
155+
* @throws OCSBadRequestException Empty team ID
108156
*
109157
* 200: Widget items returned
110158
*/
111159
#[NoAdminRequired]
112160
#[NoCSRFRequired]
113161
#[ApiRoute(verb: 'GET', url: '/api/v2/widget-items')]
114-
public function getWidgetItemsV2(array $sinceIds = [], int $limit = 7, array $widgets = []): DataResponse {
162+
public function getWidgetItemsV2(array $sinceIds = [], int $limit = 7, array $widgets = [], ?string $teamId = null): DataResponse {
163+
$teamId = $this->validateTeamAccess($teamId);
115164
$items = [];
165+
if ($teamId !== null) {
166+
foreach ($this->getTeamWidgets($teamId, $widgets) as $id => $widget) {
167+
try {
168+
$items[$id] = $widget->getTeamItems($teamId, $this->userId, $sinceIds[$id] ?? null, $limit)->jsonSerialize();
169+
} catch (\Throwable $e) {
170+
$this->logger->warning('Failed to get dashboard widget items for team', [
171+
'widget' => $id,
172+
'team' => $teamId,
173+
'exception' => $e,
174+
]);
175+
}
176+
}
177+
return new DataResponse($items);
178+
}
179+
116180
$widgets = $this->getShownWidgets($widgets);
117181
foreach ($widgets as $widget) {
118182
if ($widget instanceof IAPIWidgetV2) {
@@ -128,55 +192,80 @@ public function getWidgetItemsV2(array $sinceIds = [], int $limit = 7, array $wi
128192
/**
129193
* Get the widgets
130194
*
195+
* @param ?string $teamId Team to show widgets for; omit for the personal dashboard
131196
* @return DataResponse<Http::STATUS_OK, array<string, DashboardWidget>, array{}>
197+
* @throws OCSNotFoundException Team not found
198+
* @throws OCSBadRequestException Empty team ID
132199
*
133200
* 200: Widgets returned
134201
*/
135202
#[NoAdminRequired]
136203
#[NoCSRFRequired]
137204
#[ApiRoute(verb: 'GET', url: '/api/v1/widgets')]
138-
public function getWidgets(): DataResponse {
139-
$widgets = $this->dashboardManager->getWidgets();
140-
141-
$items = array_map(function (IWidget $widget) {
142-
$options = ($widget instanceof IOptionWidget) ? $widget->getWidgetOptions() : WidgetOptions::getDefault();
143-
$data = [
144-
'id' => $widget->getId(),
145-
'title' => $widget->getTitle(),
146-
'order' => $widget->getOrder(),
147-
'icon_class' => $widget->getIconClass(),
148-
'icon_url' => ($widget instanceof IIconWidget) ? $widget->getIconUrl() : '',
149-
'widget_url' => $widget->getUrl(),
150-
'item_icons_round' => $options->withRoundItemIcons(),
151-
'item_api_versions' => [],
152-
'reload_interval' => 0,
153-
];
154-
if ($widget instanceof IButtonWidget) {
155-
$data += [
156-
'buttons' => array_map(function (WidgetButton $button) {
157-
return [
158-
'type' => $button->getType(),
159-
'text' => $button->getText(),
160-
'link' => $button->getLink(),
161-
];
162-
}, $widget->getWidgetButtons($this->userId)),
163-
];
205+
public function getWidgets(?string $teamId = null): DataResponse {
206+
$teamId = $this->validateTeamAccess($teamId);
207+
if ($teamId !== null) {
208+
$items = [];
209+
foreach ($this->getTeamWidgets($teamId, []) as $id => $widget) {
210+
try {
211+
$items[$id] = $this->serializeWidget($widget, $widget->getTeamUrl($teamId, $this->userId) ?? $widget->getUrl());
212+
} catch (\Throwable $e) {
213+
$this->logger->warning('Failed to get dashboard widget metadata for team', [
214+
'widget' => $id,
215+
'team' => $teamId,
216+
'exception' => $e,
217+
]);
218+
}
164219
}
165-
if ($widget instanceof IReloadableWidget) {
166-
$data['reload_interval'] = $widget->getReloadInterval();
167-
}
168-
if ($widget instanceof IAPIWidget) {
169-
$data['item_api_versions'][] = 1;
170-
}
171-
if ($widget instanceof IAPIWidgetV2) {
172-
$data['item_api_versions'][] = 2;
173-
}
174-
return $data;
175-
}, $widgets);
220+
} else {
221+
$items = array_map(
222+
fn (IWidget $widget) => $this->serializeWidget($widget, $widget->getUrl()),
223+
$this->dashboardManager->getWidgets(),
224+
);
225+
}
176226

177227
return new DataResponse($items);
178228
}
179229

230+
/**
231+
* @return DashboardWidget
232+
*/
233+
private function serializeWidget(IWidget $widget, ?string $url): array {
234+
$options = ($widget instanceof IOptionWidget) ? $widget->getWidgetOptions() : WidgetOptions::getDefault();
235+
$data = [
236+
'id' => $widget->getId(),
237+
'title' => $widget->getTitle(),
238+
'order' => $widget->getOrder(),
239+
'icon_class' => $widget->getIconClass(),
240+
'icon_url' => ($widget instanceof IIconWidget) ? $widget->getIconUrl() : '',
241+
'widget_url' => $url,
242+
'item_icons_round' => $options->withRoundItemIcons(),
243+
'item_api_versions' => [],
244+
'reload_interval' => 0,
245+
];
246+
if ($widget instanceof IButtonWidget) {
247+
$data += [
248+
'buttons' => array_map(function (WidgetButton $button) {
249+
return [
250+
'type' => $button->getType(),
251+
'text' => $button->getText(),
252+
'link' => $button->getLink(),
253+
];
254+
}, $widget->getWidgetButtons($this->userId)),
255+
];
256+
}
257+
if ($widget instanceof IReloadableWidget) {
258+
$data['reload_interval'] = $widget->getReloadInterval();
259+
}
260+
if ($widget instanceof IAPIWidget) {
261+
$data['item_api_versions'][] = 1;
262+
}
263+
if ($widget instanceof IAPIWidgetV2) {
264+
$data['item_api_versions'][] = 2;
265+
}
266+
return $data;
267+
}
268+
180269
/**
181270
* Get the layout
182271
*

‎apps/dashboard/openapi.json‎

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,16 @@
347347
}
348348
}
349349
},
350+
{
351+
"name": "teamId",
352+
"in": "query",
353+
"description": "Team to show widgets for; omit for the personal dashboard",
354+
"schema": {
355+
"type": "string",
356+
"nullable": true,
357+
"default": null
358+
}
359+
},
350360
{
351361
"name": "OCS-APIRequest",
352362
"in": "header",
@@ -392,6 +402,62 @@
392402
}
393403
}
394404
},
405+
"404": {
406+
"description": "Team not found",
407+
"content": {
408+
"application/json": {
409+
"schema": {
410+
"type": "object",
411+
"required": [
412+
"ocs"
413+
],
414+
"properties": {
415+
"ocs": {
416+
"type": "object",
417+
"required": [
418+
"meta",
419+
"data"
420+
],
421+
"properties": {
422+
"meta": {
423+
"$ref": "#/components/schemas/OCSMeta"
424+
},
425+
"data": {}
426+
}
427+
}
428+
}
429+
}
430+
}
431+
}
432+
},
433+
"400": {
434+
"description": "Empty team ID",
435+
"content": {
436+
"application/json": {
437+
"schema": {
438+
"type": "object",
439+
"required": [
440+
"ocs"
441+
],
442+
"properties": {
443+
"ocs": {
444+
"type": "object",
445+
"required": [
446+
"meta",
447+
"data"
448+
],
449+
"properties": {
450+
"meta": {
451+
"$ref": "#/components/schemas/OCSMeta"
452+
},
453+
"data": {}
454+
}
455+
}
456+
}
457+
}
458+
}
459+
}
460+
},
395461
"401": {
396462
"description": "Current user is not logged in",
397463
"content": {
@@ -439,6 +505,16 @@
439505
}
440506
],
441507
"parameters": [
508+
{
509+
"name": "teamId",
510+
"in": "query",
511+
"description": "Team to show widgets for; omit for the personal dashboard",
512+
"schema": {
513+
"type": "string",
514+
"nullable": true,
515+
"default": null
516+
}
517+
},
442518
{
443519
"name": "OCS-APIRequest",
444520
"in": "header",
@@ -484,6 +560,62 @@
484560
}
485561
}
486562
},
563+
"404": {
564+
"description": "Team not found",
565+
"content": {
566+
"application/json": {
567+
"schema": {
568+
"type": "object",
569+
"required": [
570+
"ocs"
571+
],
572+
"properties": {
573+
"ocs": {
574+
"type": "object",
575+
"required": [
576+
"meta",
577+
"data"
578+
],
579+
"properties": {
580+
"meta": {
581+
"$ref": "#/components/schemas/OCSMeta"
582+
},
583+
"data": {}
584+
}
585+
}
586+
}
587+
}
588+
}
589+
}
590+
},
591+
"400": {
592+
"description": "Empty team ID",
593+
"content": {
594+
"application/json": {
595+
"schema": {
596+
"type": "object",
597+
"required": [
598+
"ocs"
599+
],
600+
"properties": {
601+
"ocs": {
602+
"type": "object",
603+
"required": [
604+
"meta",
605+
"data"
606+
],
607+
"properties": {
608+
"meta": {
609+
"$ref": "#/components/schemas/OCSMeta"
610+
},
611+
"data": {}
612+
}
613+
}
614+
}
615+
}
616+
}
617+
}
618+
},
487619
"401": {
488620
"description": "Current user is not logged in",
489621
"content": {

0 commit comments

Comments
 (0)