From 484adbbf4925eccbedafbc4c6128a6b771d6b438 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Mon, 12 May 2014 10:31:34 +0200 Subject: [PATCH] Added argument checking system --- src/Api/AbstractJob.php | 10 +- src/Api/AbstractPostJob.php | 18 - src/Api/Api.php | 81 +++- src/Api/ApiJobUnsatisfiedException.php | 6 +- src/Api/JobArgs.php | 19 +- src/Api/JobArgsAlternative.php | 25 ++ src/Api/JobArgsConjunction.php | 25 ++ src/Api/JobArgsNestedStruct.php | 19 + src/Api/JobArgsOptional.php | 20 + src/Api/Jobs/ActivateUserEmailJob.php | 17 +- src/Api/Jobs/AddPostJob.php | 9 +- src/Api/Jobs/AddUserJob.php | 7 +- .../Jobs/CommentJobs/AbstractCommentJob.php | 29 ++ .../{ => CommentJobs}/DeleteCommentJob.php | 20 +- .../Jobs/{ => CommentJobs}/EditCommentJob.php | 20 +- src/Api/Jobs/GetPostContentJob.php | 14 +- src/Api/Jobs/GetPostThumbJob.php | 23 +- src/Api/Jobs/ListLogsJob.php | 7 +- src/Api/Jobs/MergeTagsJob.php | 9 +- .../{ => Jobs/PageJobs}/AbstractPageJob.php | 9 + src/Api/Jobs/{ => PageJobs}/GetLogJob.php | 9 +- .../Jobs/{ => PageJobs}/ListCommentsJob.php | 7 +- src/Api/Jobs/{ => PageJobs}/ListPostsJob.php | 7 +- .../{ => PageJobs}/ListRelatedTagsJob.php | 8 + src/Api/Jobs/{ => PageJobs}/ListTagsJob.php | 7 +- src/Api/Jobs/{ => PageJobs}/ListUsersJob.php | 7 +- src/Api/Jobs/PasswordResetJob.php | 16 +- src/Api/Jobs/PostJobs/AbstractPostJob.php | 35 ++ src/Api/Jobs/{ => PostJobs}/AddCommentJob.php | 15 +- src/Api/Jobs/{ => PostJobs}/DeletePostJob.php | 11 +- .../{ => PostJobs}/EditPostContentJob.php | 15 +- src/Api/Jobs/{ => PostJobs}/EditPostJob.php | 7 +- .../{ => PostJobs}/EditPostRelationsJob.php | 12 +- .../Jobs/{ => PostJobs}/EditPostSafetyJob.php | 12 +- .../Jobs/{ => PostJobs}/EditPostSourceJob.php | 12 +- .../Jobs/{ => PostJobs}/EditPostTagsJob.php | 12 +- .../Jobs/{ => PostJobs}/EditPostThumbJob.php | 12 +- .../Jobs/{ => PostJobs}/FeaturePostJob.php | 9 +- src/Api/Jobs/{ => PostJobs}/FlagPostJob.php | 7 +- src/Api/Jobs/{ => PostJobs}/GetPostJob.php | 7 +- src/Api/Jobs/{ => PostJobs}/ScorePostJob.php | 9 +- .../{ => PostJobs}/TogglePostFavoriteJob.php | 9 +- .../Jobs/{ => PostJobs}/TogglePostTagJob.php | 9 +- .../TogglePostVisibilityJob.php | 7 +- src/Api/Jobs/PreviewCommentJob.php | 15 +- src/Api/Jobs/RenameTagsJob.php | 9 +- .../{ => Jobs/UserJobs}/AbstractUserJob.php | 12 + .../AcceptUserRegistrationJob.php | 7 +- src/Api/Jobs/{ => UserJobs}/DeleteUserJob.php | 7 +- .../{ => UserJobs}/EditUserAccessRankJob.php | 12 +- .../Jobs/{ => UserJobs}/EditUserEmailJob.php | 12 +- src/Api/Jobs/{ => UserJobs}/EditUserJob.php | 7 +- .../Jobs/{ => UserJobs}/EditUserNameJob.php | 12 +- .../{ => UserJobs}/EditUserPasswordJob.php | 12 +- src/Api/Jobs/{ => UserJobs}/FlagUserJob.php | 7 +- src/Api/Jobs/{ => UserJobs}/GetUserJob.php | 7 +- .../Jobs/{ => UserJobs}/ToggleUserBanJob.php | 7 +- src/Controllers/UserController.php | 82 ++-- src/Views/comment-small.phtml | 6 +- src/Views/post-view.phtml | 4 +- src/Views/static-main.phtml | 2 +- src/Views/top-navigation.phtml | 12 +- src/Views/user-delete.phtml | 2 +- src/Views/user-edit.phtml | 2 +- src/Views/user-list.phtml | 4 +- src/Views/user-select.phtml | 2 +- src/Views/user-settings.phtml | 2 +- src/Views/user-view.phtml | 24 +- src/routes.php | 20 +- tests/Api/ApiArgumentTest.php | 367 ++++++++++++++++++ tests/Api/ApiAuthTest.php | 2 +- tests/Api/ApiEmailRequirementsTest.php | 10 +- tests/Api/ApiPrivilegeTest.php | 18 +- 73 files changed, 1072 insertions(+), 261 deletions(-) delete mode 100644 src/Api/AbstractPostJob.php create mode 100644 src/Api/JobArgsAlternative.php create mode 100644 src/Api/JobArgsConjunction.php create mode 100644 src/Api/JobArgsNestedStruct.php create mode 100644 src/Api/JobArgsOptional.php create mode 100644 src/Api/Jobs/CommentJobs/AbstractCommentJob.php rename src/Api/Jobs/{ => CommentJobs}/DeleteCommentJob.php (59%) rename src/Api/Jobs/{ => CommentJobs}/EditCommentJob.php (65%) rename src/Api/{ => Jobs/PageJobs}/AbstractPageJob.php (77%) rename src/Api/Jobs/{ => PageJobs}/GetLogJob.php (87%) rename src/Api/Jobs/{ => PageJobs}/ListCommentsJob.php (88%) rename src/Api/Jobs/{ => PageJobs}/ListPostsJob.php (83%) rename src/Api/Jobs/{ => PageJobs}/ListRelatedTagsJob.php (79%) rename src/Api/Jobs/{ => PageJobs}/ListTagsJob.php (82%) rename src/Api/Jobs/{ => PageJobs}/ListUsersJob.php (82%) create mode 100644 src/Api/Jobs/PostJobs/AbstractPostJob.php rename src/Api/Jobs/{ => PostJobs}/AddCommentJob.php (69%) rename src/Api/Jobs/{ => PostJobs}/DeletePostJob.php (69%) rename src/Api/Jobs/{ => PostJobs}/EditPostContentJob.php (82%) rename src/Api/Jobs/{ => PostJobs}/EditPostJob.php (90%) rename src/Api/Jobs/{ => PostJobs}/EditPostRelationsJob.php (90%) rename src/Api/Jobs/{ => PostJobs}/EditPostSafetyJob.php (87%) rename src/Api/Jobs/{ => PostJobs}/EditPostSourceJob.php (86%) rename src/Api/Jobs/{ => PostJobs}/EditPostTagsJob.php (91%) rename src/Api/Jobs/{ => PostJobs}/EditPostThumbJob.php (84%) rename src/Api/Jobs/{ => PostJobs}/FeaturePostJob.php (80%) rename src/Api/Jobs/{ => PostJobs}/FlagPostJob.php (85%) rename src/Api/Jobs/{ => PostJobs}/GetPostJob.php (80%) rename src/Api/Jobs/{ => PostJobs}/ScorePostJob.php (69%) rename src/Api/Jobs/{ => PostJobs}/TogglePostFavoriteJob.php (77%) rename src/Api/Jobs/{ => PostJobs}/TogglePostTagJob.php (87%) rename src/Api/Jobs/{ => PostJobs}/TogglePostVisibilityJob.php (81%) rename src/Api/{ => Jobs/UserJobs}/AbstractUserJob.php (56%) rename src/Api/Jobs/{ => UserJobs}/AcceptUserRegistrationJob.php (79%) rename src/Api/Jobs/{ => UserJobs}/DeleteUserJob.php (80%) rename src/Api/Jobs/{ => UserJobs}/EditUserAccessRankJob.php (85%) rename src/Api/Jobs/{ => UserJobs}/EditUserEmailJob.php (91%) rename src/Api/Jobs/{ => UserJobs}/EditUserJob.php (91%) rename src/Api/Jobs/{ => UserJobs}/EditUserNameJob.php (85%) rename src/Api/Jobs/{ => UserJobs}/EditUserPasswordJob.php (86%) rename src/Api/Jobs/{ => UserJobs}/FlagUserJob.php (85%) rename src/Api/Jobs/{ => UserJobs}/GetUserJob.php (65%) rename src/Api/Jobs/{ => UserJobs}/ToggleUserBanJob.php (82%) create mode 100644 tests/Api/ApiArgumentTest.php diff --git a/src/Api/AbstractJob.php b/src/Api/AbstractJob.php index bb6c6bae..5b415225 100644 --- a/src/Api/AbstractJob.php +++ b/src/Api/AbstractJob.php @@ -14,22 +14,22 @@ abstract class AbstractJob public abstract function execute(); - public function isSatisfied() + public function getRequiredArguments() { - return true; + return null; } - public function requiresAuthentication() + public function getRequiredPrivileges() { return false; } - public function requiresConfirmedEmail() + public function isAuthenticationRequired() { return false; } - public function requiresPrivilege() + public function isConfirmedEmailRequired() { return false; } diff --git a/src/Api/AbstractPostJob.php b/src/Api/AbstractPostJob.php deleted file mode 100644 index 4e3956cd..00000000 --- a/src/Api/AbstractPostJob.php +++ /dev/null @@ -1,18 +0,0 @@ -hasArgument(JobArgs::ARG_POST_ENTITY)) - { - $this->post = $this->getArgument(JobArgs::ARG_POST_ENTITY); - } - else - { - $postId = $this->getArgument(JobArgs::ARG_POST_ID); - $this->post = PostModel::getByIdOrName($postId); - } - } -} diff --git a/src/Api/Api.php b/src/Api/Api.php index f89cbeba..2310b288 100644 --- a/src/Api/Api.php +++ b/src/Api/Api.php @@ -9,8 +9,7 @@ final class Api { $job->setArguments($jobArgs); - if (!$job->isSatisfied()) - throw new ApiJobUnsatisfiedException($job); + self::checkArguments($job); $job->prepare(); @@ -20,25 +19,6 @@ final class Api }); } - public static function checkPrivileges(AbstractJob $job) - { - if ($job->requiresAuthentication()) - Access::assertAuthentication(); - - if ($job->requiresConfirmedEmail()) - Access::assertEmailConfirmation(); - - $privileges = $job->requiresPrivilege(); - if ($privileges !== false) - { - if (!is_array($privileges)) - $privileges = [$privileges]; - - foreach ($privileges as $privilege) - Access::assert($privilege); - } - } - public static function runMultiple($jobs) { $statuses = []; @@ -52,4 +32,63 @@ final class Api }); return $statuses; } + + public static function checkArguments(AbstractJob $job) + { + self::runArgumentCheck($job, $job->getRequiredArguments()); + } + + public static function checkPrivileges(AbstractJob $job) + { + if ($job->isAuthenticationRequired()) + Access::assertAuthentication(); + + if ($job->isConfirmedEmailRequired()) + Access::assertEmailConfirmation(); + + $privileges = $job->getRequiredPrivileges(); + if ($privileges !== false) + { + if (!is_array($privileges)) + $privileges = [$privileges]; + + foreach ($privileges as $privilege) + Access::assert($privilege); + } + } + + private static function runArgumentCheck($job, $item) + { + if (is_array($item)) + throw new Exception('Argument definition cannot be an array.'); + elseif ($item instanceof JobArgsNestedStruct) + { + if ($item instanceof JobArgsAlternative) + { + $success = false; + foreach ($item->args as $subItem) + { + try + { + self::runArgumentCheck($job, $subItem); + $success = true; + } + catch (ApiJobUnsatisfiedException $e) + { + } + } + if (!$success) + throw new ApiJobUnsatisfiedException($job); + } + elseif ($item instanceof JobArgsConjunction) + { + foreach ($item->args as $subItem) + !self::runArgumentCheck($job, $subItem); + } + } + elseif ($item === null) + return; + elseif (!$job->hasArgument($item)) + throw new ApiJobUnsatisfiedException($job, $item); + } } diff --git a/src/Api/ApiJobUnsatisfiedException.php b/src/Api/ApiJobUnsatisfiedException.php index 43c799ec..0bf3db81 100644 --- a/src/Api/ApiJobUnsatisfiedException.php +++ b/src/Api/ApiJobUnsatisfiedException.php @@ -1,8 +1,10 @@ args); + elseif ($arg !== null) + $finalArgs []= $arg; + } + + if (count($finalArgs) == 1) + return $finalArgs[0]; + else + return new self($finalArgs); + } +} diff --git a/src/Api/JobArgsConjunction.php b/src/Api/JobArgsConjunction.php new file mode 100644 index 00000000..4571bfd7 --- /dev/null +++ b/src/Api/JobArgsConjunction.php @@ -0,0 +1,25 @@ +args); + elseif ($arg !== null) + $finalArgs []= $arg; + } + + if (count($finalArgs) == 1) + return $finalArgs[0]; + else + return new self($finalArgs); + } +} diff --git a/src/Api/JobArgsNestedStruct.php b/src/Api/JobArgsNestedStruct.php new file mode 100644 index 00000000..ca272157 --- /dev/null +++ b/src/Api/JobArgsNestedStruct.php @@ -0,0 +1,19 @@ +args = $args; + } + + public static function factory(array $args) + { + throw new BadMethodCallException('Not implemented'); + } +} diff --git a/src/Api/JobArgsOptional.php b/src/Api/JobArgsOptional.php new file mode 100644 index 00000000..b2a341a5 --- /dev/null +++ b/src/Api/JobArgsOptional.php @@ -0,0 +1,20 @@ +hasArgument(JobArgs::ARG_TOKEN)) { - $user = UserModel::getByNameOrEmail($this->getArgument(JobArgs::ARG_USER_NAME)); + if ($this->hasArgument(JobArgs::ARG_USER_ENTITY)) + $user = $this->getArgument(JobArgs::ARG_USER_ENTITY); + elseif ($this->hasArgument(JobArgs::ARG_USER_NAME)) + $user = UserModel::getByName($this->getArgument(JobArgs::ARG_USER_NAME)); + else + $user = UserModel::getByEmail($this->getArgument(JobArgs::ARG_USER_EMAIL)); if (empty($user->getUnconfirmedEmail())) { @@ -38,6 +43,16 @@ class ActivateUserEmailJob extends AbstractJob } } + public function getRequiredArguments() + { + return JobArgs::Alternative( + JobArgs::ARG_TOKEN, + JobArgs::Alternative( + JobArgs::ARG_USER_ENTITY, + JobArgs::ARG_USER_EMAIL, + JobArgs::ARG_USER_NAME)); + } + public static function sendEmail($user) { $regConfig = getConfig()->registration; diff --git a/src/Api/Jobs/AddPostJob.php b/src/Api/Jobs/AddPostJob.php index ffafd600..189a1793 100644 --- a/src/Api/Jobs/AddPostJob.php +++ b/src/Api/Jobs/AddPostJob.php @@ -44,12 +44,17 @@ class AddPostJob extends AbstractJob return $post; } - public function requiresPrivilege() + public function getRequiredArguments() + { + return JobArgs::Optional(JobArgs::ARG_ANONYMOUS); + } + + public function getRequiredPrivileges() { return new Privilege(Privilege::AddPost); } - public function requiresConfirmedEmail() + public function isConfirmedEmailRequired() { return getConfig()->registration->needEmailForUploading; } diff --git a/src/Api/Jobs/AddUserJob.php b/src/Api/Jobs/AddUserJob.php index 86f67a4b..bc36172c 100644 --- a/src/Api/Jobs/AddUserJob.php +++ b/src/Api/Jobs/AddUserJob.php @@ -46,7 +46,12 @@ class AddUserJob extends AbstractJob return $user; } - public function requiresPrivilege() + public function getRequiredArguments() + { + return null; + } + + public function getRequiredPrivileges() { return new Privilege(Privilege::RegisterAccount); } diff --git a/src/Api/Jobs/CommentJobs/AbstractCommentJob.php b/src/Api/Jobs/CommentJobs/AbstractCommentJob.php new file mode 100644 index 00000000..ebfd4613 --- /dev/null +++ b/src/Api/Jobs/CommentJobs/AbstractCommentJob.php @@ -0,0 +1,29 @@ +hasArgument(JobArgs::ARG_COMMENT_ENTITY)) + { + $this->comment = $this->getArgument(JobArgs::ARG_COMMENT_ENTITY); + } + else + { + $commentId = $this->getArgument(JobArgs::ARG_COMMENT_ID); + $this->comment = CommentModel::getById($commentId); + } + } + + public function getRequiredArguments() + { + return JobArgs::Conjunction( + JobArgs::Alternative( + JobArgs::ARG_COMMENT_ID, + JobArgs::ARG_COMMENT_ENTITY), + $this->getRequiredSubArguments()); + } + + public abstract function getRequiredSubArguments(); +} diff --git a/src/Api/Jobs/DeleteCommentJob.php b/src/Api/Jobs/CommentJobs/DeleteCommentJob.php similarity index 59% rename from src/Api/Jobs/DeleteCommentJob.php rename to src/Api/Jobs/CommentJobs/DeleteCommentJob.php index 7238d60e..d85470a3 100644 --- a/src/Api/Jobs/DeleteCommentJob.php +++ b/src/Api/Jobs/CommentJobs/DeleteCommentJob.php @@ -1,13 +1,6 @@ comment = CommentModel::getById($this->getArgument(JobArgs::ARG_COMMENT_ID)); - } - public function execute() { $post = $this->comment->getPost(); @@ -19,19 +12,24 @@ class DeleteCommentJob extends AbstractJob 'post' => TextHelper::reprPost($post)]); } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return null; + } + + public function getRequiredPrivileges() { return new Privilege( Privilege::DeleteComment, Access::getIdentity($this->comment->getCommenter())); } - public function requiresAuthentication() + public function isAuthenticationRequired() { return true; } - public function requiresConfirmedEmail() + public function isConfirmedEmailRequired() { return false; } diff --git a/src/Api/Jobs/EditCommentJob.php b/src/Api/Jobs/CommentJobs/EditCommentJob.php similarity index 65% rename from src/Api/Jobs/EditCommentJob.php rename to src/Api/Jobs/CommentJobs/EditCommentJob.php index c868114f..535d690b 100644 --- a/src/Api/Jobs/EditCommentJob.php +++ b/src/Api/Jobs/CommentJobs/EditCommentJob.php @@ -1,13 +1,6 @@ comment = CommentModel::getById($this->getArgument(JobArgs::ARG_COMMENT_ID)); - } - public function execute() { $comment = $this->comment; @@ -23,19 +16,24 @@ class EditCommentJob extends AbstractJob return $comment; } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return JobArgs::ARG_NEW_TEXT; + } + + public function getRequiredPrivileges() { return new Privilege( Privilege::EditComment, Access::getIdentity($this->comment->getCommenter())); } - public function requiresAuthentication() + public function isAuthenticationRequired() { return true; } - public function requiresConfirmedEmail() + public function isConfirmedEmailRequired() { return false; } diff --git a/src/Api/Jobs/GetPostContentJob.php b/src/Api/Jobs/GetPostContentJob.php index a1b65fef..921b527d 100644 --- a/src/Api/Jobs/GetPostContentJob.php +++ b/src/Api/Jobs/GetPostContentJob.php @@ -5,7 +5,10 @@ class GetPostContentJob extends AbstractJob public function prepare() { - $this->post = PostModel::getByName($this->getArgument(JobArgs::ARG_POST_NAME)); + if ($this->hasArgument(JobArgs::ARG_POST_ENTITY)) + $this->post = $this->getArgument(JobArgs::ARG_POST_ENTITY); + else + $this->post = PostModel::getByName($this->getArgument(JobArgs::ARG_POST_NAME)); } public function execute() @@ -30,7 +33,14 @@ class GetPostContentJob extends AbstractJob return new ApiFileOutput($path, $fileName); } - public function requiresPrivilege() + public function getRequiredArguments() + { + return JobArgs::Alternative( + JobArgs::ARG_POST_NAME, + JobArgs::ARG_POST_ENTITY); + } + + public function getRequiredPrivileges() { $post = $this->post; $privileges = []; diff --git a/src/Api/Jobs/GetPostThumbJob.php b/src/Api/Jobs/GetPostThumbJob.php index 0f550cb1..90a8e0d1 100644 --- a/src/Api/Jobs/GetPostThumbJob.php +++ b/src/Api/Jobs/GetPostThumbJob.php @@ -3,7 +3,14 @@ class GetPostThumbJob extends AbstractJob { public function execute() { - $name = $this->getArgument(JobArgs::ARG_POST_NAME); + if ($this->getArgument(JobArgs::ARG_POST_NAME)) + $name = $this->getArgument(JobArgs::ARG_POST_NAME); + else + { + $post = PostModel::getByName($this->getArgument(JobArgs::ARG_POST_ENTITY)); + $name = $post->getName(); + } + $width = $this->hasArgument(JobArgs::ARG_THUMB_WIDTH) ? $this->getArgument(JobArgs::ARG_THUMB_WIDTH) : null; $height = $this->hasArgument(JobArgs::ARG_THUMB_HEIGHT) ? $this->getArgument(JobArgs::ARG_THUMB_HEIGHT) : null; @@ -13,7 +20,7 @@ class GetPostThumbJob extends AbstractJob $path = PostModel::getThumbDefaultPath($name, $width, $height); if (!file_exists($path)) { - $post = PostModel::getByIdOrName($name); + $post = PostModel::getByName($name); if ($post->isHidden()) Access::assert(new Privilege(Privilege::ListPosts, 'hidden')); @@ -35,7 +42,17 @@ class GetPostThumbJob extends AbstractJob return new ApiFileOutput($path, 'thumbnail.jpg'); } - public function requiresPrivilege() + public function getRequiredArguments() + { + return JobArgs::Conjunction( + JobArgs::Alternative( + JobArgs::ARG_POST_ENTITY, + JobArgs::ARG_POST_NAME), + JobArgs::Optional(JobArgs::ARG_THUMB_WIDTH), + JobArgs::Optional(JobArgs::ARG_THUMB_HEIGHT)); + } + + public function getRequiredPrivileges() { //manually enforced in execute when post is retrieved return false; diff --git a/src/Api/Jobs/ListLogsJob.php b/src/Api/Jobs/ListLogsJob.php index 8d812346..fe467f7e 100644 --- a/src/Api/Jobs/ListLogsJob.php +++ b/src/Api/Jobs/ListLogsJob.php @@ -17,7 +17,12 @@ class ListLogsJob extends AbstractJob return $logs; } - public function requiresPrivilege() + public function getRequiredArguments() + { + return null; + } + + public function getRequiredPrivileges() { return new Privilege(Privilege::ListLogs); } diff --git a/src/Api/Jobs/MergeTagsJob.php b/src/Api/Jobs/MergeTagsJob.php index e9b2d35b..f36f65b0 100644 --- a/src/Api/Jobs/MergeTagsJob.php +++ b/src/Api/Jobs/MergeTagsJob.php @@ -15,7 +15,14 @@ class MergeTagsJob extends AbstractJob 'target' => TextHelper::reprTag($targetTag)]); } - public function requiresPrivilege() + public function getRequiredArguments() + { + return JobArgs::Conjunction( + JobArgs::ARG_SOURCE_TAG_NAME, + JobArgs::ARG_TARGET_TAG_NAME); + } + + public function getRequiredPrivileges() { return new Privilege(Privilege::MergeTags); } diff --git a/src/Api/AbstractPageJob.php b/src/Api/Jobs/PageJobs/AbstractPageJob.php similarity index 77% rename from src/Api/AbstractPageJob.php rename to src/Api/Jobs/PageJobs/AbstractPageJob.php index 35cbddd5..2e9719d1 100644 --- a/src/Api/AbstractPageJob.php +++ b/src/Api/Jobs/PageJobs/AbstractPageJob.php @@ -30,4 +30,13 @@ abstract class AbstractPageJob extends AbstractJob $ret->pageCount = $pageCount; return $ret; } + + public function getRequiredArguments() + { + return JobArgs::Conjunction( + JobArgs::ARG_PAGE_NUMBER, + $this->getRequiredSubArguments()); + } + + public abstract function getRequiredSubArguments(); } diff --git a/src/Api/Jobs/GetLogJob.php b/src/Api/Jobs/PageJobs/GetLogJob.php similarity index 87% rename from src/Api/Jobs/GetLogJob.php rename to src/Api/Jobs/PageJobs/GetLogJob.php index 2796193e..84b949eb 100644 --- a/src/Api/Jobs/GetLogJob.php +++ b/src/Api/Jobs/PageJobs/GetLogJob.php @@ -39,7 +39,14 @@ class GetLogJob extends AbstractPageJob return intval(getConfig()->browsing->logsPerPage); } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return JobArgs::Conjunction( + JobArgs::ARG_LOG_ID, + JobArgs::ARG_QUERY); + } + + public function getRequiredPrivileges() { return new Privilege(Privilege::ViewLog); } diff --git a/src/Api/Jobs/ListCommentsJob.php b/src/Api/Jobs/PageJobs/ListCommentsJob.php similarity index 88% rename from src/Api/Jobs/ListCommentsJob.php rename to src/Api/Jobs/PageJobs/ListCommentsJob.php index f75c8447..21e8ebd3 100644 --- a/src/Api/Jobs/ListCommentsJob.php +++ b/src/Api/Jobs/PageJobs/ListCommentsJob.php @@ -25,7 +25,12 @@ class ListCommentsJob extends AbstractPageJob return intval(getConfig()->comments->commentsPerPage); } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return null; + } + + public function getRequiredPrivileges() { return new Privilege(Privilege::ListComments); } diff --git a/src/Api/Jobs/ListPostsJob.php b/src/Api/Jobs/PageJobs/ListPostsJob.php similarity index 83% rename from src/Api/Jobs/ListPostsJob.php rename to src/Api/Jobs/PageJobs/ListPostsJob.php index e1104903..eccacaa4 100644 --- a/src/Api/Jobs/ListPostsJob.php +++ b/src/Api/Jobs/PageJobs/ListPostsJob.php @@ -20,7 +20,12 @@ class ListPostsJob extends AbstractPageJob return intval(getConfig()->browsing->postsPerPage); } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return JobArgs::ARG_QUERY; + } + + public function getRequiredPrivileges() { return new Privilege(Privilege::ListPosts); } diff --git a/src/Api/Jobs/ListRelatedTagsJob.php b/src/Api/Jobs/PageJobs/ListRelatedTagsJob.php similarity index 79% rename from src/Api/Jobs/ListRelatedTagsJob.php rename to src/Api/Jobs/PageJobs/ListRelatedTagsJob.php index dd13b865..cd732cce 100644 --- a/src/Api/Jobs/ListRelatedTagsJob.php +++ b/src/Api/Jobs/PageJobs/ListRelatedTagsJob.php @@ -20,4 +20,12 @@ class ListRelatedTagsJob extends ListTagsJob { return intval(getConfig()->browsing->tagsRelated); } + + public function getRequiredArguments() + { + return JobArgs::Conjunction( + parent::getRequiredArguments(), + Jobargs::ARG_TAG_NAME, + JobArgs::Optional(JobArgs::ARG_TAG_NAMES)); + } } diff --git a/src/Api/Jobs/ListTagsJob.php b/src/Api/Jobs/PageJobs/ListTagsJob.php similarity index 82% rename from src/Api/Jobs/ListTagsJob.php rename to src/Api/Jobs/PageJobs/ListTagsJob.php index d6b7ccd4..08a8f6fe 100644 --- a/src/Api/Jobs/ListTagsJob.php +++ b/src/Api/Jobs/PageJobs/ListTagsJob.php @@ -18,7 +18,12 @@ class ListTagsJob extends AbstractPageJob return intval(getConfig()->browsing->tagsPerPage); } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return JobArgs::ARG_QUERY; + } + + public function getRequiredPrivileges() { return new Privilege(Privilege::ListTags); } diff --git a/src/Api/Jobs/ListUsersJob.php b/src/Api/Jobs/PageJobs/ListUsersJob.php similarity index 82% rename from src/Api/Jobs/ListUsersJob.php rename to src/Api/Jobs/PageJobs/ListUsersJob.php index e49e1128..2c06d1e7 100644 --- a/src/Api/Jobs/ListUsersJob.php +++ b/src/Api/Jobs/PageJobs/ListUsersJob.php @@ -18,7 +18,12 @@ class ListUsersJob extends AbstractPageJob return intval(getConfig()->browsing->usersPerPage); } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return JobArgs::ARG_QUERY; + } + + public function getRequiredPrivileges() { return new Privilege(Privilege::ListUsers); } diff --git a/src/Api/Jobs/PasswordResetJob.php b/src/Api/Jobs/PasswordResetJob.php index 087ab6fb..d75a4b49 100644 --- a/src/Api/Jobs/PasswordResetJob.php +++ b/src/Api/Jobs/PasswordResetJob.php @@ -5,7 +5,12 @@ class PasswordResetJob extends AbstractJob { if (!$this->hasArgument(JobArgs::ARG_TOKEN)) { - $user = UserModel::getByNameOrEmail($this->getArgument(JobArgs::ARG_USER_NAME)); + if ($this->hasArgument(JobArgs::ARG_USER_NAME)) + $user = UserModel::getByNameOrEmail($this->getArgument(JobArgs::ARG_USER_NAME)); + elseif ($this->hasArgument(JobArgs::ARG_USER_EMAIL)) + $user = UserModel::getByNameOrEmail($this->getArgument(JobArgs::ARG_USER_EMAIL)); + else + $user = $this->getArgument(JobArgs::ARG_USER_ENTITTY); if (empty($user->getConfirmedEmail())) throw new SimpleException('This user has no e-mail confirmed; password reset cannot proceed'); @@ -42,6 +47,15 @@ class PasswordResetJob extends AbstractJob } } + public function getRequiredArguments() + { + return JobArgs::Alternative( + JobArgs::ARG_USER_NAME, + JobArgs::ARG_USER_EMAIL, + JobArgs::ARG_USER_ENTITY, + JobArgs::ARG_TOKEN); + } + public static function sendEmail($user) { $regConfig = getConfig()->registration; diff --git a/src/Api/Jobs/PostJobs/AbstractPostJob.php b/src/Api/Jobs/PostJobs/AbstractPostJob.php new file mode 100644 index 00000000..29503bbd --- /dev/null +++ b/src/Api/Jobs/PostJobs/AbstractPostJob.php @@ -0,0 +1,35 @@ +hasArgument(JobArgs::ARG_POST_ENTITY)) + { + $this->post = $this->getArgument(JobArgs::ARG_POST_ENTITY); + } + elseif ($this->hasArgument(JobArgs::ARG_POST_ID)) + { + $postId = $this->getArgument(JobArgs::ARG_POST_ID); + $this->post = PostModel::getById($postId); + } + else + { + $postName = $this->getArgument(JobArgs::ARG_POST_NAME); + $this->post = PostModel::getByName($postName); + } + } + + public function getRequiredArguments() + { + return JobArgs::Conjunction( + JobArgs::Alternative( + JobArgs::ARG_POST_ID, + JobArgs::ARG_POST_NAME, + JobArgs::ARG_POST_ENTITY), + $this->getRequiredSubArguments()); + } + + public abstract function getRequiredSubArguments(); +} diff --git a/src/Api/Jobs/AddCommentJob.php b/src/Api/Jobs/PostJobs/AddCommentJob.php similarity index 69% rename from src/Api/Jobs/AddCommentJob.php rename to src/Api/Jobs/PostJobs/AddCommentJob.php index 8f8f98f8..a755617b 100644 --- a/src/Api/Jobs/AddCommentJob.php +++ b/src/Api/Jobs/PostJobs/AddCommentJob.php @@ -1,10 +1,10 @@ post; $user = Auth::getCurrentUser(); - $post = PostModel::getById($this->getArgument(JobArgs::ARG_POST_ID)); $text = $this->getArgument(JobArgs::ARG_NEW_TEXT); $comment = CommentModel::spawn(); @@ -21,17 +21,22 @@ class AddCommentJob extends AbstractJob return $comment; } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return JobArgs::ARG_NEW_TEXT; + } + + public function getRequiredPrivileges() { return new Privilege(Privilege::AddComment); } - public function requiresAuthentication() + public function isAuthenticationRequired() { return false; } - public function requiresConfirmedEmail() + public function isConfirmedEmailRequired() { return getConfig()->registration->needEmailForCommenting; } diff --git a/src/Api/Jobs/DeletePostJob.php b/src/Api/Jobs/PostJobs/DeletePostJob.php similarity index 69% rename from src/Api/Jobs/DeletePostJob.php rename to src/Api/Jobs/PostJobs/DeletePostJob.php index 5d1e5701..e84aa47a 100644 --- a/src/Api/Jobs/DeletePostJob.php +++ b/src/Api/Jobs/PostJobs/DeletePostJob.php @@ -12,19 +12,24 @@ class DeletePostJob extends AbstractPostJob 'post' => TextHelper::reprPost($post)]); } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return null; + } + + public function getRequiredPrivileges() { return new Privilege( Privilege::DeletePost, Access::getIdentity($this->post->getUploader())); } - public function requiresAuthentication() + public function isAuthenticationRequired() { return true; } - public function requiresConfirmedEmail() + public function isConfirmedEmailRequired() { return false; } diff --git a/src/Api/Jobs/EditPostContentJob.php b/src/Api/Jobs/PostJobs/EditPostContentJob.php similarity index 82% rename from src/Api/Jobs/EditPostContentJob.php rename to src/Api/Jobs/PostJobs/EditPostContentJob.php index 0751753b..a7cd0f4c 100644 --- a/src/Api/Jobs/EditPostContentJob.php +++ b/src/Api/Jobs/PostJobs/EditPostContentJob.php @@ -1,12 +1,6 @@ hasArgument(JobArgs::ARG_NEW_POST_CONTENT) - or $this->hasArgument(JobArgs::ARG_NEW_POST_CONTENT_URL); - } - public function execute() { $post = $this->post; @@ -32,7 +26,14 @@ class EditPostContentJob extends AbstractPostJob return $post; } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return JobArgs::Alternative( + JobArgs::ARG_NEW_POST_CONTENT, + JobArgs::ARG_NEW_POST_CONTENT_URL); + } + + public function getRequiredPrivileges() { return new Privilege( $this->getContext() == self::CONTEXT_BATCH_ADD diff --git a/src/Api/Jobs/EditPostJob.php b/src/Api/Jobs/PostJobs/EditPostJob.php similarity index 90% rename from src/Api/Jobs/EditPostJob.php rename to src/Api/Jobs/PostJobs/EditPostJob.php index 1f43c856..90ceea92 100644 --- a/src/Api/Jobs/EditPostJob.php +++ b/src/Api/Jobs/PostJobs/EditPostJob.php @@ -43,7 +43,12 @@ class EditPostJob extends AbstractPostJob return $post; } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return null; + } + + public function getRequiredPrivileges() { return new Privilege( $this->getContext() == self::CONTEXT_BATCH_ADD diff --git a/src/Api/Jobs/EditPostRelationsJob.php b/src/Api/Jobs/PostJobs/EditPostRelationsJob.php similarity index 90% rename from src/Api/Jobs/EditPostRelationsJob.php rename to src/Api/Jobs/PostJobs/EditPostRelationsJob.php index 315e496e..ad084f28 100644 --- a/src/Api/Jobs/EditPostRelationsJob.php +++ b/src/Api/Jobs/PostJobs/EditPostRelationsJob.php @@ -1,11 +1,6 @@ hasArgument(JobArgs::ARG_NEW_RELATED_POST_IDS); - } - public function execute() { $post = $this->post; @@ -37,7 +32,12 @@ class EditPostRelationsJob extends AbstractPostJob return $post; } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return JobArgs::ARG_NEW_RELATED_POST_IDS; + } + + public function getRequiredPrivileges() { return new Privilege( $this->getContext() == self::CONTEXT_BATCH_ADD diff --git a/src/Api/Jobs/EditPostSafetyJob.php b/src/Api/Jobs/PostJobs/EditPostSafetyJob.php similarity index 87% rename from src/Api/Jobs/EditPostSafetyJob.php rename to src/Api/Jobs/PostJobs/EditPostSafetyJob.php index c2bc3182..67555cc5 100644 --- a/src/Api/Jobs/EditPostSafetyJob.php +++ b/src/Api/Jobs/PostJobs/EditPostSafetyJob.php @@ -1,11 +1,6 @@ hasArgument(JobArgs::ARG_NEW_SAFETY); - } - public function execute() { $post = $this->post; @@ -28,7 +23,12 @@ class EditPostSafetyJob extends AbstractPostJob return $post; } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return JobArgs::ARG_NEW_SAFETY; + } + + public function getRequiredPrivileges() { return new Privilege( $this->getContext() == self::CONTEXT_BATCH_ADD diff --git a/src/Api/Jobs/EditPostSourceJob.php b/src/Api/Jobs/PostJobs/EditPostSourceJob.php similarity index 86% rename from src/Api/Jobs/EditPostSourceJob.php rename to src/Api/Jobs/PostJobs/EditPostSourceJob.php index 23337302..c7e7d8da 100644 --- a/src/Api/Jobs/EditPostSourceJob.php +++ b/src/Api/Jobs/PostJobs/EditPostSourceJob.php @@ -1,11 +1,6 @@ hasArgument(JobArgs::ARG_NEW_SOURCE); - } - public function execute() { $post = $this->post; @@ -28,7 +23,12 @@ class EditPostSourceJob extends AbstractPostJob return $post; } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return JobArgs::ARG_NEW_SOURCE; + } + + public function getRequiredPrivileges() { return new Privilege( $this->getContext() == self::CONTEXT_BATCH_ADD diff --git a/src/Api/Jobs/EditPostTagsJob.php b/src/Api/Jobs/PostJobs/EditPostTagsJob.php similarity index 91% rename from src/Api/Jobs/EditPostTagsJob.php rename to src/Api/Jobs/PostJobs/EditPostTagsJob.php index 8bc0164c..45939a02 100644 --- a/src/Api/Jobs/EditPostTagsJob.php +++ b/src/Api/Jobs/PostJobs/EditPostTagsJob.php @@ -1,11 +1,6 @@ hasArgument(JobArgs::ARG_NEW_TAG_NAMES); - } - public function execute() { $post = $this->post; @@ -45,7 +40,12 @@ class EditPostTagsJob extends AbstractPostJob return $post; } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return JobArgs::ARG_NEW_TAG_NAMES; + } + + public function getRequiredPrivileges() { return new Privilege( $this->getContext() == self::CONTEXT_BATCH_ADD diff --git a/src/Api/Jobs/EditPostThumbJob.php b/src/Api/Jobs/PostJobs/EditPostThumbJob.php similarity index 84% rename from src/Api/Jobs/EditPostThumbJob.php rename to src/Api/Jobs/PostJobs/EditPostThumbJob.php index d6619002..8331e426 100644 --- a/src/Api/Jobs/EditPostThumbJob.php +++ b/src/Api/Jobs/PostJobs/EditPostThumbJob.php @@ -1,11 +1,6 @@ hasArgument(JobArgs::ARG_NEW_THUMB_CONTENT); - } - public function execute() { $post = $this->post; @@ -23,7 +18,12 @@ class EditPostThumbJob extends AbstractPostJob return $post; } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return JobArgs::ARG_NEW_THUMB_CONTENT; + } + + public function getRequiredPrivileges() { return new Privilege( $this->getContext() == self::CONTEXT_BATCH_ADD diff --git a/src/Api/Jobs/FeaturePostJob.php b/src/Api/Jobs/PostJobs/FeaturePostJob.php similarity index 80% rename from src/Api/Jobs/FeaturePostJob.php rename to src/Api/Jobs/PostJobs/FeaturePostJob.php index 41c957b8..8b03735d 100644 --- a/src/Api/Jobs/FeaturePostJob.php +++ b/src/Api/Jobs/PostJobs/FeaturePostJob.php @@ -20,14 +20,19 @@ class FeaturePostJob extends AbstractPostJob return $post; } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return JobArgs::Optional(JobArgs::ARG_ANONYMOUS); + } + + public function getRequiredPrivileges() { return new Privilege( Privilege::FeaturePost, Access::getIdentity($this->post->getUploader())); } - public function requiresAuthentication() + public function isAuthenticationRequired() { return true; } diff --git a/src/Api/Jobs/FlagPostJob.php b/src/Api/Jobs/PostJobs/FlagPostJob.php similarity index 85% rename from src/Api/Jobs/FlagPostJob.php rename to src/Api/Jobs/PostJobs/FlagPostJob.php index 83d6ac67..11e1534e 100644 --- a/src/Api/Jobs/FlagPostJob.php +++ b/src/Api/Jobs/PostJobs/FlagPostJob.php @@ -19,7 +19,12 @@ class FlagPostJob extends AbstractPostJob return $post; } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return null; + } + + public function getRequiredPrivileges() { return new Privilege( Privilege::FlagPost, diff --git a/src/Api/Jobs/GetPostJob.php b/src/Api/Jobs/PostJobs/GetPostJob.php similarity index 80% rename from src/Api/Jobs/GetPostJob.php rename to src/Api/Jobs/PostJobs/GetPostJob.php index 860f7613..fa297de5 100644 --- a/src/Api/Jobs/GetPostJob.php +++ b/src/Api/Jobs/PostJobs/GetPostJob.php @@ -10,7 +10,12 @@ class GetPostJob extends AbstractPostJob return $post; } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return null; + } + + public function getRequiredPrivileges() { $post = $this->post; $privileges = []; diff --git a/src/Api/Jobs/ScorePostJob.php b/src/Api/Jobs/PostJobs/ScorePostJob.php similarity index 69% rename from src/Api/Jobs/ScorePostJob.php rename to src/Api/Jobs/PostJobs/ScorePostJob.php index 95fb1206..4a50339c 100644 --- a/src/Api/Jobs/ScorePostJob.php +++ b/src/Api/Jobs/PostJobs/ScorePostJob.php @@ -11,14 +11,19 @@ class ScorePostJob extends AbstractPostJob return $post; } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return JobArgs::ARG_NEW_POST_SCORE; + } + + public function getRequiredPrivileges() { return new Privilege( Privilege::ScorePost, Access::getIdentity($this->post->getUploader())); } - public function requiresAuthentication() + public function isAuthenticationRequired() { return true; } diff --git a/src/Api/Jobs/TogglePostFavoriteJob.php b/src/Api/Jobs/PostJobs/TogglePostFavoriteJob.php similarity index 77% rename from src/Api/Jobs/TogglePostFavoriteJob.php rename to src/Api/Jobs/PostJobs/TogglePostFavoriteJob.php index 986a8c64..29f72d54 100644 --- a/src/Api/Jobs/TogglePostFavoriteJob.php +++ b/src/Api/Jobs/PostJobs/TogglePostFavoriteJob.php @@ -19,14 +19,19 @@ class TogglePostFavoriteJob extends AbstractPostJob return $post; } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return JobArgs::ARG_NEW_STATE; + } + + public function getRequiredPrivileges() { return new Privilege( Privilege::FavoritePost, Access::getIdentity($this->post->getUploader())); } - public function requiresAuthentication() + public function isAuthenticationRequired() { return true; } diff --git a/src/Api/Jobs/TogglePostTagJob.php b/src/Api/Jobs/PostJobs/TogglePostTagJob.php similarity index 87% rename from src/Api/Jobs/TogglePostTagJob.php rename to src/Api/Jobs/PostJobs/TogglePostTagJob.php index 364025dc..efacdd67 100644 --- a/src/Api/Jobs/TogglePostTagJob.php +++ b/src/Api/Jobs/PostJobs/TogglePostTagJob.php @@ -50,7 +50,14 @@ class TogglePostTagJob extends AbstractPostJob return $post; } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return JobArgs::Conjunction( + JobArgs::ARG_TAG_NAME, + Jobargs::ARG_NEW_STATE); + } + + public function getRequiredPrivileges() { return new Privilege( Privilege::EditPostTags, diff --git a/src/Api/Jobs/TogglePostVisibilityJob.php b/src/Api/Jobs/PostJobs/TogglePostVisibilityJob.php similarity index 81% rename from src/Api/Jobs/TogglePostVisibilityJob.php rename to src/Api/Jobs/PostJobs/TogglePostVisibilityJob.php index 7cda94f1..16b52d7a 100644 --- a/src/Api/Jobs/TogglePostVisibilityJob.php +++ b/src/Api/Jobs/PostJobs/TogglePostVisibilityJob.php @@ -19,7 +19,12 @@ class TogglePostVisibilityJob extends AbstractPostJob return $post; } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return JobArgs::ARG_NEW_STATE; + } + + public function getRequiredPrivileges() { return new Privilege( Privilege::HidePost, diff --git a/src/Api/Jobs/PreviewCommentJob.php b/src/Api/Jobs/PreviewCommentJob.php index 599c5199..8ca89485 100644 --- a/src/Api/Jobs/PreviewCommentJob.php +++ b/src/Api/Jobs/PreviewCommentJob.php @@ -26,17 +26,26 @@ class PreviewCommentJob extends AbstractJob return $comment; } - public function requiresPrivilege() + public function getRequiredArguments() + { + return JobArgs::Conjunction( + JobArgs::ARG_NEW_TEXT, + JobArgs::Alternative( + JobArgs::ARG_COMMENT_ID, + JobArgs::ARG_POST_ID)); + } + + public function getRequiredPrivileges() { return new Privilege(Privilege::AddComment); } - public function requiresAuthentication() + public function isAuthenticationRequired() { return false; } - public function requiresConfirmedEmail() + public function isConfirmedEmailRequired() { return getConfig()->registration->needEmailForCommenting; } diff --git a/src/Api/Jobs/RenameTagsJob.php b/src/Api/Jobs/RenameTagsJob.php index 22796e67..3242d3bf 100644 --- a/src/Api/Jobs/RenameTagsJob.php +++ b/src/Api/Jobs/RenameTagsJob.php @@ -15,7 +15,14 @@ class RenameTagsJob extends AbstractJob 'target' => TextHelper::reprTag($targetTag)]); } - public function requiresPrivilege() + public function getRequiredArguments() + { + return JobArgs::Conjunction( + JobArgs::ARG_SOURCE_TAG_NAME, + JobArgs::ARG_TARGET_TAG_NAME); + } + + public function getRequiredPrivileges() { return new Privilege(Privilege::RenameTags); } diff --git a/src/Api/AbstractUserJob.php b/src/Api/Jobs/UserJobs/AbstractUserJob.php similarity index 56% rename from src/Api/AbstractUserJob.php rename to src/Api/Jobs/UserJobs/AbstractUserJob.php index 47ffb547..5bf5cfc6 100644 --- a/src/Api/AbstractUserJob.php +++ b/src/Api/Jobs/UserJobs/AbstractUserJob.php @@ -15,4 +15,16 @@ abstract class AbstractUserJob extends AbstractJob $this->user = UserModel::getByNameOrEmail($userName); } } + + public function getRequiredArguments() + { + return JobArgs::Conjunction( + JobArgs::Alternative( + JobArgs::ARG_USER_NAME, + JobArgs::ARG_USER_EMAIL, + JobArgs::ARG_USER_ENTITY), + $this->getRequiredSubArguments()); + } + + public abstract function getRequiredSubArguments(); } diff --git a/src/Api/Jobs/AcceptUserRegistrationJob.php b/src/Api/Jobs/UserJobs/AcceptUserRegistrationJob.php similarity index 79% rename from src/Api/Jobs/AcceptUserRegistrationJob.php rename to src/Api/Jobs/UserJobs/AcceptUserRegistrationJob.php index ef58bcc4..3fdc2ee4 100644 --- a/src/Api/Jobs/AcceptUserRegistrationJob.php +++ b/src/Api/Jobs/UserJobs/AcceptUserRegistrationJob.php @@ -13,7 +13,12 @@ class AcceptUserRegistrationJob extends AbstractUserJob 'subject' => TextHelper::reprUser($user)]); } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return null; + } + + public function getRequiredPrivileges() { return new Privilege(Privilege::AcceptUserRegistration); } diff --git a/src/Api/Jobs/DeleteUserJob.php b/src/Api/Jobs/UserJobs/DeleteUserJob.php similarity index 80% rename from src/Api/Jobs/DeleteUserJob.php rename to src/Api/Jobs/UserJobs/DeleteUserJob.php index b2d5a223..60b59a87 100644 --- a/src/Api/Jobs/DeleteUserJob.php +++ b/src/Api/Jobs/UserJobs/DeleteUserJob.php @@ -13,7 +13,12 @@ class DeleteUserJob extends AbstractUserJob 'subject' => TextHelper::reprUser($name)]); } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return null; + } + + public function getRequiredPrivileges() { return new Privilege( Privilege::DeleteUser, diff --git a/src/Api/Jobs/EditUserAccessRankJob.php b/src/Api/Jobs/UserJobs/EditUserAccessRankJob.php similarity index 85% rename from src/Api/Jobs/EditUserAccessRankJob.php rename to src/Api/Jobs/UserJobs/EditUserAccessRankJob.php index f8398236..755eeee0 100644 --- a/src/Api/Jobs/EditUserAccessRankJob.php +++ b/src/Api/Jobs/UserJobs/EditUserAccessRankJob.php @@ -1,11 +1,6 @@ hasArgument(JobArgs::ARG_NEW_ACCESS_RANK); - } - public function execute() { $user = $this->user; @@ -28,7 +23,12 @@ class EditUserAccessRankJob extends AbstractUserJob return $user; } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return JobArgs::ARG_NEW_ACCESS_RANK; + } + + public function getRequiredPrivileges() { return new Privilege( Privilege::ChangeUserAccessRank, diff --git a/src/Api/Jobs/EditUserEmailJob.php b/src/Api/Jobs/UserJobs/EditUserEmailJob.php similarity index 91% rename from src/Api/Jobs/EditUserEmailJob.php rename to src/Api/Jobs/UserJobs/EditUserEmailJob.php index 6eb64418..aecd3120 100644 --- a/src/Api/Jobs/EditUserEmailJob.php +++ b/src/Api/Jobs/UserJobs/EditUserEmailJob.php @@ -1,11 +1,6 @@ hasArgument(JobArgs::ARG_NEW_EMAIL); - } - public function execute() { if (getConfig()->registration->needEmailForRegistering) @@ -49,7 +44,12 @@ class EditUserEmailJob extends AbstractUserJob } } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return JobArgs::ARG_NEW_EMAIL; + } + + public function getRequiredPrivileges() { return new Privilege( $this->getContext() == self::CONTEXT_BATCH_ADD diff --git a/src/Api/Jobs/EditUserJob.php b/src/Api/Jobs/UserJobs/EditUserJob.php similarity index 91% rename from src/Api/Jobs/EditUserJob.php rename to src/Api/Jobs/UserJobs/EditUserJob.php index be08f19a..3110583a 100644 --- a/src/Api/Jobs/EditUserJob.php +++ b/src/Api/Jobs/UserJobs/EditUserJob.php @@ -65,7 +65,12 @@ class EditUserJob extends AbstractUserJob return $user; } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return null; + } + + public function getRequiredPrivileges() { return false; } diff --git a/src/Api/Jobs/EditUserNameJob.php b/src/Api/Jobs/UserJobs/EditUserNameJob.php similarity index 85% rename from src/Api/Jobs/EditUserNameJob.php rename to src/Api/Jobs/UserJobs/EditUserNameJob.php index 85eee44f..9a92b212 100644 --- a/src/Api/Jobs/EditUserNameJob.php +++ b/src/Api/Jobs/UserJobs/EditUserNameJob.php @@ -1,11 +1,6 @@ hasArgument(JobArgs::ARG_NEW_USER_NAME); - } - public function execute() { $user = $this->user; @@ -28,7 +23,12 @@ class EditUserNameJob extends AbstractUserJob return $user; } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return JobArgs::ARG_NEW_USER_NAME; + } + + public function getRequiredPrivileges() { return new Privilege( $this->getContext() == self::CONTEXT_BATCH_ADD diff --git a/src/Api/Jobs/EditUserPasswordJob.php b/src/Api/Jobs/UserJobs/EditUserPasswordJob.php similarity index 86% rename from src/Api/Jobs/EditUserPasswordJob.php rename to src/Api/Jobs/UserJobs/EditUserPasswordJob.php index ecdc1626..c3ab4687 100644 --- a/src/Api/Jobs/EditUserPasswordJob.php +++ b/src/Api/Jobs/UserJobs/EditUserPasswordJob.php @@ -1,11 +1,6 @@ hasArgument(JobArgs::ARG_NEW_PASSWORD); - } - public function execute() { $user = $this->user; @@ -27,7 +22,12 @@ class EditUserPasswordJob extends AbstractUserJob return $user; } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return JobArgs::ARG_NEW_PASSWORD; + } + + public function getRequiredPrivileges() { return new Privilege( $this->getContext() == self::CONTEXT_BATCH_ADD diff --git a/src/Api/Jobs/FlagUserJob.php b/src/Api/Jobs/UserJobs/FlagUserJob.php similarity index 85% rename from src/Api/Jobs/FlagUserJob.php rename to src/Api/Jobs/UserJobs/FlagUserJob.php index d969bf9c..3911fdbc 100644 --- a/src/Api/Jobs/FlagUserJob.php +++ b/src/Api/Jobs/UserJobs/FlagUserJob.php @@ -19,7 +19,12 @@ class FlagUserJob extends AbstractUserJob return $user; } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return null; + } + + public function getRequiredPrivileges() { return new Privilege( Privilege::FlagUser, diff --git a/src/Api/Jobs/GetUserJob.php b/src/Api/Jobs/UserJobs/GetUserJob.php similarity index 65% rename from src/Api/Jobs/GetUserJob.php rename to src/Api/Jobs/UserJobs/GetUserJob.php index b7022a16..9f3e5e6d 100644 --- a/src/Api/Jobs/GetUserJob.php +++ b/src/Api/Jobs/UserJobs/GetUserJob.php @@ -6,7 +6,12 @@ class GetUserJob extends AbstractUserJob return $this->user; } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return null; + } + + public function getRequiredPrivileges() { return new Privilege( Privilege::ViewUser, diff --git a/src/Api/Jobs/ToggleUserBanJob.php b/src/Api/Jobs/UserJobs/ToggleUserBanJob.php similarity index 82% rename from src/Api/Jobs/ToggleUserBanJob.php rename to src/Api/Jobs/UserJobs/ToggleUserBanJob.php index ec1434c7..76c2897e 100644 --- a/src/Api/Jobs/ToggleUserBanJob.php +++ b/src/Api/Jobs/UserJobs/ToggleUserBanJob.php @@ -22,7 +22,12 @@ class ToggleUserBanJob extends AbstractUserJob return $user; } - public function requiresPrivilege() + public function getRequiredSubArguments() + { + return JobArgs::ARG_NEW_STATE; + } + + public function getRequiredPrivileges() { return new Privilege( Privilege::BanUser, diff --git a/src/Controllers/UserController.php b/src/Controllers/UserController.php index 65196406..0858661c 100644 --- a/src/Controllers/UserController.php +++ b/src/Controllers/UserController.php @@ -17,13 +17,11 @@ class UserController $context->transport->paginator = $ret; } - public function genericView($name, $tab = 'favs', $page = 1) + public function genericView($identifier, $tab = 'favs', $page = 1) { $user = Api::run( new GetUserJob(), - [ - JobArgs::ARG_USER_NAME => $name, - ]); + $this->appendUserIdentifierArgument([], $identifier)); $flagged = in_array(TextHelper::reprUser($user), SessionHelper::get('flagged', [])); @@ -69,9 +67,9 @@ class UserController } } - public function settingsAction($name) + public function settingsAction($identifier) { - $this->genericView($name, 'settings'); + $this->genericView($identifier, 'settings'); $user = getContext()->transport->user; @@ -97,9 +95,9 @@ class UserController Messenger::message('Browsing settings updated!'); } - public function editAction($name) + public function editAction($identifier) { - $this->genericView($name, 'edit'); + $this->genericView($identifier, 'edit'); $this->requirePasswordConfirmation(); if (InputHelper::get('password1') != InputHelper::get('password2')) @@ -107,12 +105,12 @@ class UserController $args = [ - JobArgs::ARG_USER_NAME => $name, JobArgs::ARG_NEW_USER_NAME => InputHelper::get('name'), JobArgs::ARG_NEW_PASSWORD => InputHelper::get('password1'), JobArgs::ARG_NEW_EMAIL => InputHelper::get('email'), JobArgs::ARG_NEW_ACCESS_RANK => InputHelper::get('access-rank'), ]; + $args = $this->appendUserIdentifierArgument($args, $identifier); $args = array_filter($args); $user = Api::run(new EditUserJob(), $args); @@ -127,13 +125,14 @@ class UserController Messenger::message($message); } - public function deleteAction($name) + public function deleteAction($identifier) { - $this->genericView($name, 'delete'); + $this->genericView($identifier, 'delete'); $this->requirePasswordConfirmation(); - Api::run(new DeleteUserJob(), [ - JobArgs::ARG_USER_NAME => $name]); + Api::run( + new DeleteUserJob(), + $this->appendUserIdentifierArgument([], $identifier)); $user = UserModel::tryGetById(Auth::getCurrentUser()->getId()); if (!$user) @@ -143,29 +142,36 @@ class UserController exit; } - public function flagAction($name) + public function flagAction($identifier) { - Api::run(new FlagUserJob(), [JobArgs::ARG_USER_NAME => $name]); + Api::run( + new FlagUserJob(), + $this->appendUserIdentifierArgument([], $identifier)); } - public function banAction($name) + public function banAction($identifier) { - Api::run(new ToggleUserBanJob(), [ - JobArgs::ARG_USER_NAME => $name, - JobArgs::ARG_NEW_STATE => true]); + Api::run( + new ToggleUserBanJob(), + $this->appendUserIdentifierArgument([ + JobArgs::ARG_NEW_STATE => true + ], $identifier)); } - public function unbanAction($name) + public function unbanAction($identifier) { - Api::run(new ToggleUserBanJob(), [ - JobArgs::ARG_USER_NAME => $name, - JobArgs::ARG_NEW_STATE => false]); + Api::run( + new ToggleUserBanJob(), + $this->appendUserIdentifierArgument([ + JobArgs::ARG_NEW_STATE => true + ], $identifier)); } - public function acceptRegistrationAction($name) + public function acceptRegistrationAction($identifier) { - Api::run(new AcceptUserRegistrationJob(), [ - JobArgs::ARG_USER_NAME => $name]); + Api::run( + new AcceptUserRegistrationJob(), + $this->appendUserIdentifierArgument([], $identifier)); } public function toggleSafetyAction($safety) @@ -243,17 +249,20 @@ class UserController $context = getContext(); $context->viewName = 'message'; Assets::setSubTitle('account activation'); - $name = InputHelper::get('name'); + $identifier = InputHelper::get('identifier'); if (empty($tokenText)) { - Api::run(new ActivateUserEmailJob(), [ JobArgs::ARG_USER_NAME => $name ]); + Api::run( + new ActivateUserEmailJob(), + $this->appendUserIdentifierArgument([], $identifier)); Messenger::message('Activation e-mail resent.'); } else { - $user = Api::run(new ActivateUserEmailJob(), [ JobArgs::ARG_TOKEN => $tokenText ]); + $user = Api::run(new ActivateUserEmailJob(), [ + JobArgs::ARG_TOKEN => $tokenText ]); $message = 'Activation completed successfully.'; if (getConfig()->registration->staffActivation) @@ -277,11 +286,13 @@ class UserController $context = getContext(); $context->viewName = 'message'; Assets::setSubTitle('password reset'); - $name = InputHelper::get('name'); + $identifier = InputHelper::get('identifier'); if (empty($tokenText)) { - Api::run(new PasswordResetJob(), [ JobArgs::ARG_USER_NAME => $name ]); + Api::run( + new PasswordResetJob(), + $this->appendUserIdentifierArgument([], $identifier)); Messenger::message('E-mail sent. Follow instructions to reset password.'); } @@ -308,4 +319,13 @@ class UserController throw new SimpleException('Must supply valid password'); } } + + private function appendUserIdentifierArgument(array $arguments, $userIdentifier) + { + if (strpos($userIdentifier, '@') !== false) + $arguments[JobArgs::ARG_USER_EMAIL] = $userIdentifier; + else + $arguments[JobArgs::ARG_USER_NAME] = $userIdentifier; + return $arguments; + } } diff --git a/src/Views/comment-small.phtml b/src/Views/comment-small.phtml index ef0ac062..5c1f0a84 100644 --- a/src/Views/comment-small.phtml +++ b/src/Views/comment-small.phtml @@ -8,7 +8,9 @@ Assets::addScript('comment-edit.js');
context->comment->getCommenter() ?> - + <?= $commenter->getName() ?> @@ -24,7 +26,7 @@ Assets::addScript('comment-edit.js'); + ['identifier' => $commenter->getName()]) ?>"> getName() ?> diff --git a/src/Views/post-view.phtml b/src/Views/post-view.phtml index 1490e041..5e5e0c9f 100644 --- a/src/Views/post-view.phtml +++ b/src/Views/post-view.phtml @@ -95,7 +95,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0; + ['identifier' => $uploader->getName()]) ?>"> <?= $uploader->getName() ?> @@ -245,7 +245,7 @@ $canEditAnything = count(array_filter($editPostPrivileges)) > 0;
  • + ['identifier' => $user->getName()]) ?>"> <?= $user->getName() ?>
  • diff --git a/src/Views/static-main.phtml b/src/Views/static-main.phtml index 6b646e3f..215d9ecf 100644 --- a/src/Views/static-main.phtml +++ b/src/Views/static-main.phtml @@ -43,7 +43,7 @@ Assets::addStylesheet('static-main.css'); by + ['identifier' => $this->context->featuredPostUser->getName()]) ?>"> context->featuredPostUser->getName() ?> , diff --git a/src/Views/top-navigation.phtml b/src/Views/top-navigation.phtml index 09f45d23..1c15ee1e 100644 --- a/src/Views/top-navigation.phtml +++ b/src/Views/top-navigation.phtml @@ -56,8 +56,8 @@ 'Users', \Chibi\Router::linkTo(['UserController', 'listView']), $activeController == 'user' and $activeAction != 'registration' and - (!isset($this->context->route->arguments['name']) or - $this->context->route->arguments['name'] != Auth::getCurrentUser()->getName())); + (!isset($this->context->route->arguments['identifier']) or + $this->context->route->arguments['identifier'] != Auth::getCurrentUser()->getName())); } if (!Auth::isLoggedIn()) @@ -76,9 +76,9 @@ { $registerNavItem( 'My account', - \Chibi\Router::linkTo(['UserController', 'genericView'], ['name' => Auth::getCurrentUser()->getName()]), - $activeController == 'user' and isset($this->context->route->arguments['name']) and - $this->context->route->arguments['name'] == Auth::getCurrentUser()->getName()); + \Chibi\Router::linkTo(['UserController', 'genericView'], ['identifier' => Auth::getCurrentUser()->getName()]), + $activeController == 'user' and isset($this->context->route->arguments['identifier']) and + $this->context->route->arguments['identifier'] == Auth::getCurrentUser()->getName()); $registerNavItem( 'Log out', @@ -119,7 +119,7 @@ TextCaseConverter::CAMEL_CASE, TextCaseConverter::SPINAL_CASE) ?>"> - $this->context->transport->user->getName()]) ?>" method="post" class="delete confirmable" autocomplete="off" diff --git a/src/Views/user-edit.phtml b/src/Views/user-edit.phtml index 0fd3b809..4fab1695 100644 --- a/src/Views/user-edit.phtml +++ b/src/Views/user-edit.phtml @@ -1,7 +1,7 @@
    $this->context->transport->user->getName()]) ?>" method="post" class="edit" autocomplete="off"> diff --git a/src/Views/user-list.phtml b/src/Views/user-list.phtml index cbc06eb5..3fca6dbe 100644 --- a/src/Views/user-list.phtml +++ b/src/Views/user-list.phtml @@ -44,14 +44,14 @@ if (Auth::getCurrentUser()->hasEnabledEndlessScrolling())
    + ['identifier' => $user->getName()]) ?>"> <?= $user->getName() ?>

    + ['identifier' => $user->getName()]) ?>"> getName() ?>

    diff --git a/src/Views/user-select.phtml b/src/Views/user-select.phtml index 1ad7c546..d6a67d68 100644 --- a/src/Views/user-select.phtml +++ b/src/Views/user-select.phtml @@ -13,7 +13,7 @@
    - +
    diff --git a/src/Views/user-settings.phtml b/src/Views/user-settings.phtml index 99a79498..046a6745 100644 --- a/src/Views/user-settings.phtml +++ b/src/Views/user-settings.phtml @@ -1,7 +1,7 @@ $this->context->transport->user->getName()]) ?>" method="post" class="settings"> diff --git a/src/Views/user-view.phtml b/src/Views/user-view.phtml index 99c046a6..158f6f22 100644 --- a/src/Views/user-view.phtml +++ b/src/Views/user-view.phtml @@ -7,7 +7,7 @@ Assets::addStylesheet('user-view.css');
    + ['identifier' => $this->context->transport->user->getName()]) ?>"> 'Edit account settings', 'link' => \Chibi\Router::linkTo( ['UserController', 'editAction'], - ['name' => $this->context->transport->user->getName(), 'tab' => 'edit']), + ['identifier' => $this->context->transport->user->getName(), 'tab' => 'edit']), ]; } @@ -112,7 +112,7 @@ Assets::addStylesheet('user-view.css'); 'text' => 'Accept registration', 'simple-action' => \Chibi\Router::linkTo( ['UserController', 'acceptRegistrationAction'], - ['name' => $this->context->transport->user->getName()]), + ['identifier' => $this->context->transport->user->getName()]), ]; } @@ -137,7 +137,7 @@ Assets::addStylesheet('user-view.css'); 'text' => 'Flag for moderator attention', 'simple-action' => \Chibi\Router::linkTo( ['UserController', 'flagAction'], - ['name' => $this->context->transport->user->getName()]), + ['identifier' => $this->context->transport->user->getName()]), 'data-confirm-text' => 'Are you sure you want to flag this user?', ]; } @@ -155,7 +155,7 @@ Assets::addStylesheet('user-view.css'); 'text' => 'Ban user', 'simple-action' => \Chibi\Router::linkTo( ['UserController', 'banAction'], - ['name' => $this->context->transport->user->getName()]), + ['identifier' => $this->context->transport->user->getName()]), 'data-confirm-text' => 'Are you sure you want to ban this user?', ]; } @@ -167,7 +167,7 @@ Assets::addStylesheet('user-view.css'); 'text' => 'Unban user', 'simple-action' => \Chibi\Router::linkTo( ['UserController', 'unbanAction'], - ['name' => $this->context->transport->user->getName()]), + ['identifier' => $this->context->transport->user->getName()]), 'data-confirm-text' => 'Are you sure you want to unban this user?', ]; } @@ -183,7 +183,7 @@ Assets::addStylesheet('user-view.css'); 'text' => 'Delete account', 'link' => \Chibi\Router::linkTo( ['UserController', 'deleteAction'], - ['name' => $this->context->transport->user->getName(), 'tab' => 'delete']), + ['identifier' => $this->context->transport->user->getName(), 'tab' => 'delete']), ]; } @@ -202,7 +202,7 @@ Assets::addStylesheet('user-view.css'); Favs @@ -216,7 +216,7 @@ Assets::addStylesheet('user-view.css'); Uploads @@ -234,7 +234,7 @@ Assets::addStylesheet('user-view.css'); Browsing settings @@ -249,7 +249,7 @@ Assets::addStylesheet('user-view.css'); Account settings @@ -266,7 +266,7 @@ Assets::addStylesheet('user-view.css'); Delete account diff --git a/src/routes.php b/src/routes.php index e333f4d7..d8bc281e 100644 --- a/src/routes.php +++ b/src/routes.php @@ -88,7 +88,7 @@ $tagValidation = $userValidation = [ - 'name' => '[^\/]+', + 'identifier' => '[^\/]+', 'page' => '\d*', 'tab' => 'favs|uploads|settings|edit|delete', 'filter' => '[^\/]+', @@ -97,8 +97,8 @@ $userValidation = \Chibi\Router::register(['UserController', 'listView'], 'GET', '/users', $userValidation); \Chibi\Router::register(['UserController', 'listView'], 'GET', '/users/{page}', $userValidation); \Chibi\Router::register(['UserController', 'listView'], 'GET', '/users/{filter}/{page}', $userValidation); -\Chibi\Router::register(['UserController', 'genericView'], 'GET', '/user/{name}/{tab}', $userValidation); -\Chibi\Router::register(['UserController', 'genericView'], 'GET', '/user/{name}/{tab}/{page}', $userValidation); +\Chibi\Router::register(['UserController', 'genericView'], 'GET', '/user/{identifier}/{tab}', $userValidation); +\Chibi\Router::register(['UserController', 'genericView'], 'GET', '/user/{identifier}/{tab}/{page}', $userValidation); \Chibi\Router::register(['UserController', 'registrationView'], 'GET', '/register', $userValidation); \Chibi\Router::register(['UserController', 'registrationAction'], 'POST', '/register', $userValidation); @@ -110,11 +110,11 @@ $userValidation = \Chibi\Router::register(['UserController', 'passwordResetAction'], 'POST', '/password-reset', $userValidation); \Chibi\Router::register(['UserController', 'passwordResetAction'], 'GET', '/password-reset/{tokenText}', $userValidation); -\Chibi\Router::register(['UserController', 'flagAction'], 'POST', '/user/{name}/flag', $userValidation); -\Chibi\Router::register(['UserController', 'banAction'], 'POST', '/user/{name}/ban', $userValidation); -\Chibi\Router::register(['UserController', 'unbanAction'], 'POST', '/user/{name}/unban', $userValidation); -\Chibi\Router::register(['UserController', 'acceptRegistrationAction'], 'POST', '/user/{name}/accept-registration', $userValidation); -\Chibi\Router::register(['UserController', 'deleteAction'], 'POST', '/user/{name}/delete', $userValidation); -\Chibi\Router::register(['UserController', 'settingsAction'], 'POST', '/user/{name}/settings', $userValidation); -\Chibi\Router::register(['UserController', 'editAction'], 'POST', '/user/{name}/edit', $userValidation); +\Chibi\Router::register(['UserController', 'flagAction'], 'POST', '/user/{identifier}/flag', $userValidation); +\Chibi\Router::register(['UserController', 'banAction'], 'POST', '/user/{identifier}/ban', $userValidation); +\Chibi\Router::register(['UserController', 'unbanAction'], 'POST', '/user/{identifier}/unban', $userValidation); +\Chibi\Router::register(['UserController', 'acceptRegistrationAction'], 'POST', '/user/{identifier}/accept-registration', $userValidation); +\Chibi\Router::register(['UserController', 'deleteAction'], 'POST', '/user/{identifier}/delete', $userValidation); +\Chibi\Router::register(['UserController', 'settingsAction'], 'POST', '/user/{identifier}/settings', $userValidation); +\Chibi\Router::register(['UserController', 'editAction'], 'POST', '/user/{identifier}/edit', $userValidation); \Chibi\Router::register(['UserController', 'toggleSafetyAction'], 'POST', '/user/toggle-safety/{safety}', $userValidation); diff --git a/tests/Api/ApiArgumentTest.php b/tests/Api/ApiArgumentTest.php new file mode 100644 index 00000000..55847140 --- /dev/null +++ b/tests/Api/ApiArgumentTest.php @@ -0,0 +1,367 @@ +testArguments(new AcceptUserRegistrationJob(), + $this->getUserSelector()); + } + + public function testActivateUserEmailJob() + { + $this->testArguments(new ActivateUserEmailJob(), + JobArgs::Alternative( + JobArgs::ARG_TOKEN, + $this->getUserSelector())); + } + + public function testAddPostJob() + { + $this->testArguments(new AddPostJob(), + JobArgs::Optional( + JobArgs::ARG_ANONYMOUS)); + } + + public function testAddCommentJob() + { + $this->testArguments(new AddCommentJob(), + JobArgs::Conjunction( + $this->getPostSelector(), + JobArgs::ARG_NEW_TEXT)); + } + + public function testAddUserJob() + { + $this->testArguments(new AddUserJob(), null); + } + + public function testDeletePostJob() + { + $this->testArguments(new DeletePostJob(), + $this->getPostSelector()); + } + + public function testDeleteCommentJob() + { + $this->testArguments(new DeleteCommentJob(), + JobArgs::Alternative( + JobArgs::ARG_COMMENT_ENTITY, + JobArgs::ARG_COMMENT_ID)); + } + + public function testDeleteUserJob() + { + $this->testArguments(new DeleteUserJob(), + $this->getUserSelector()); + } + + public function testEditCommentJob() + { + $this->testArguments(new EditCommentJob(), + JobArgs::Conjunction( + JobArgs::ARG_NEW_TEXT, + JobArgs::Alternative( + JobArgs::ARG_COMMENT_ENTITY, + JobArgs::ARG_COMMENT_ID))); + } + + public function testEditPostContentJob() + { + $this->testArguments(new EditPostContentJob(), + JobArgs::Conjunction( + $this->getPostSelector(), + JobArgs::Alternative( + JobArgs::ARG_NEW_POST_CONTENT, + JobArgs::ARG_NEW_POST_CONTENT_URL))); + } + + public function testEditPostJob() + { + $this->testArguments(new EditPostJob(), + $this->getPostSelector()); + } + + public function testEditPostRelationsJob() + { + $this->testArguments(new EditPostRelationsJob(), + JobArgs::Conjunction( + $this->getPostSelector(), + JobArgs::ARG_NEW_RELATED_POST_IDS)); + } + + public function testEditPostSafetyJob() + { + $this->testArguments(new EditPostSafetyJob(), + JobArgs::Conjunction( + $this->getPostSelector(), + JobArgs::ARG_NEW_SAFETY)); + } + + public function testEditPostSourceJob() + { + $this->testArguments(new EditPostSourceJob(), + JobArgs::Conjunction( + $this->getPostSelector(), + JobArgs::ARG_NEW_SOURCE)); + } + + public function testEditPostTagsJob() + { + $this->testArguments(new EditPostTagsJob(), + JobArgs::Conjunction( + $this->getPostSelector(), + JobArgs::ARG_NEW_TAG_NAMES)); + } + + public function testEditPostThumbJob() + { + $this->testArguments(new EditPostThumbJob(), + JobArgs::Conjunction( + $this->getPostSelector(), + JobArgs::ARG_NEW_THUMB_CONTENT)); + } + + public function testEditUserAccessRankJob() + { + $this->testArguments(new EditUserAccessRankJob(), + JobArgs::Conjunction( + $this->getUserSelector(), + JobArgs::ARG_NEW_ACCESS_RANK)); + } + + public function testEditUserEmailJob() + { + $this->testArguments(new EditUserEmailJob(), + JobArgs::Conjunction( + $this->getUserSelector(), + JobArgs::ARG_NEW_EMAIL)); + } + + public function testEditUserJob() + { + $this->testArguments(new EditUserJob(), + $this->getUserSelector()); + } + + public function testEditUserNameJob() + { + $this->testArguments(new EditUserNameJob(), + JobArgs::Conjunction( + $this->getUserSelector(), + JobArgs::ARG_NEW_USER_NAME)); + } + + public function testEditUserPasswordJob() + { + $this->testArguments(new EditUserPasswordJob(), + JobArgs::Conjunction( + $this->getUserSelector(), + JobArgs::ARG_NEW_PASSWORD)); + } + + public function testFeaturePostJob() + { + $this->testArguments(new FeaturePostJob(), + JobArgs::Conjunction( + JobArgs::Optional(JobArgs::ARG_ANONYMOUS), + $this->getPostSelector())); + } + + public function testFlagPostJob() + { + $this->testArguments(new FlagPostJob(), + $this->getPostSelector()); + } + + public function testFlagUserJob() + { + $this->testArguments(new FlagUserJob(), + $this->getUserSelector()); + } + + public function testGetLogJob() + { + $this->testArguments(new GetLogJob(), + JobArgs::Conjunction( + JobArgs::ARG_QUERY, + JobArgs::ARG_LOG_ID, + JobArgs::ARG_PAGE_NUMBER)); + } + + public function testGetPostContentJob() + { + $this->testArguments(new GetPostContentJob(), + $this->getPostSafeSelector()); + } + + public function testGetPostJob() + { + $this->testArguments(new GetPostJob(), + $this->getPostSelector()); + } + + public function testGetPostThumbJob() + { + $this->testArguments(new GetPostThumbJob(), + JobArgs::Conjunction( + JobArgs::Optional(JobArgs::ARG_THUMB_WIDTH), + JobArgs::Optional(JobArgs::ARG_THUMB_HEIGHT), + $this->getPostSafeSelector())); + } + + public function testGetUserJob() + { + $this->testArguments(new GetUserJob(), + $this->getUserSelector()); + } + + public function testListCommentsJob() + { + $this->testArguments(new ListCommentsJob(), + JobArgs::ARG_PAGE_NUMBER); + } + + public function testListLogsJob() + { + $this->testArguments(new ListLogsJob(), + null); + } + + public function testListPostsJob() + { + $this->testArguments(new ListPostsJob(), + JobArgs::Conjunction( + JobArgs::ARG_QUERY, + JobArgs::ARG_PAGE_NUMBER)); + } + + public function testListRelatedTagsJob() + { + $this->testArguments(new ListRelatedTagsJob(), + JobArgs::Conjunction( + JobArgs::Optional(JobArgs::ARG_TAG_NAMES), + JobArgs::ARG_TAG_NAME, + JobArgs::ARG_QUERY, + JobArgs::ARG_PAGE_NUMBER)); + } + + public function testListTagsJob() + { + $this->testArguments(new ListTagsJob(), + JobArgs::Conjunction( + JobArgs::ARG_QUERY, + JobArgs::ARG_PAGE_NUMBER)); + } + + public function testListUsersJob() + { + $this->testArguments(new ListUsersJob(), + JobArgs::Conjunction( + JobArgs::ARG_QUERY, + JobArgs::ARG_PAGE_NUMBER)); + } + + public function testMergeTagsJob() + { + $this->testArguments(new MergeTagsJob(), + JobArgs::Conjunction( + JobArgs::ARG_SOURCE_TAG_NAME, + JobArgs::ARG_TARGET_TAG_NAME)); + } + + public function testRenameTagsJob() + { + $this->testArguments(new RenameTagsJob(), + JobArgs::Conjunction( + JobArgs::ARG_SOURCE_TAG_NAME, + JobArgs::ARG_TARGET_TAG_NAME)); + } + + public function testPasswordResetJob() + { + $this->testArguments(new PasswordResetJob(), + JobArgs::Alternative( + JobArgs::ARG_TOKEN, + $this->getUserSelector())); + } + + public function testPreviewCommentJob() + { + $this->testArguments(new PreviewCommentJob(), + JobArgs::Conjunction( + JobArgs::Alternative( + JobArgs::ARG_POST_ID, + JobArgs::ARG_COMMENT_ID), + JobArgs::ARG_NEW_TEXT)); + } + + public function testScorePostJob() + { + $this->testArguments(new ScorePostJob(), + JobArgs::Conjunction( + $this->getPostSelector(), + JobArgs::ARG_NEW_POST_SCORE)); + } + + public function testTogglePostFavoriteJob() + { + $this->testArguments(new TogglePostFavoriteJob(), + JobArgs::Conjunction( + $this->getPostSelector(), + JobArgs::ARG_NEW_STATE)); + } + + public function testTogglePostTagJob() + { + $this->testArguments(new TogglePostTagJob(), + JobArgs::Conjunction( + $this->getPostSelector(), + JobArgs::ARG_TAG_NAME, + JobArgs::ARG_NEW_STATE)); + } + + public function testTogglePostVisibilityJob() + { + $this->testArguments(new TogglePostVisibilityJob(), + JobArgs::Conjunction( + $this->getPostSelector(), + JobArgs::ARG_NEW_STATE)); + } + + public function testToggleUserBanJob() + { + $this->testArguments(new ToggleUserBanJob(), + JobArgs::Conjunction( + $this->getUserSelector(), + JobArgs::ARG_NEW_STATE)); + } + + protected function testArguments($job, $arguments) + { + $this->testedJobs []= $job; + $this->assert->areEquivalent($arguments, $job->getRequiredArguments()); + } + + protected function getPostSelector() + { + return JobArgs::Alternative( + JobArgs::ARG_POST_ENTITY, + JobArgs::ARG_POST_ID, + JobArgs::ARG_POST_NAME); + } + + protected function getPostSafeSelector() + { + return JobArgs::Alternative( + JobArgs::ARG_POST_NAME, + JobArgs::ARG_POST_ENTITY); + } + + protected function getUserSelector() + { + return JobArgs::Alternative( + JobArgs::ARG_USER_EMAIL, + JobArgs::ARG_USER_ENTITY, + JobArgs::ARG_USER_NAME); + } +} diff --git a/tests/Api/ApiAuthTest.php b/tests/Api/ApiAuthTest.php index 5b810737..c963c30b 100644 --- a/tests/Api/ApiAuthTest.php +++ b/tests/Api/ApiAuthTest.php @@ -52,7 +52,7 @@ class ApiAuthTest extends AbstractFullApiTest protected function testAuth($job, $expectedAuth) { $this->testedJobs []= $job; - $this->assert->areEqual($expectedAuth, $job->requiresAuthentication()); + $this->assert->areEqual($expectedAuth, $job->isAuthenticationRequired()); } public function testAuthEnforcing() diff --git a/tests/Api/ApiEmailRequirementsTest.php b/tests/Api/ApiEmailRequirementsTest.php index f7429019..624f480b 100644 --- a/tests/Api/ApiEmailRequirementsTest.php +++ b/tests/Api/ApiEmailRequirementsTest.php @@ -52,7 +52,7 @@ class ApiEmailRequirementsTest extends AbstractFullApiTest protected function testRegularEmailRequirement($job) { $this->testedJobs []= $job; - $this->assert->areEqual(false, $job->requiresConfirmedEmail()); + $this->assert->areEqual(false, $job->isConfirmedEmailRequired()); } public function testCommentsEmailRequirements() @@ -66,10 +66,10 @@ class ApiEmailRequirementsTest extends AbstractFullApiTest $this->testedJobs []= $job; getConfig()->registration->needEmailForCommenting = false; - $this->assert->areEqual(false, $job->requiresConfirmedEmail()); + $this->assert->areEqual(false, $job->isConfirmedEmailRequired()); getConfig()->registration->needEmailForCommenting = true; - $this->assert->areEqual(true, $job->requiresConfirmedEmail()); + $this->assert->areEqual(true, $job->isConfirmedEmailRequired()); } public function testPostingEmailRequirement() @@ -79,10 +79,10 @@ class ApiEmailRequirementsTest extends AbstractFullApiTest $this->testedJobs []= $job; getConfig()->registration->needEmailForUploading = false; - $this->assert->areEqual(false, $job->requiresConfirmedEmail()); + $this->assert->areEqual(false, $job->isConfirmedEmailRequired()); getConfig()->registration->needEmailForUploading = true; - $this->assert->areEqual(true, $job->requiresConfirmedEmail()); + $this->assert->areEqual(true, $job->isConfirmedEmailRequired()); } public function testEnforcing() diff --git a/tests/Api/ApiPrivilegeTest.php b/tests/Api/ApiPrivilegeTest.php index 09454ea0..c50d5624 100644 --- a/tests/Api/ApiPrivilegeTest.php +++ b/tests/Api/ApiPrivilegeTest.php @@ -33,7 +33,7 @@ class ApiPrivilegeTest extends AbstractFullApiTest protected function testRegularPrivilege($job, $expectedPrivilege) { $this->testedJobs []= $job; - $this->assert->areEquivalent($expectedPrivilege, $job->requiresPrivilege()); + $this->assert->areEquivalent($expectedPrivilege, $job->getRequiredPrivileges()); } public function testDynamicPostPrivileges() @@ -80,12 +80,12 @@ class ApiPrivilegeTest extends AbstractFullApiTest $expectedPrivilege->secondary = 'all'; $job->setArgument(JobArgs::ARG_POST_ID, $otherPost->getId()); $job->prepare(); - $this->assert->areEquivalent($expectedPrivilege, $job->requiresPrivilege()); + $this->assert->areEquivalent($expectedPrivilege, $job->getRequiredPrivileges()); $expectedPrivilege->secondary = 'own'; $job->setArgument(JobArgs::ARG_POST_ID, $ownPost->getId()); $job->prepare(); - $this->assert->areEquivalent($expectedPrivilege, $job->requiresPrivilege()); + $this->assert->areEquivalent($expectedPrivilege, $job->getRequiredPrivileges()); } public function testDynamicPostRetrievalPrivileges() @@ -110,7 +110,7 @@ class ApiPrivilegeTest extends AbstractFullApiTest $job->prepare(); $this->assert->areEquivalent([ new Privilege(Privilege::ViewPost, 'hidden'), - new Privilege(Privilege::ViewPost, 'safe')], $job->requiresPrivilege()); + new Privilege(Privilege::ViewPost, 'safe')], $job->getRequiredPrivileges()); } } @@ -118,7 +118,7 @@ class ApiPrivilegeTest extends AbstractFullApiTest { $job = new GetPostThumbJob(); $this->testedJobs []= $job; - $this->assert->areEquivalent(false, $job->requiresPrivilege()); + $this->assert->areEquivalent(false, $job->getRequiredPrivileges()); } public function testDynamicUserPrivileges() @@ -149,12 +149,12 @@ class ApiPrivilegeTest extends AbstractFullApiTest $expectedPrivilege->secondary = 'own'; $job->setArgument(JobArgs::ARG_USER_NAME, $ownUser->getName()); $job->prepare(); - $this->assert->areEquivalent($expectedPrivilege, $job->requiresPrivilege()); + $this->assert->areEquivalent($expectedPrivilege, $job->getRequiredPrivileges()); $expectedPrivilege->secondary = 'all'; $job->setArgument(JobArgs::ARG_USER_NAME, $otherUser->getName()); $job->prepare(); - $this->assert->areEquivalent($expectedPrivilege, $job->requiresPrivilege()); + $this->assert->areEquivalent($expectedPrivilege, $job->getRequiredPrivileges()); } public function testDynamicCommentPrivileges() @@ -175,12 +175,12 @@ class ApiPrivilegeTest extends AbstractFullApiTest $expectedPrivilege->secondary = 'own'; $job->setArgument(JobArgs::ARG_COMMENT_ID, $ownComment->getId()); $job->prepare(); - $this->assert->areEquivalent($expectedPrivilege, $job->requiresPrivilege()); + $this->assert->areEquivalent($expectedPrivilege, $job->getRequiredPrivileges()); $expectedPrivilege->secondary = 'all'; $job->setArgument(JobArgs::ARG_COMMENT_ID, $otherComment->getId()); $job->prepare(); - $this->assert->areEquivalent($expectedPrivilege, $job->requiresPrivilege()); + $this->assert->areEquivalent($expectedPrivilege, $job->getRequiredPrivileges()); } public function testPrivilegeEnforcing()