diff --git a/src/Api.php b/src/Api.php index 64099943..64dc2879 100644 --- a/src/Api.php +++ b/src/Api.php @@ -7,12 +7,21 @@ class Api return \Chibi\Database::transaction(function() use ($job, $jobArgs) { + $job->prepare($jobArgs); + if ($job->requiresAuthentication()) Access::assertAuthentication(); if ($job->requiresConfirmedEmail()) Access::assertEmailConfirmation(); + $p = $job->requiresPrivilege(); + list ($privilege, $subPrivilege) = is_array($p) + ? $p + : [$p, null]; + if ($privilege !== null) + Access::assert($privilege, $subPrivilege); + return $job->execute($jobArgs); }); } diff --git a/src/Jobs/AbstractJob.php b/src/Jobs/AbstractJob.php index a12400e4..d168fcc4 100644 --- a/src/Jobs/AbstractJob.php +++ b/src/Jobs/AbstractJob.php @@ -1,6 +1,10 @@ TextHelper::reprUser(Auth::getCurrentUser()), + 'user' => TextHelper::reprUser($user), 'post' => TextHelper::reprPost($comment->getPost()->id)]); return $comment; diff --git a/src/Jobs/EditCommentJob.php b/src/Jobs/EditCommentJob.php index 1c7d6589..8419b844 100644 --- a/src/Jobs/EditCommentJob.php +++ b/src/Jobs/EditCommentJob.php @@ -1,18 +1,24 @@ comment = CommentModel::findById($arguments['comment-id']); + } + public function execute($arguments) { $user = Auth::getCurrentUser(); - $comment = CommentModel::findById($arguments['comment-id']); - $text = CommentModel::validateText($arguments['text']); + $comment = $this->comment; $comment->commentDate = time(); - $comment->text = $text; + $comment->text = CommentModel::validateText($arguments['text']); CommentModel::save($comment); LogHelper::log('{user} edited comment in {post}', [ - 'user' => TextHelper::reprUser(Auth::getCurrentUser()), + 'user' => TextHelper::reprUser($user), 'post' => TextHelper::reprPost($comment->getPost())]); return $comment; @@ -20,7 +26,11 @@ class EditCommentJob extends AbstractJob public function requiresPrivilege() { - return Privilege::EditComment; + return + [ + Privilege::EditComment, + Access::getIdentity($this->comment->getCommenter()) + ]; } public function requiresAuthentication() diff --git a/src/Jobs/PreviewCommentJob.php b/src/Jobs/PreviewCommentJob.php index b3f3c35f..232258b5 100644 --- a/src/Jobs/PreviewCommentJob.php +++ b/src/Jobs/PreviewCommentJob.php @@ -10,7 +10,6 @@ class PreviewCommentJob extends AbstractJob $comment->setCommenter($user); $comment->commentDate = time(); $comment->text = $text; - return $comment; }