]> BookStack Code Mirror - bookstack/commitdiff
#47 - Adds comment level permissions to the front-end.
authorAbijeet <redacted>
Sun, 4 Jun 2017 05:47:14 +0000 (11:17 +0530)
committerAbijeet <redacted>
Sun, 4 Jun 2017 05:47:14 +0000 (11:17 +0530)
app/Http/Controllers/CommentController.php
app/Http/Controllers/PageController.php
app/Repos/CommentRepo.php
resources/assets/js/controllers.js
resources/assets/js/directives.js
resources/views/comments/comments.blade.php
resources/views/comments/list-item.blade.php

index 29ccdf5a758f623672e10fd104386bb8c19e3c2f..3a267193d5b155cd59e06798949ddc1a774a6d52 100644 (file)
@@ -88,6 +88,13 @@ class CommentController extends Controller
         $this->checkOwnablePermission('page-view', $page);
 
         $comments = $this->commentRepo->getPageComments($pageId);
-        return response()->json(['success' => true, 'comments'=> $comments['comments'], 'total' => $comments['total']]);
+        return response()->json(['success' => true, 'comments'=> $comments['comments'],
+            'total' => $comments['total'], 'permissions' => [
+                'comment_create' => $this->currentUser->can('comment-create-all'),
+                'comment_update_own' => $this->currentUser->can('comment-update-own'),
+                'comment_update_all' => $this->currentUser->can('comment-update-all'),
+                'comment_delete_all' => $this->currentUser->can('comment-delete-all'),
+                'comment_delete_own' => $this->currentUser->can('comment-delete-own'),
+            ], 'user_id' => $this->currentUser->id]);
     }
 }
index 73619721390a7cad6956ce7d75bb599f86e7e353..9a8525c2365c5025d46ea51172157437ed783823 100644 (file)
@@ -161,7 +161,7 @@ class PageController extends Controller
         $pageContent = $this->entityRepo->renderPage($page);
         $sidebarTree = $this->entityRepo->getBookChildren($page->book);
         $pageNav = $this->entityRepo->getPageNav($pageContent);
-        
+
         Views::add($page);
         $this->setPageTitle($page->getShortName());
         return view('pages/show', [
@@ -376,7 +376,7 @@ class PageController extends Controller
 
         $page->fill($revision->toArray());
         $this->setPageTitle(trans('entities.pages_revision_named', ['pageName' => $page->getShortName()]));
-        
+
         return view('pages/revision', [
             'page' => $page,
             'book' => $page->book,
@@ -590,9 +590,4 @@ class PageController extends Controller
         return redirect($page->getUrl());
     }
 
-    public function getLastXComments($pageId)
-    {
-        // $this->checkOwnablePermission('page-view', $page);
-    }
-
 }
index 7d0c4ebd7b419ad11035cb680dfa03995dcc1f13..83847239f1be6e10bb45992b17aa87e3b2ae6c5b 100644 (file)
@@ -43,11 +43,14 @@ class CommentRepo {
         $comments = $this->comment->getAllPageComments($pageId);
         $index = [];
         $totalComments = count($comments);
+        $finalCommentList = [];
+
         // normalizing the response.
-        foreach($comments as &$comment) {
-            $comment = $this->normalizeComment($comment);
+        for ($i = 0; $i < count($comments); ++$i) {
+            $comment = $this->normalizeComment($comments[$i]);
             $parentId = $comment->parent_id;
             if (empty($parentId)) {
+                $finalCommentList[] = $comment;
                 $index[$comment->id] = $comment;
                 continue;
             }
@@ -63,7 +66,7 @@ class CommentRepo {
             $index[$comment->id] = $comment;
         }
         return [
-            'comments' => $comments,
+            'comments' => $finalCommentList,
             'total' => $totalComments
         ];
     }
index f64d7c038d4263ce469365c7858b397d58e6f55d..4763f986745e1f639091fb1191d3b2e2507ebbaf 100644 (file)
@@ -756,6 +756,7 @@ module.exports = function (ngApp, events) {
         // keep track of comment levels
         $scope.level = 1;
         vm.totalCommentsStr = 'Loading...';
+        vm.permissions = {};
 
         $scope.$on('evt.new-comment', function (event, comment) {
             // add the comment to the comment list.
@@ -764,6 +765,21 @@ module.exports = function (ngApp, events) {
             event.preventDefault();
         });
 
+        vm.canEdit = function (comment) {
+            if (vm.permissions.comment_update_all) {
+                return true;
+            }
+
+            if (vm.permissions.comment_update_own && comment.created_by.id === vm.current_user_id) {
+                return true;
+            }
+            return false;
+        }
+
+        vm.canComment = function () {
+            return vm.permissions.comment_create;
+        }
+
         $timeout(function() {
             $http.get(window.baseUrl(`/ajax/page/${$scope.pageId}/comments/`)).then(resp => {
                 if (!resp.data || resp.data.success !== true) {
@@ -772,6 +788,9 @@ module.exports = function (ngApp, events) {
                 }
                 vm.comments = resp.data.comments;
                 vm.totalComments = resp.data.total;
+                vm.permissions = resp.data.permissions;
+                vm.current_user_id = resp.data.user_id;
+
                 // TODO : Fetch message from translate.
                 if (vm.totalComments === 0) {
                     vm.totalCommentsStr = 'No comments found.';
index 278e0f8c656c74342989213a04659c18f67161f0..0929a9cf4b2d83a67974c13009209179a41d679d 100644 (file)
@@ -908,7 +908,7 @@ module.exports = function (ngApp, events) {
         }
 
         function removeDupe() {
-            let $existingElement = $document.find('.comments-list comment-reply');
+            let $existingElement = $document.find('.comments-list comment-reply, .comments-list comment-edit');
             if (!$existingElement.length) {
                 return;
             }
index 93e7ebc05bdb0f1fd083323237a80545ef46dc64..ffa75cfed227638fc72e1615c32803134994045e 100644 (file)
@@ -12,5 +12,7 @@
 
         </div>
     </div>
-    @include('comments/comment-reply', ['pageId' => $pageId])
+    <div ng-if="::vm.canComment()">
+        @include('comments/comment-reply', ['pageId' => $pageId])
+    </div>
 </div>
\ No newline at end of file
index 46af1a862005a184cb20677ce52de679f8232ee5..67355c586001eb8a4dc1241294eaccf621055214 100644 (file)
@@ -11,8 +11,8 @@
         </div>
         <div class="comment-actions">
             <ul>
-                <li ng-if="level < 3"><a href="#" comment-reply-link no-comment-reply-dupe="true" comment="comment" is-reply="true">Reply</a></li>
-                <li><a href="#" comment-reply-link no-comment-reply-dupe="true" comment="comment">Edit</a></li>
+                <li ng-if="::(level < 3 && vm.canComment())"><a href="#" comment-reply-link no-comment-reply-dupe="true" comment="comment" is-reply="true">Reply</a></li>
+                <li ng-if="::vm.canEdit(comment)"><a href="#" comment-reply-link no-comment-reply-dupe="true" comment="comment" >Edit</a></li>
                 <li>Created <a title="@{{::comment.created.day_time_str}}" href="#comment-@{{::comment.id}}-@{{::pageId}}">@{{::comment.created.diff}}</a></li>
                 <li ng-if="comment.updated"><span title="@{{comment.updated.day_time_str}}">Updated @{{comment.updated.diff}} by
                     <a href="@{{comment.updated_by.profile_url}}">@{{comment.updated_by.name}}</a></span></li>
Morty Proxy This is a proxified and sanitized view of the page, visit original site.