1 <?php namespace BookStack\Http\Controllers;
3 use BookStack\Repos\CommentRepo;
4 use BookStack\Repos\EntityRepo;
6 use Illuminate\Http\Request;
8 // delete -checkOwnablePermission \
9 class CommentController extends Controller
11 protected $entityRepo;
13 public function __construct(EntityRepo $entityRepo, CommentRepo $commentRepo, Comment $comment)
15 $this->entityRepo = $entityRepo;
16 $this->commentRepo = $commentRepo;
17 $this->comment = $comment;
18 parent::__construct();
21 public function save(Request $request, $pageId, $commentId = null)
23 $this->validate($request, [
24 'text' => 'required|string',
25 'html' => 'required|string',
29 $page = $this->entityRepo->getById('page', $pageId, true);
30 } catch (ModelNotFoundException $e) {
31 return response('Not found', 404);
35 // cannot add comments to drafts.
36 return response()->json([
38 'message' => trans('errors.cannot_add_comment_to_draft'),
42 $this->checkOwnablePermission('page-view', $page);
43 if (empty($commentId)) {
44 // create a new comment.
45 $this->checkPermission('comment-create-all');
46 $comment = $this->commentRepo->create($page, $request->only(['text', 'html', 'parent_id']));
47 $respMsg = trans('entities.comment_created');
49 // update existing comment
50 // get comment by ID and check if this user has permission to update.
51 $comment = $this->comment->findOrFail($commentId);
52 $this->checkOwnablePermission('comment-update', $comment);
53 $this->commentRepo->update($comment, $request->all());
54 $respMsg = trans('entities.comment_updated');
57 $comment = $this->commentRepo->getCommentById($comment->id);
59 return response()->json([
60 'status' => 'success',
61 'message' => $respMsg,
67 public function destroy($id) {
68 $comment = $this->comment->findOrFail($id);
69 $this->checkOwnablePermission('comment-delete', $comment);
70 $this->commentRepo->delete($comment);
71 $comment = $this->commentRepo->getCommentById($comment->id);
73 return response()->json([
75 'message' => trans('entities.comment_deleted'),
81 public function getPageComments($pageId) {
83 $page = $this->entityRepo->getById('page', $pageId, true);
84 } catch (ModelNotFoundException $e) {
85 return response('Not found', 404);
89 // cannot add comments to drafts.
90 return response()->json([
92 'message' => trans('errors.no_comments_for_draft'),
96 $this->checkOwnablePermission('page-view', $page);
98 $comments = $this->commentRepo->getPageComments($pageId);
99 return response()->json(['success' => true, 'comments'=> $comments['comments'],
100 'total' => $comments['total'], 'permissions' => [
101 'comment_create' => $this->currentUser->can('comment-create-all'),
102 'comment_update_own' => $this->currentUser->can('comment-update-own'),
103 'comment_update_all' => $this->currentUser->can('comment-update-all'),
104 'comment_delete_all' => $this->currentUser->can('comment-delete-all'),
105 'comment_delete_own' => $this->currentUser->can('comment-delete-own'),
106 ], 'user_id' => $this->currentUser->id]);