diff options
author | Joas Schilling <coding@schilljs.com> | 2024-04-17 13:08:06 +0200 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2024-04-17 15:36:40 +0200 |
commit | 661143a1d15e56b8c7146b845a2f74c03365fe78 (patch) | |
tree | 1a7bec80013d2ea22d6bc54c341bedd43649f0b5 | |
parent | f325e404d6b866b44fd2969a8533d35a89cd6934 (diff) | |
download | nextcloud-server-661143a1d15e56b8c7146b845a2f74c03365fe78.tar.gz nextcloud-server-661143a1d15e56b8c7146b845a2f74c03365fe78.zip |
fix(activity): Add a dedicated exception when invalid values are set
Signed-off-by: Joas Schilling <coding@schilljs.com>
-rw-r--r-- | lib/composer/composer/autoload_classmap.php | 1 | ||||
-rw-r--r-- | lib/composer/composer/autoload_static.php | 2 | ||||
-rw-r--r-- | lib/private/Activity/Event.php | 134 | ||||
-rw-r--r-- | lib/public/Activity/Exceptions/InvalidValueException.php | 49 | ||||
-rw-r--r-- | lib/public/Activity/IEvent.php | 50 |
5 files changed, 125 insertions, 111 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 02d9f994503..8d6412c724f 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -14,6 +14,7 @@ return array( 'OCP\\Accounts\\PropertyDoesNotExistException' => $baseDir . '/lib/public/Accounts/PropertyDoesNotExistException.php', 'OCP\\Accounts\\UserUpdatedEvent' => $baseDir . '/lib/public/Accounts/UserUpdatedEvent.php', 'OCP\\Activity\\ActivitySettings' => $baseDir . '/lib/public/Activity/ActivitySettings.php', + 'OCP\\Activity\\Exceptions\\InvalidValueException' => $baseDir . '/lib/public/Activity/Exceptions/InvalidValueException.php', 'OCP\\Activity\\IConsumer' => $baseDir . '/lib/public/Activity/IConsumer.php', 'OCP\\Activity\\IEvent' => $baseDir . '/lib/public/Activity/IEvent.php', 'OCP\\Activity\\IEventMerger' => $baseDir . '/lib/public/Activity/IEventMerger.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 1f29b6cb3b6..31583ef51a3 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -47,6 +47,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\Accounts\\PropertyDoesNotExistException' => __DIR__ . '/../../..' . '/lib/public/Accounts/PropertyDoesNotExistException.php', 'OCP\\Accounts\\UserUpdatedEvent' => __DIR__ . '/../../..' . '/lib/public/Accounts/UserUpdatedEvent.php', 'OCP\\Activity\\ActivitySettings' => __DIR__ . '/../../..' . '/lib/public/Activity/ActivitySettings.php', + 'OCP\\Activity\\Exceptions\\InvalidValueException' => __DIR__ . '/../../..' . '/lib/public/Activity/Exceptions/InvalidValueException.php', 'OCP\\Activity\\IConsumer' => __DIR__ . '/../../..' . '/lib/public/Activity/IConsumer.php', 'OCP\\Activity\\IEvent' => __DIR__ . '/../../..' . '/lib/public/Activity/IEvent.php', 'OCP\\Activity\\IEventMerger' => __DIR__ . '/../../..' . '/lib/public/Activity/IEventMerger.php', @@ -1171,6 +1172,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OC\\Core\\Command\\User\\AuthTokens\\Add' => __DIR__ . '/../../..' . '/core/Command/User/AuthTokens/Add.php', 'OC\\Core\\Command\\User\\AuthTokens\\Delete' => __DIR__ . '/../../..' . '/core/Command/User/AuthTokens/Delete.php', 'OC\\Core\\Command\\User\\AuthTokens\\ListCommand' => __DIR__ . '/../../..' . '/core/Command/User/AuthTokens/ListCommand.php', + 'OC\\Core\\Command\\User\\ClearGeneratedAvatarCacheCommand' => __DIR__ . '/../../..' . '/core/Command/User/ClearGeneratedAvatarCacheCommand.php', 'OC\\Core\\Command\\User\\Delete' => __DIR__ . '/../../..' . '/core/Command/User/Delete.php', 'OC\\Core\\Command\\User\\Disable' => __DIR__ . '/../../..' . '/core/Command/User/Disable.php', 'OC\\Core\\Command\\User\\Enable' => __DIR__ . '/../../..' . '/core/Command/User/Enable.php', diff --git a/lib/private/Activity/Event.php b/lib/private/Activity/Event.php index cc1135cf179..cfcfc26945e 100644 --- a/lib/private/Activity/Event.php +++ b/lib/private/Activity/Event.php @@ -28,6 +28,7 @@ declare(strict_types=1); */ namespace OC\Activity; +use OCP\Activity\Exceptions\InvalidValueException; use OCP\Activity\IEvent; use OCP\RichObjectStrings\InvalidObjectExeption; use OCP\RichObjectStrings\IValidator; @@ -89,16 +90,11 @@ class Event implements IEvent { } /** - * Set the app of the activity - * - * @param string $app - * @return IEvent - * @throws \InvalidArgumentException if the app id is invalid - * @since 8.2.0 + * {@inheritDoc} */ public function setApp(string $app): IEvent { if ($app === '' || isset($app[32])) { - throw new \InvalidArgumentException('The given app is invalid'); + throw new InvalidValueException('app'); } $this->app = $app; return $this; @@ -112,16 +108,11 @@ class Event implements IEvent { } /** - * Set the type of the activity - * - * @param string $type - * @return IEvent - * @throws \InvalidArgumentException if the type is invalid - * @since 8.2.0 + * {@inheritDoc} */ public function setType(string $type): IEvent { if ($type === '' || isset($type[255])) { - throw new \InvalidArgumentException('The given type is invalid'); + throw new InvalidValueException('type'); } $this->type = $type; return $this; @@ -135,16 +126,11 @@ class Event implements IEvent { } /** - * Set the affected user of the activity - * - * @param string $affectedUser - * @return IEvent - * @throws \InvalidArgumentException if the affected user is invalid - * @since 8.2.0 + * {@inheritDoc} */ public function setAffectedUser(string $affectedUser): IEvent { if ($affectedUser === '' || isset($affectedUser[64])) { - throw new \InvalidArgumentException('The given affected user is invalid'); + throw new InvalidValueException('affectedUser'); } $this->affectedUser = $affectedUser; return $this; @@ -158,16 +144,11 @@ class Event implements IEvent { } /** - * Set the author of the activity - * - * @param string $author - * @return IEvent - * @throws \InvalidArgumentException if the author is invalid - * @since 8.2.0 + * {@inheritDoc} */ public function setAuthor(string $author): IEvent { if (isset($author[64])) { - throw new \InvalidArgumentException('The given author user is invalid'); + throw new InvalidValueException('author'); } $this->author = $author; return $this; @@ -181,14 +162,12 @@ class Event implements IEvent { } /** - * Set the timestamp of the activity - * - * @param int $timestamp - * @return IEvent - * @throws \InvalidArgumentException if the timestamp is invalid - * @since 8.2.0 + * {@inheritDoc} */ public function setTimestamp(int $timestamp): IEvent { + if ($timestamp < 0) { + throw new InvalidValueException('timestamp'); + } $this->timestamp = $timestamp; return $this; } @@ -201,17 +180,11 @@ class Event implements IEvent { } /** - * Set the subject of the activity - * - * @param string $subject - * @param array $parameters - * @return IEvent - * @throws \InvalidArgumentException if the subject or parameters are invalid - * @since 8.2.0 + * {@inheritDoc} */ public function setSubject(string $subject, array $parameters = []): IEvent { if (isset($subject[255])) { - throw new \InvalidArgumentException('The given subject is invalid'); + throw new InvalidValueException('subject'); } $this->subject = $subject; $this->subjectParameters = $parameters; @@ -233,14 +206,11 @@ class Event implements IEvent { } /** - * @param string $subject - * @return $this - * @throws \InvalidArgumentException if the subject is invalid - * @since 11.0.0 + * {@inheritDoc} */ public function setParsedSubject(string $subject): IEvent { if ($subject === '') { - throw new \InvalidArgumentException('The given parsed subject is invalid'); + throw new InvalidValueException('parsedSubject'); } $this->subjectParsed = $subject; return $this; @@ -255,21 +225,21 @@ class Event implements IEvent { } /** - * @param string $subject - * @param array $parameters - * @return $this - * @throws \InvalidArgumentException if the subject or parameters are invalid - * @since 11.0.0 + * {@inheritDoc} */ public function setRichSubject(string $subject, array $parameters = []): IEvent { if ($subject === '') { - throw new \InvalidArgumentException('The given parsed subject is invalid'); + throw new InvalidValueException('richSubject'); } $this->subjectRich = $subject; $this->subjectRichParameters = $parameters; if ($this->subjectParsed === '') { - $this->subjectParsed = $this->richToParsed($subject, $parameters); + try { + $this->subjectParsed = $this->richToParsed($subject, $parameters); + } catch (\InvalidArgumentException $e) { + throw new InvalidValueException('richSubjectParameters', $e); + } } return $this; @@ -316,17 +286,11 @@ class Event implements IEvent { } /** - * Set the message of the activity - * - * @param string $message - * @param array $parameters - * @return IEvent - * @throws \InvalidArgumentException if the message or parameters are invalid - * @since 8.2.0 + * {@inheritDoc} */ public function setMessage(string $message, array $parameters = []): IEvent { if (isset($message[255])) { - throw new \InvalidArgumentException('The given message is invalid'); + throw new InvalidValueException('message'); } $this->message = $message; $this->messageParameters = $parameters; @@ -348,10 +312,7 @@ class Event implements IEvent { } /** - * @param string $message - * @return $this - * @throws \InvalidArgumentException if the message is invalid - * @since 11.0.0 + * {@inheritDoc} */ public function setParsedMessage(string $message): IEvent { $this->messageParsed = $message; @@ -367,18 +328,18 @@ class Event implements IEvent { } /** - * @param string $message - * @param array $parameters - * @return $this - * @throws \InvalidArgumentException if the subject or parameters are invalid - * @since 11.0.0 + * {@inheritDoc} */ public function setRichMessage(string $message, array $parameters = []): IEvent { $this->messageRich = $message; $this->messageRichParameters = $parameters; if ($this->messageParsed === '') { - $this->messageParsed = $this->richToParsed($message, $parameters); + try { + $this->messageParsed = $this->richToParsed($message, $parameters); + } catch (\InvalidArgumentException $e) { + throw new InvalidValueException('richMessageParameters', $e); + } } return $this; @@ -401,21 +362,14 @@ class Event implements IEvent { } /** - * Set the object of the activity - * - * @param string $objectType - * @param int $objectId - * @param string $objectName - * @return IEvent - * @throws \InvalidArgumentException if the object is invalid - * @since 8.2.0 + * {@inheritDoc} */ public function setObject(string $objectType, int $objectId, string $objectName = ''): IEvent { if (isset($objectType[255])) { - throw new \InvalidArgumentException('The given object type is invalid'); + throw new InvalidValueException('objectType'); } if (isset($objectName[4000])) { - throw new \InvalidArgumentException('The given object name is invalid'); + throw new InvalidValueException('objectName'); } $this->objectType = $objectType; $this->objectId = $objectId; @@ -445,16 +399,11 @@ class Event implements IEvent { } /** - * Set the link of the activity - * - * @param string $link - * @return IEvent - * @throws \InvalidArgumentException if the link is invalid - * @since 8.2.0 + * {@inheritDoc} */ public function setLink(string $link): IEvent { if (isset($link[4000])) { - throw new \InvalidArgumentException('The given link is invalid'); + throw new InvalidValueException('link'); } $this->link = $link; return $this; @@ -468,14 +417,11 @@ class Event implements IEvent { } /** - * @param string $icon - * @return $this - * @throws \InvalidArgumentException if the icon is invalid - * @since 11.0.0 + * {@inheritDoc} */ public function setIcon(string $icon): IEvent { if (isset($icon[4000])) { - throw new \InvalidArgumentException('The given icon is invalid'); + throw new InvalidValueException('icon'); } $this->icon = $icon; return $this; diff --git a/lib/public/Activity/Exceptions/InvalidValueException.php b/lib/public/Activity/Exceptions/InvalidValueException.php new file mode 100644 index 00000000000..24d0d12bb12 --- /dev/null +++ b/lib/public/Activity/Exceptions/InvalidValueException.php @@ -0,0 +1,49 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright Copyright (c) 2024 Joas Schilling <coding@schilljs.com> + * + * @author Joas Schilling <coding@schilljs.com> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCP\Activity\Exceptions; + +/** + * @since 30.0.0 + */ +class InvalidValueException extends \InvalidArgumentException { + /** + * @since 30.0.0 + */ + public function __construct( + protected string $field, + ?\Throwable $previous = null, + ) { + parent::__construct('Value provided for ' . $field . ' is not valid', previous: $previous); + } + + /** + * @since 30.0.0 + */ + public function getFieldIdentifier(): string { + return $this->field; + } +} diff --git a/lib/public/Activity/IEvent.php b/lib/public/Activity/IEvent.php index 4c47cc47a3f..5ca8ce15a21 100644 --- a/lib/public/Activity/IEvent.php +++ b/lib/public/Activity/IEvent.php @@ -30,6 +30,8 @@ declare(strict_types=1); namespace OCP\Activity; +use OCP\Activity\Exceptions\InvalidValueException; + /** * Interface IEvent * @@ -41,8 +43,9 @@ interface IEvent { * * @param string $app * @return IEvent - * @throws \InvalidArgumentException if the app id is invalid + * @throws InvalidValueException if the app id is invalid * @since 8.2.0 + * @since 30.0.0 throws {@see InvalidValueException} instead of \InvalidArgumentException */ public function setApp(string $app): self; @@ -51,28 +54,31 @@ interface IEvent { * * @param string $type * @return IEvent - * @throws \InvalidArgumentException if the type is invalid + * @throws InvalidValueException if the type is invalid * @since 8.2.0 + * @since 30.0.0 throws {@see InvalidValueException} instead of \InvalidArgumentException */ public function setType(string $type): self; /** * Set the affected user of the activity * - * @param string $user + * @param string $affectedUser * @return IEvent - * @throws \InvalidArgumentException if the affected user is invalid + * @throws InvalidValueException if the affected user is invalid * @since 8.2.0 + * @since 30.0.0 throws {@see InvalidValueException} instead of \InvalidArgumentException */ - public function setAffectedUser(string $user): self; + public function setAffectedUser(string $affectedUser): self; /** * Set the author of the activity * * @param string $author * @return IEvent - * @throws \InvalidArgumentException if the author is invalid + * @throws InvalidValueException if the author is invalid * @since 8.2.0 + * @since 30.0.0 throws {@see InvalidValueException} instead of \InvalidArgumentException */ public function setAuthor(string $author): self; @@ -81,8 +87,9 @@ interface IEvent { * * @param int $timestamp * @return IEvent - * @throws \InvalidArgumentException if the timestamp is invalid + * @throws InvalidValueException if the timestamp is invalid * @since 8.2.0 + * @since 30.0.0 throws {@see InvalidValueException} instead of \InvalidArgumentException */ public function setTimestamp(int $timestamp): self; @@ -92,8 +99,9 @@ interface IEvent { * @param string $subject * @param array $parameters * @return IEvent - * @throws \InvalidArgumentException if the subject or parameters are invalid + * @throws InvalidValueException if the subject or parameters are invalid * @since 8.2.0 + * @since 30.0.0 throws {@see InvalidValueException} instead of \InvalidArgumentException */ public function setSubject(string $subject, array $parameters = []): self; @@ -109,8 +117,9 @@ interface IEvent { * * @param string $subject * @return $this - * @throws \InvalidArgumentException if the subject is invalid + * @throws InvalidValueException if the subject is invalid * @since 11.0.0 + * @since 30.0.0 throws {@see InvalidValueException} instead of \InvalidArgumentException */ public function setParsedSubject(string $subject): self; @@ -132,8 +141,9 @@ interface IEvent { * @param string $subject * @param array $parameters * @return $this - * @throws \InvalidArgumentException if the subject or parameters are invalid + * @throws InvalidValueException if the subject or parameters are invalid * @since 11.0.0 + * @since 30.0.0 throws {@see InvalidValueException} instead of \InvalidArgumentException */ public function setRichSubject(string $subject, array $parameters = []): self; @@ -155,8 +165,9 @@ interface IEvent { * @param string $message * @param array $parameters * @return IEvent - * @throws \InvalidArgumentException if the message or parameters are invalid + * @throws InvalidValueException if the message or parameters are invalid * @since 8.2.0 + * @since 30.0.0 throws {@see InvalidValueException} instead of \InvalidArgumentException */ public function setMessage(string $message, array $parameters = []): self; @@ -172,8 +183,9 @@ interface IEvent { * * @param string $message * @return $this - * @throws \InvalidArgumentException if the message is invalid + * @throws InvalidValueException if the message is invalid * @since 11.0.0 + * @since 30.0.0 throws {@see InvalidValueException} instead of \InvalidArgumentException */ public function setParsedMessage(string $message): self; @@ -197,6 +209,7 @@ interface IEvent { * @return $this * @throws \InvalidArgumentException if the message or parameters are invalid * @since 11.0.0 + * @since 30.0.0 throws {@see InvalidValueException} instead of \InvalidArgumentException */ public function setRichMessage(string $message, array $parameters = []): self; @@ -219,8 +232,9 @@ interface IEvent { * @param int $objectId * @param string $objectName * @return IEvent - * @throws \InvalidArgumentException if the object is invalid + * @throws InvalidValueException if the object is invalid * @since 8.2.0 + * @since 30.0.0 throws {@see InvalidValueException} instead of \InvalidArgumentException */ public function setObject(string $objectType, int $objectId, string $objectName = ''): self; @@ -229,8 +243,9 @@ interface IEvent { * * @param string $link * @return IEvent - * @throws \InvalidArgumentException if the link is invalid + * @throws InvalidValueException if the link is invalid * @since 8.2.0 + * @since 30.0.0 throws {@see InvalidValueException} instead of \InvalidArgumentException */ public function setLink(string $link): self; @@ -315,8 +330,9 @@ interface IEvent { /** * @param string $icon * @return $this - * @throws \InvalidArgumentException if the icon is invalid + * @throws InvalidValueException if the icon is invalid * @since 11.0.0 + * @since 30.0.0 throws {@see InvalidValueException} instead of \InvalidArgumentException */ public function setIcon(string $icon): self; @@ -352,7 +368,7 @@ interface IEvent { public function isValidParsed(): bool; /** - * Set whether or not a notification should be automatically generated for this activity. + * Set whether a notification should be automatically generated for this activity. * * Set this to `false` if the app already generates a notification for the event. * @@ -363,7 +379,7 @@ interface IEvent { public function setGenerateNotification(bool $generate): self; /** - * whether or not a notification should be automatically generated for this activity. + * Whether a notification should be automatically generated for this activity. * * @return bool * @since 20.0.0 |