MCP: edit_comment and delete_comment tools
Lint / JS (eslint) (push) Successful in 14s
Lint / PHP (phpcs PSR-12) (push) Successful in 35s
Lint / PHP requirements (version + extensions) (push) Successful in 34s
Lint / Deploy (push) Skipped
Lint / Notify on failure (push) Skipped
Lint / PHP (phpcs PSR-12) (pull_request) Successful in 1m0s
Security / PHP Security (semgrep) (push) Successful in 2m44s
Lint / PHP requirements (version + extensions) (pull_request) Successful in 1m9s
Lint / JS (eslint) (pull_request) Successful in 14s
Lint / Deploy (pull_request) Skipped
Lint / Notify on failure (pull_request) Skipped
Security / PHP Security (semgrep) (pull_request) Successful in 1m44s
Lint / JS (eslint) (push) Successful in 14s
Lint / PHP (phpcs PSR-12) (push) Successful in 35s
Lint / PHP requirements (version + extensions) (push) Successful in 34s
Lint / Deploy (push) Skipped
Lint / Notify on failure (push) Skipped
Lint / PHP (phpcs PSR-12) (pull_request) Successful in 1m0s
Security / PHP Security (semgrep) (push) Successful in 2m44s
Lint / PHP requirements (version + extensions) (pull_request) Successful in 1m9s
Lint / JS (eslint) (pull_request) Successful in 14s
Lint / Deploy (pull_request) Skipped
Lint / Notify on failure (pull_request) Skipped
Security / PHP Security (semgrep) (pull_request) Successful in 1m44s
Two new tickets:write tools, on the same code path as the web UI's comment edit/delete. The access check, owner-or-admin rule and audit entry moved out of api/update_comment.php and api/delete_comment.php into CommentService::updateComment/deleteComment (as add_comment did in #111), and both endpoints now call it; their responses and status codes are unchanged apart from extra ticket_id/replies_removed fields. - edit_comment: replace a comment's text (own comments; admins any). - delete_comment: delete a comment; replies go with it (fk_parent_comment ON DELETE CASCADE), so the result and the audit entry report replies_removed. - A comment on a ticket the user can't see reads as "Comment N not found", like a missing one, so restricted tickets stay hidden. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bdc6028572
commit
450b2fac3b
6 files changed
+209
-102
No files matched your search
+124
-7
@@ -1,14 +1,20 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Adding a comment to a ticket: validation, access check, reply-parent check,
|
||||
* @mention extraction (audit-logged, and notified only to mentioned users who
|
||||
* can see the ticket), comment + watcher notifications.
|
||||
* Adding, editing and deleting ticket comments.
|
||||
*
|
||||
* Shared by the web UI (api/add_comment.php) and the MCP add_comment tool so
|
||||
* both run one code path (tinker_tickets#111). Extracted verbatim from
|
||||
* add_comment.php. Returns result arrays; on validation failure the array
|
||||
* carries 'http_status' for HTTP callers. Callers own sessions/CSRF/responses.
|
||||
* addComment: validation, access check, reply-parent check, @mention
|
||||
* extraction (audit-logged, and notified only to mentioned users who can see
|
||||
* the ticket), comment + watcher notifications.
|
||||
* updateComment / deleteComment: ticket access check, then the model's
|
||||
* owner-or-admin check, then an audit entry.
|
||||
*
|
||||
* Shared by the web UI (api/add_comment.php, update_comment.php,
|
||||
* delete_comment.php) and the MCP add_comment / edit_comment /
|
||||
* delete_comment tools so both run one code path (tinker_tickets#111).
|
||||
* Extracted verbatim from those endpoints. Returns result arrays; where the
|
||||
* endpoint answered with an HTTP error, the array carries 'http_status'.
|
||||
* Callers own sessions/CSRF/responses.
|
||||
*/
|
||||
|
||||
require_once dirname(__DIR__) . '/models/TicketModel.php';
|
||||
@@ -157,4 +163,115 @@ class CommentService
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit a comment's text. Only its author or an admin may (CommentModel).
|
||||
*
|
||||
* @param array $currentUser Authenticated user row
|
||||
* @param array $data comment_id, comment_text, optional markdown_enabled
|
||||
*/
|
||||
public static function updateComment(mysqli $conn, array $currentUser, array $data): array
|
||||
{
|
||||
$commentId = (int)($data['comment_id'] ?? 0);
|
||||
$commentText = trim((string)($data['comment_text'] ?? ''));
|
||||
$markdownEnabled = !empty($data['markdown_enabled']);
|
||||
if ($commentText === '') {
|
||||
return ['success' => false, 'error' => 'Comment text cannot be empty', 'http_status' => 400];
|
||||
}
|
||||
|
||||
$commentModel = new CommentModel($conn);
|
||||
$comment = $commentModel->getCommentById($commentId);
|
||||
if ($comment && !self::canAccessCommentTicket($conn, $comment, $currentUser)) {
|
||||
return ['success' => false, 'error' => 'Access denied', 'http_status' => 403];
|
||||
}
|
||||
|
||||
$result = $commentModel->updateComment(
|
||||
$commentId,
|
||||
$commentText,
|
||||
$markdownEnabled,
|
||||
$currentUser['user_id'],
|
||||
$currentUser['is_admin'] ?? false
|
||||
);
|
||||
|
||||
if ($result['success']) {
|
||||
(new AuditLogModel($conn))->log(
|
||||
$currentUser['user_id'],
|
||||
'update',
|
||||
'comment',
|
||||
(string)$commentId,
|
||||
[
|
||||
'ticket_id' => $comment['ticket_id'] ?? null,
|
||||
'comment_text_preview' => substr($commentText, 0, 100),
|
||||
]
|
||||
);
|
||||
$result['ticket_id'] = $comment['ticket_id'] ?? null;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a comment. Only its author or an admin may (CommentModel).
|
||||
* Replies to it are deleted with it (fk_parent_comment ON DELETE CASCADE);
|
||||
* the result's replies_removed says how many.
|
||||
*
|
||||
* @param array $currentUser Authenticated user row
|
||||
* @param array $data comment_id
|
||||
*/
|
||||
public static function deleteComment(mysqli $conn, array $currentUser, array $data): array
|
||||
{
|
||||
$commentId = (int)($data['comment_id'] ?? 0);
|
||||
|
||||
$commentModel = new CommentModel($conn);
|
||||
$comment = $commentModel->getCommentById($commentId);
|
||||
if ($comment && !self::canAccessCommentTicket($conn, $comment, $currentUser)) {
|
||||
return ['success' => false, 'error' => 'Access denied', 'http_status' => 403];
|
||||
}
|
||||
|
||||
$replies = $comment ? self::countReplies($conn, $commentId) : 0;
|
||||
$result = $commentModel->deleteComment($commentId, $currentUser['user_id'], $currentUser['is_admin'] ?? false);
|
||||
|
||||
if ($result['success'] && $comment) {
|
||||
(new AuditLogModel($conn))->log(
|
||||
$currentUser['user_id'],
|
||||
'delete',
|
||||
'comment',
|
||||
(string)$commentId,
|
||||
[
|
||||
'ticket_id' => $comment['ticket_id'],
|
||||
'comment_text_preview' => substr($comment['comment_text'], 0, 100),
|
||||
'replies_removed' => $replies,
|
||||
]
|
||||
);
|
||||
$result['replies_removed'] = $replies;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private static function canAccessCommentTicket(mysqli $conn, array $comment, array $currentUser): bool
|
||||
{
|
||||
$ticketModel = new TicketModel($conn);
|
||||
$ticket = $ticketModel->getTicketById($comment['ticket_id']);
|
||||
return $ticket && $ticketModel->canUserAccessTicket($ticket, $currentUser);
|
||||
}
|
||||
|
||||
/** All replies under a comment, at any depth. */
|
||||
private static function countReplies(mysqli $conn, int $commentId): int
|
||||
{
|
||||
$count = 0;
|
||||
$frontier = [$commentId];
|
||||
$stmt = $conn->prepare("SELECT comment_id FROM ticket_comments WHERE parent_comment_id = ?");
|
||||
while ($frontier) {
|
||||
$parent = array_pop($frontier);
|
||||
$stmt->bind_param('i', $parent);
|
||||
$stmt->execute();
|
||||
foreach ($stmt->get_result()->fetch_all(MYSQLI_ASSOC) as $row) {
|
||||
$count++;
|
||||
$frontier[] = (int)$row['comment_id'];
|
||||
}
|
||||
}
|
||||
$stmt->close();
|
||||
return $count;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user