0
0
Fork 0
mirror of https://github.com/nextcloud/server.git synced 2025-05-11 17:16:10 +00:00
nextcloud_server/apps/dav/lib/Controller/OutOfOfficeController.php
Christoph Wurst 89b6ba6c4d
fix(dav): Handle long absence status earlier
Validate the request early. Don't let this cause a database error.

Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
2025-03-05 13:23:57 +01:00

189 lines
5.9 KiB
PHP

<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2023 Richard Steinmetz <richard@steinmetz.cloud>
*
* @author Richard Steinmetz <richard@steinmetz.cloud>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\DAV\Controller;
use DateTimeImmutable;
use OCA\DAV\ResponseDefinitions;
use OCA\DAV\Service\AbsenceService;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCSController;
use OCP\IRequest;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\User\IAvailabilityCoordinator;
use function mb_strlen;
/**
* @psalm-import-type DAVOutOfOfficeData from ResponseDefinitions
* @psalm-import-type DAVCurrentOutOfOfficeData from ResponseDefinitions
*/
class OutOfOfficeController extends OCSController {
public function __construct(
string $appName,
IRequest $request,
private IUserManager $userManager,
private ?IUserSession $userSession,
private AbsenceService $absenceService,
private IAvailabilityCoordinator $coordinator,
) {
parent::__construct($appName, $request);
}
/**
* Get the currently configured out-of-office data of a user.
*
* @param string $userId The user id to get out-of-office data for.
* @return DataResponse<Http::STATUS_OK, DAVCurrentOutOfOfficeData, array{}>|DataResponse<Http::STATUS_NOT_FOUND, null, array{}>
*
* 200: Out-of-office data
* 404: No out-of-office data was found
*/
#[NoAdminRequired]
public function getCurrentOutOfOfficeData(string $userId): DataResponse {
$user = $this->userManager->get($userId);
if ($user === null) {
return new DataResponse(null, Http::STATUS_NOT_FOUND);
}
try {
$data = $this->absenceService->getCurrentAbsence($user);
if ($data === null) {
return new DataResponse(null, Http::STATUS_NOT_FOUND);
}
} catch (DoesNotExistException) {
return new DataResponse(null, Http::STATUS_NOT_FOUND);
}
return new DataResponse($data->jsonSerialize());
}
/**
* Get the configured out-of-office data of a user.
*
* @param string $userId The user id to get out-of-office data for.
* @return DataResponse<Http::STATUS_OK, DAVOutOfOfficeData, array{}>|DataResponse<Http::STATUS_NOT_FOUND, null, array{}>
*
* 200: Out-of-office data
* 404: No out-of-office data was found
*/
#[NoAdminRequired]
public function getOutOfOffice(string $userId): DataResponse {
try {
$data = $this->absenceService->getAbsence($userId);
if ($data === null) {
return new DataResponse(null, Http::STATUS_NOT_FOUND);
}
} catch (DoesNotExistException) {
return new DataResponse(null, Http::STATUS_NOT_FOUND);
}
return new DataResponse([
'id' => $data->getId(),
'userId' => $data->getUserId(),
'firstDay' => $data->getFirstDay(),
'lastDay' => $data->getLastDay(),
'status' => $data->getStatus(),
'message' => $data->getMessage(),
]);
}
/**
* Set out-of-office absence
*
* @param string $firstDay First day of the absence in format `YYYY-MM-DD`
* @param string $lastDay Last day of the absence in format `YYYY-MM-DD`
* @param string $status Short text that is set as user status during the absence
* @param string $message Longer multiline message that is shown to others during the absence
* @return DataResponse<Http::STATUS_OK, DAVOutOfOfficeData, array{}>|DataResponse<Http::STATUS_BAD_REQUEST, array{error: 'firstDay'|'statusLength'}, array{}>|DataResponse<Http::STATUS_UNAUTHORIZED, null, array{}>
*
* 200: Absence data
* 400: When validation fails, e.g. data range error or the first day is not before the last day
* 401: When the user is not logged in
*/
#[NoAdminRequired]
public function setOutOfOffice(
string $firstDay,
string $lastDay,
string $status,
string $message,
): DataResponse {
$user = $this->userSession?->getUser();
if ($user === null) {
return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
}
if (mb_strlen($status) > 100) {
return new DataResponse(['error' => 'statusLength'], Http::STATUS_BAD_REQUEST);
}
$parsedFirstDay = new DateTimeImmutable($firstDay);
$parsedLastDay = new DateTimeImmutable($lastDay);
if ($parsedFirstDay->getTimestamp() > $parsedLastDay->getTimestamp()) {
return new DataResponse(['error' => 'firstDay'], Http::STATUS_BAD_REQUEST);
}
$data = $this->absenceService->createOrUpdateAbsence(
$user,
$firstDay,
$lastDay,
$status,
$message,
);
$this->coordinator->clearCache($user->getUID());
return new DataResponse([
'id' => $data->getId(),
'userId' => $data->getUserId(),
'firstDay' => $data->getFirstDay(),
'lastDay' => $data->getLastDay(),
'status' => $data->getStatus(),
'message' => $data->getMessage(),
]);
}
/**
* Clear the out-of-office
*
* @return DataResponse<Http::STATUS_OK|Http::STATUS_UNAUTHORIZED, null, array{}>
*
* 200: When the absence was cleared successfully
* 401: When the user is not logged in
*/
#[NoAdminRequired]
public function clearOutOfOffice(): DataResponse {
$user = $this->userSession?->getUser();
if ($user === null) {
return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
}
$this->absenceService->clearAbsence($user);
$this->coordinator->clearCache($user->getUID());
return new DataResponse(null);
}
}