Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/Capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function getCapabilities(): array {
'admin-notifications' => [
'ocs',
'cli',
'self',
],
],
];
Expand Down
9 changes: 9 additions & 0 deletions lib/Controller/APIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUser;
Expand Down Expand Up @@ -47,6 +48,7 @@ public function __construct(
protected IValidator $richValidator,
protected IL10N $l,
protected LoggerInterface $logger,
protected IGroupManager $groupManager,
) {
parent::__construct($appName, $request);
}
Expand Down Expand Up @@ -96,6 +98,7 @@ public function generateNotification(string $userId, string $shortMessage, strin
* 200: Notification generated successfully, returned id is the notification ID for future delete requests
* 400: Provided data was invalid, check error field of the response of log file for details
*/
#[NoAdminRequired]
#[OpenAPI(scope: OpenAPI::SCOPE_ADMINISTRATION)]
#[ApiRoute(verb: 'POST', url: '/api/{apiVersion3}/admin_notifications/{userId}', requirements: ['apiVersion3' => '(v3)'])]
public function generateNotificationV3(
Expand All @@ -105,6 +108,12 @@ public function generateNotificationV3(
array $subjectParameters = [],
array $messageParameters = [],
): DataResponse {
$actor = $this->userSession->getUser();
if (!$this->groupManager->isAdmin($actor?->getUID() ?? '')
&& $userId !== $actor?->getUID()) {
return new DataResponse(['error' => 'user'], Http::STATUS_BAD_REQUEST);
}

$user = $this->userManager->get($userId);

if (!$user instanceof IUser) {
Expand Down
29 changes: 0 additions & 29 deletions openapi-administration.json
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,6 @@
"post": {
"operationId": "api-generate-notification-v3",
"summary": "Generate a notification with rich object parameters for a user",
"description": "This endpoint requires admin access",
"tags": [
"api"
],
Expand Down Expand Up @@ -633,34 +632,6 @@
}
}
}
},
"403": {
"description": "Logged in account must be an admin",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {}
}
}
}
}
}
}
}
}
}
Expand Down
29 changes: 0 additions & 29 deletions openapi-full.json
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,6 @@
"post": {
"operationId": "api-generate-notification-v3",
"summary": "Generate a notification with rich object parameters for a user",
"description": "This endpoint requires admin access",
"tags": [
"api"
],
Expand Down Expand Up @@ -744,34 +743,6 @@
}
}
}
},
"403": {
"description": "Logged in account must be an admin",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {}
}
}
}
}
}
}
}
}
}
Expand Down
38 changes: 38 additions & 0 deletions tests/Integration/features/admin-notification-v3.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
# SPDX-License-Identifier: CC0-1.0
Feature: admin-notification-v3
Background:
Given user "test1" exists
Given user "test2" exists
Given as user "test1"

Scenario: Non-admin user creates a notification for themselves
When sending "POST" to "/apps/notifications/api/v3/admin_notifications/test1?format=json" with
| subject | self notification |
| message | this is the message |
Then status code is 200
And user "test1" has 1 notifications on v2
And last notification on v2 matches
| app | admin_notifications |
| subject | self notification |
| message | this is the message |
| object_type | admin_notifications |

Scenario: Non-admin user can not create a notification for another user
When sending "POST" to "/apps/notifications/api/v3/admin_notifications/test2?format=json" with
| subject | foreign notification |
Then status code is 400
Given as user "test2"
Then user "test2" has 0 notifications on v2

Scenario: Admin creates a notification for another user
Given as user "admin"
When sending "POST" to "/apps/notifications/api/v3/admin_notifications/test1?format=json" with
| subject | admin notification |
Then status code is 200
Given as user "test1"
Then user "test1" has 1 notifications on v2
And last notification on v2 matches
| app | admin_notifications |
| subject | admin notification |
| object_type | admin_notifications |
1 change: 1 addition & 0 deletions tests/Unit/CapabilitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public function testGetCapabilities(bool $webpush, bool $webpushBrowser, array $
'admin-notifications' => [
'ocs',
'cli',
'self',
],
],
], $capabilities->getCapabilities());
Expand Down
13 changes: 13 additions & 0 deletions tests/Unit/Controller/APIControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IUser;
Expand All @@ -38,6 +39,7 @@ class APIControllerTest extends TestCase {
protected IValidator&MockObject $richValidator;
protected IL10N&MockObject $l;
protected LoggerInterface&MockObject $logger;
protected IGroupManager&MockObject $groupManager;

protected APIController $controller;

Expand All @@ -54,6 +56,7 @@ protected function setUp(): void {
$this->richValidator = $this->createMock(IValidator::class);
$this->l = $this->createMock(IL10N::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->groupManager = $this->createMock(IGroupManager::class);

$this->controller = new APIController(
'notifications',
Expand All @@ -66,6 +69,7 @@ protected function setUp(): void {
$this->richValidator,
$this->l,
$this->logger,
$this->groupManager,
);
}

Expand Down Expand Up @@ -95,6 +99,9 @@ public function testGenerateNotification(string $userId, string $short, string $
->method('get')
->with($userId)
->willReturn($u);
$this->userSession->expects($this->any())
->method('getUser')
->willReturn($this->mockUser($userId));

$dateTime = new \DateTime();
$dateTime->setTimestamp($time);
Expand Down Expand Up @@ -156,4 +163,10 @@ public function testGenerateNotification(string $userId, string $short, string $
$this->assertInstanceOf(DataResponse::class, $response);
$this->assertSame($statusCode, $response->getStatus());
}

private function mockUser(string $uid): IUser&MockObject {
$user = $this->createMock(IUser::class);
$user->method('getUID')->willReturn($uid);
return $user;
}
}
Loading