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');