feat: add validators to check values in services

Signed-off-by: Luka Trovic <luka@nextcloud.com>
This commit is contained in:
Luka Trovic
2022-09-13 19:14:59 +02:00
committed by backportbot-nextcloud[bot]
parent f250d9956b
commit 9d09916c17
22 changed files with 560 additions and 283 deletions

View File

@@ -36,6 +36,7 @@ use OCA\Deck\NoPermissionException;
use OCA\Deck\NotFoundException;
use OCA\Deck\Cache\AttachmentCacheHelper;
use OCA\Deck\StatusException;
use OCA\Deck\Validators\AttachmentServiceValidator;
use OCP\AppFramework\Db\IMapperException;
use OCP\AppFramework\Http\Response;
use OCP\IL10N;
@@ -60,17 +61,22 @@ class AttachmentService {
/** @var ChangeHelper */
private $changeHelper;
private IUserManager $userManager;
/** @var AttachmentServiceValidator */
private AttachmentServiceValidator $attachmentServiceValidator;
public function __construct(AttachmentMapper $attachmentMapper,
CardMapper $cardMapper,
IUserManager $userManager,
ChangeHelper $changeHelper,
PermissionService $permissionService,
Application $application,
AttachmentCacheHelper $attachmentCacheHelper,
$userId,
IL10N $l10n,
ActivityManager $activityManager) {
public function __construct(
AttachmentMapper $attachmentMapper,
CardMapper $cardMapper,
IUserManager $userManager,
ChangeHelper $changeHelper,
PermissionService $permissionService,
Application $application,
AttachmentCacheHelper $attachmentCacheHelper,
$userId,
IL10N $l10n,
ActivityManager $activityManager,
AttachmentServiceValidator $attachmentServiceValidator
) {
$this->attachmentMapper = $attachmentMapper;
$this->cardMapper = $cardMapper;
$this->permissionService = $permissionService;
@@ -81,6 +87,7 @@ class AttachmentService {
$this->activityManager = $activityManager;
$this->changeHelper = $changeHelper;
$this->userManager = $userManager;
$this->attachmentServiceValidator = $attachmentServiceValidator;
// Register shipped attachment services
// TODO: move this to a plugin based approach once we have different types of attachments
@@ -187,17 +194,7 @@ class AttachmentService {
* @throws BadRequestException
*/
public function create($cardId, $type, $data) {
if (is_numeric($cardId) === false) {
throw new BadRequestException('card id must be a number');
}
if ($type === false || $type === null) {
throw new BadRequestException('type must be provided');
}
if ($data === false || $data === null) {
//throw new BadRequestException('data must be provided');
}
$this->attachmentServiceValidator->check(compact('cardId', 'type', 'data'));
$this->permissionService->checkPermission($this->cardMapper, $cardId, Acl::PERMISSION_EDIT);
@@ -283,6 +280,8 @@ class AttachmentService {
* @throws NoPermissionException
*/
public function update($cardId, $attachmentId, $data, $type = 'deck_file') {
$this->attachmentServiceValidator->check(compact('cardId', 'type', 'data'));
try {
$service = $this->getService($type);
} catch (InvalidAttachmentType $e) {
@@ -304,9 +303,6 @@ class AttachmentService {
}
}
if ($data === false || $data === null) {
//throw new BadRequestException('data must be provided');
}
try {
$attachment = $this->attachmentMapper->find($attachmentId);
} catch (\Exception $e) {