Skip to content

Commit

Permalink
imprv: Permissions to operate comment (#4466)
Browse files Browse the repository at this point in the history
* check author when updating and removing

* restrict to show controls when the user is not the author
  • Loading branch information
yuki-takei committed Oct 8, 2021
1 parent 83dcf12 commit 863bfd7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 35 deletions.
4 changes: 1 addition & 3 deletions packages/app/src/client/services/CommentContainer.js
Expand Up @@ -132,11 +132,9 @@ export default class CommentContainer extends Container {
return this.appContainer.apiPost('/comments.update', {
commentForm: {
comment,
page_id: pageId,
revision_id: revisionId,
is_markdown: isMarkdown,
revision_id: revisionId,
comment_id: commentId,
author,
},
})
.then((res) => {
Expand Down
6 changes: 1 addition & 5 deletions packages/app/src/components/PageComment/Comment.jsx
Expand Up @@ -73,10 +73,6 @@ class Comment extends React.PureComponent {
interceptorManager.process('postRenderCommentHtml', this.currentRenderingContext);
}

checkPermissionToControlComment() {
return this.props.appContainer.isAdmin || this.isCurrentUserEqualsToAuthor();
}

isCurrentUserEqualsToAuthor() {
const { creator } = this.props.comment;
if (creator == null) {
Expand Down Expand Up @@ -210,7 +206,7 @@ class Comment extends React.PureComponent {
</UncontrolledTooltip>
</span>
</div>
{this.checkPermissionToControlComment() && (
{this.isCurrentUserEqualsToAuthor() && (
<CommentControl
onClickDeleteBtn={this.deleteBtnClickedHandler}
onClickEditBtn={() => this.setState({ isReEdit: true })}
Expand Down
10 changes: 0 additions & 10 deletions packages/app/src/server/models/comment.js
Expand Up @@ -65,16 +65,6 @@ module.exports = function(crowi) {
}));
};

commentSchema.statics.updateCommentsByPageId = function(comment, isMarkdown, commentId) {
const Comment = this;

return Comment.findOneAndUpdate(
{ _id: commentId },
{ $set: { comment, isMarkdown } },
);

};

commentSchema.statics.removeCommentsByPageId = function(pageId) {
const Comment = this;

Expand Down
45 changes: 28 additions & 17 deletions packages/app/src/server/routes/comment.js
Expand Up @@ -310,10 +310,10 @@ module.exports = function(crowi, app) {
* $ref: '#/components/schemas/Page/properties/_id'
* revision_id:
* $ref: '#/components/schemas/Revision/properties/_id'
* comment_id:
* $ref: '#/components/schemas/Comment/properties/_id'
* comment:
* $ref: '#/components/schemas/Comment/properties/comment'
* comment_position:
* $ref: '#/components/schemas/Comment/properties/commentPosition'
* required:
* - form
* responses:
Expand All @@ -340,33 +340,41 @@ module.exports = function(crowi, app) {
api.update = async function(req, res) {
const { commentForm } = req.body;

const pageId = commentForm.page_id;
const comment = commentForm.comment;
const commentStr = commentForm.comment;
const isMarkdown = commentForm.is_markdown;
const commentId = commentForm.comment_id;
const author = commentForm.author;
const revision = commentForm.revision_id;

if (comment === '') {
if (commentStr === '') {
return res.json(ApiResponse.error('Comment text is required'));
}

if (commentId == null) {
return res.json(ApiResponse.error('\'comment_id\' is undefined'));
}

if (author !== req.user.username) {
return res.json(ApiResponse.error('Only the author can edit'));
}

// check whether accessible
const isAccessible = await Page.isAccessiblePageByViewer(pageId, req.user);
if (!isAccessible) {
return res.json(ApiResponse.error('Current user is not accessible to this page.'));
}

let updatedComment;
try {
updatedComment = await Comment.updateCommentsByPageId(comment, isMarkdown, commentId);
const comment = await Comment.findById(commentId).exec();

if (comment == null) {
throw new Error('This comment does not exist.');
}

// check whether accessible
const pageId = comment.page;
const isAccessible = await Page.isAccessiblePageByViewer(pageId, req.user);
if (!isAccessible) {
throw new Error('Current user is not accessible to this page.');
}
if (req.user.id !== comment.creator.toString()) {
throw new Error('Current user is not operatable to this comment.');
}

updatedComment = await Comment.findOneAndUpdate(
{ _id: commentId },
{ $set: { comment: commentStr, isMarkdown, revision } },
);
}
catch (err) {
logger.error(err);
Expand Down Expand Up @@ -438,6 +446,9 @@ module.exports = function(crowi, app) {
if (!isAccessible) {
throw new Error('Current user is not accessible to this page.');
}
if (req.user.id !== comment.creator.toString()) {
throw new Error('Current user is not operatable to this comment.');
}

await comment.removeWithReplies();
await Page.updateCommentCount(comment.page);
Expand Down

0 comments on commit 863bfd7

Please sign in to comment.