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
@@ -132,6 +132,8 @@ claude mcp login tinker # add --no-browser on a headless machine, the
|
|||||||
| `find_similar_tickets` | `tickets:read` | Open tickets with similar titles, by title text or an existing ticket (same finder as the ticket page's possible-duplicates list) |
|
| `find_similar_tickets` | `tickets:read` | Open tickets with similar titles, by title text or an existing ticket (same finder as the ticket page's possible-duplicates list) |
|
||||||
| `create_ticket` | `tickets:write` | Create a ticket (title, description, priority, category, type, visibility, assignee) |
|
| `create_ticket` | `tickets:write` | Create a ticket (title, description, priority, category, type, visibility, assignee) |
|
||||||
| `add_comment` | `tickets:write` | Comment (markdown, @mentions, replies) |
|
| `add_comment` | `tickets:write` | Comment (markdown, @mentions, replies) |
|
||||||
|
| `edit_comment` | `tickets:write` | Replace a comment's text (your own comments; admins any) |
|
||||||
|
| `delete_comment` | `tickets:write` | Delete a comment and its replies (your own comments; admins any); reports `replies_removed` |
|
||||||
| `update_status` | `tickets:write` | Workflow-validated status change; `comment` required when the transition requires one |
|
| `update_status` | `tickets:write` | Workflow-validated status change; `comment` required when the transition requires one |
|
||||||
| `assign_ticket` | `tickets:write` | Assign / unassign (admin, creator, or current assignee only) |
|
| `assign_ticket` | `tickets:write` | Assign / unassign (admin, creator, or current assignee only) |
|
||||||
| `link_tickets` | `tickets:write` | Add a `blocks` / `blocked_by` / `relates_to` / `duplicates` link ("A duplicates B" = A is the duplicate; links only, never closes anything) |
|
| `link_tickets` | `tickets:write` | Add a `blocks` / `blocked_by` / `relates_to` / `duplicates` link ("A duplicates B" = A is the duplicate; links only, never closes anything) |
|
||||||
@@ -448,7 +450,7 @@ tinker_tickets/
|
|||||||
│ └── cleanup_orphan_uploads.php # Delete orphaned upload files past grace period (cron)
|
│ └── cleanup_orphan_uploads.php # Delete orphaned upload files past grace period (cron)
|
||||||
├── services/
|
├── services/
|
||||||
│ ├── AssignmentService.php # Assign/unassign (shared by assign_ticket.php + MCP)
|
│ ├── AssignmentService.php # Assign/unassign (shared by assign_ticket.php + MCP)
|
||||||
│ ├── CommentService.php # Add comment + mentions/notifications (shared by add_comment.php + MCP)
|
│ ├── CommentService.php # Add/edit/delete comments (shared by the comment endpoints + MCP)
|
||||||
│ ├── DependencyService.php # List/add/remove ticket links (shared by ticket_dependencies.php + MCP)
|
│ ├── DependencyService.php # List/add/remove ticket links (shared by ticket_dependencies.php + MCP)
|
||||||
│ ├── SimilarTicketService.php # Similar-title finder (shared by check_duplicates.php + MCP)
|
│ ├── SimilarTicketService.php # Similar-title finder (shared by check_duplicates.php + MCP)
|
||||||
│ └── TicketCreationService.php # Create ticket (shared by TicketController + MCP)
|
│ └── TicketCreationService.php # Create ticket (shared by TicketController + MCP)
|
||||||
|
|||||||
+5
-42
@@ -18,9 +18,7 @@ ob_start();
|
|||||||
try {
|
try {
|
||||||
require_once dirname(__DIR__) . '/config/config.php';
|
require_once dirname(__DIR__) . '/config/config.php';
|
||||||
require_once dirname(__DIR__) . '/helpers/Database.php';
|
require_once dirname(__DIR__) . '/helpers/Database.php';
|
||||||
require_once dirname(__DIR__) . '/models/CommentModel.php';
|
require_once dirname(__DIR__) . '/services/CommentService.php';
|
||||||
require_once dirname(__DIR__) . '/models/TicketModel.php';
|
|
||||||
require_once dirname(__DIR__) . '/models/AuditLogModel.php';
|
|
||||||
|
|
||||||
// Only allow POST or DELETE — reject GET to prevent CSRF bypass
|
// Only allow POST or DELETE — reject GET to prevent CSRF bypass
|
||||||
$method = $_SERVER['REQUEST_METHOD'];
|
$method = $_SERVER['REQUEST_METHOD'];
|
||||||
@@ -54,8 +52,6 @@ try {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$currentUser = $_SESSION['user'];
|
$currentUser = $_SESSION['user'];
|
||||||
$userId = $currentUser['user_id'];
|
|
||||||
$isAdmin = $currentUser['is_admin'] ?? false;
|
|
||||||
|
|
||||||
// Use centralized database connection
|
// Use centralized database connection
|
||||||
$conn = Database::getConnection();
|
$conn = Database::getConnection();
|
||||||
@@ -76,43 +72,10 @@ try {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$commentId = (int)$data['comment_id'];
|
$result = CommentService::deleteComment($conn, $currentUser, $data);
|
||||||
|
if (isset($result['http_status'])) {
|
||||||
// Initialize models
|
http_response_code($result['http_status']);
|
||||||
$commentModel = new CommentModel($conn);
|
unset($result['http_status']);
|
||||||
$auditLog = new AuditLogModel($conn);
|
|
||||||
|
|
||||||
// Get comment before deletion for audit log and access check
|
|
||||||
$comment = $commentModel->getCommentById($commentId);
|
|
||||||
|
|
||||||
// Verify user can access the parent ticket
|
|
||||||
if ($comment) {
|
|
||||||
$ticketModel = new TicketModel($conn);
|
|
||||||
$ticket = $ticketModel->getTicketById($comment['ticket_id']);
|
|
||||||
if (!$ticket || !$ticketModel->canUserAccessTicket($ticket, $currentUser)) {
|
|
||||||
ob_end_clean();
|
|
||||||
http_response_code(403);
|
|
||||||
header('Content-Type: application/json');
|
|
||||||
echo json_encode(['success' => false, 'error' => 'Access denied']);
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete comment
|
|
||||||
$result = $commentModel->deleteComment($commentId, $userId, $isAdmin);
|
|
||||||
|
|
||||||
// Log the deletion if successful
|
|
||||||
if ($result['success'] && $comment) {
|
|
||||||
$auditLog->log(
|
|
||||||
$userId,
|
|
||||||
'delete',
|
|
||||||
'comment',
|
|
||||||
(string)$commentId,
|
|
||||||
[
|
|
||||||
'ticket_id' => $comment['ticket_id'],
|
|
||||||
'comment_text_preview' => substr($comment['comment_text'], 0, 100)
|
|
||||||
]
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Discard any unexpected output
|
// Discard any unexpected output
|
||||||
|
|||||||
+5
-50
@@ -18,9 +18,7 @@ ob_start();
|
|||||||
try {
|
try {
|
||||||
require_once dirname(__DIR__) . '/config/config.php';
|
require_once dirname(__DIR__) . '/config/config.php';
|
||||||
require_once dirname(__DIR__) . '/helpers/Database.php';
|
require_once dirname(__DIR__) . '/helpers/Database.php';
|
||||||
require_once dirname(__DIR__) . '/models/CommentModel.php';
|
require_once dirname(__DIR__) . '/services/CommentService.php';
|
||||||
require_once dirname(__DIR__) . '/models/TicketModel.php';
|
|
||||||
require_once dirname(__DIR__) . '/models/AuditLogModel.php';
|
|
||||||
|
|
||||||
// Check authentication via session
|
// Check authentication via session
|
||||||
if (session_status() === PHP_SESSION_NONE) {
|
if (session_status() === PHP_SESSION_NONE) {
|
||||||
@@ -47,8 +45,6 @@ try {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$currentUser = $_SESSION['user'];
|
$currentUser = $_SESSION['user'];
|
||||||
$userId = $currentUser['user_id'];
|
|
||||||
$isAdmin = $currentUser['is_admin'] ?? false;
|
|
||||||
|
|
||||||
// Use centralized database connection
|
// Use centralized database connection
|
||||||
$conn = Database::getConnection();
|
$conn = Database::getConnection();
|
||||||
@@ -64,51 +60,10 @@ try {
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
$commentId = (int)$data['comment_id'];
|
$result = CommentService::updateComment($conn, $currentUser, $data);
|
||||||
$commentText = trim($data['comment_text']);
|
if (isset($result['http_status'])) {
|
||||||
$markdownEnabled = isset($data['markdown_enabled']) && $data['markdown_enabled'];
|
http_response_code($result['http_status']);
|
||||||
|
unset($result['http_status']);
|
||||||
if (empty($commentText)) {
|
|
||||||
ob_end_clean();
|
|
||||||
http_response_code(400);
|
|
||||||
header('Content-Type: application/json');
|
|
||||||
echo json_encode(['success' => false, 'error' => 'Comment text cannot be empty']);
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize models
|
|
||||||
$commentModel = new CommentModel($conn);
|
|
||||||
$auditLog = new AuditLogModel($conn);
|
|
||||||
|
|
||||||
// Verify user can access the parent ticket
|
|
||||||
$comment = $commentModel->getCommentById($commentId);
|
|
||||||
if ($comment) {
|
|
||||||
$ticketModel = new TicketModel($conn);
|
|
||||||
$ticket = $ticketModel->getTicketById($comment['ticket_id']);
|
|
||||||
if (!$ticket || !$ticketModel->canUserAccessTicket($ticket, $currentUser)) {
|
|
||||||
ob_end_clean();
|
|
||||||
http_response_code(403);
|
|
||||||
header('Content-Type: application/json');
|
|
||||||
echo json_encode(['success' => false, 'error' => 'Access denied']);
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update comment
|
|
||||||
$result = $commentModel->updateComment($commentId, $commentText, $markdownEnabled, $userId, $isAdmin);
|
|
||||||
|
|
||||||
// Log the update if successful
|
|
||||||
if ($result['success']) {
|
|
||||||
$auditLog->log(
|
|
||||||
$userId,
|
|
||||||
'update',
|
|
||||||
'comment',
|
|
||||||
(string)$commentId,
|
|
||||||
[
|
|
||||||
'ticket_id' => $comment['ticket_id'] ?? null,
|
|
||||||
'comment_text_preview' => substr($commentText, 0, 100),
|
|
||||||
]
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Discard any unexpected output
|
// Discard any unexpected output
|
||||||
|
|||||||
@@ -15,7 +15,10 @@ use TinkerTickets\Mcp\Tools\TicketWriteTools;
|
|||||||
final class ToolCatalog
|
final class ToolCatalog
|
||||||
{
|
{
|
||||||
/** Tool names that mutate data and require tickets:write. */
|
/** Tool names that mutate data and require tickets:write. */
|
||||||
private const WRITE_TOOLS = ['create_ticket', 'add_comment', 'update_status', 'assign_ticket', 'link_tickets', 'unlink_tickets'];
|
private const WRITE_TOOLS = [
|
||||||
|
'create_ticket', 'add_comment', 'edit_comment', 'delete_comment',
|
||||||
|
'update_status', 'assign_ticket', 'link_tickets', 'unlink_tickets',
|
||||||
|
];
|
||||||
|
|
||||||
public static function isWriteTool(string $name): bool
|
public static function isWriteTool(string $name): bool
|
||||||
{
|
{
|
||||||
@@ -27,7 +30,7 @@ final class ToolCatalog
|
|||||||
$read = new TicketReadTools($conn);
|
$read = new TicketReadTools($conn);
|
||||||
$write = new TicketWriteTools($conn);
|
$write = new TicketWriteTools($conn);
|
||||||
$readOnly = new ToolAnnotations(readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false);
|
$readOnly = new ToolAnnotations(readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false);
|
||||||
// Writes change tickets (and notify people); only unlink_tickets removes anything.
|
// Writes change tickets (and notify people); unlink_tickets and delete_comment remove things.
|
||||||
$additive = new ToolAnnotations(readOnlyHint: false, destructiveHint: false, idempotentHint: false, openWorldHint: false);
|
$additive = new ToolAnnotations(readOnlyHint: false, destructiveHint: false, idempotentHint: false, openWorldHint: false);
|
||||||
$update = new ToolAnnotations(readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: false);
|
$update = new ToolAnnotations(readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: false);
|
||||||
$remove = new ToolAnnotations(readOnlyHint: false, destructiveHint: true, idempotentHint: true, openWorldHint: false);
|
$remove = new ToolAnnotations(readOnlyHint: false, destructiveHint: true, idempotentHint: true, openWorldHint: false);
|
||||||
@@ -38,6 +41,8 @@ final class ToolCatalog
|
|||||||
->addTool([$read, 'findSimilarTickets'], 'find_similar_tickets', 'Find similar tickets', null, $readOnly)
|
->addTool([$read, 'findSimilarTickets'], 'find_similar_tickets', 'Find similar tickets', null, $readOnly)
|
||||||
->addTool([$write, 'createTicket'], 'create_ticket', 'Create ticket', null, $additive)
|
->addTool([$write, 'createTicket'], 'create_ticket', 'Create ticket', null, $additive)
|
||||||
->addTool([$write, 'addComment'], 'add_comment', 'Add comment', null, $additive)
|
->addTool([$write, 'addComment'], 'add_comment', 'Add comment', null, $additive)
|
||||||
|
->addTool([$write, 'editComment'], 'edit_comment', 'Edit comment', null, $update)
|
||||||
|
->addTool([$write, 'deleteComment'], 'delete_comment', 'Delete comment', null, $remove)
|
||||||
->addTool([$write, 'updateStatus'], 'update_status', 'Update ticket status', null, $update)
|
->addTool([$write, 'updateStatus'], 'update_status', 'Update ticket status', null, $update)
|
||||||
->addTool([$write, 'assignTicket'], 'assign_ticket', 'Assign ticket', null, $update)
|
->addTool([$write, 'assignTicket'], 'assign_ticket', 'Assign ticket', null, $update)
|
||||||
->addTool([$write, 'linkTickets'], 'link_tickets', 'Link tickets', null, $additive)
|
->addTool([$write, 'linkTickets'], 'link_tickets', 'Link tickets', null, $additive)
|
||||||
|
|||||||
@@ -111,6 +111,55 @@ final class TicketWriteTools
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Edit one of your comments (admins can edit anyone's). The new text
|
||||||
|
* replaces the old; comment_id comes from get_ticket.
|
||||||
|
*
|
||||||
|
* @param int $comment_id The comment to edit.
|
||||||
|
* @param string $text The new comment text.
|
||||||
|
* @param bool $markdown Render the comment as markdown.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function editComment(int $comment_id, string $text, bool $markdown = true): array
|
||||||
|
{
|
||||||
|
$user = McpIdentity::user();
|
||||||
|
$result = \CommentService::updateComment($this->conn, $user, [
|
||||||
|
'comment_id' => $comment_id,
|
||||||
|
'comment_text' => $text,
|
||||||
|
'markdown_enabled' => $markdown,
|
||||||
|
]);
|
||||||
|
if (empty($result['success'])) {
|
||||||
|
throw new ToolCallException($this->commentError($result, $comment_id));
|
||||||
|
}
|
||||||
|
|
||||||
|
return ['comment_id' => $comment_id, 'ticket_id' => (string)$result['ticket_id'], 'edited' => true];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete one of your comments (admins can delete anyone's). Replies to it
|
||||||
|
* are deleted with it; replies_removed says how many. This can't be undone.
|
||||||
|
*
|
||||||
|
* @param int $comment_id The comment to delete; comment_id comes from get_ticket.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function deleteComment(int $comment_id): array
|
||||||
|
{
|
||||||
|
$user = McpIdentity::user();
|
||||||
|
$result = \CommentService::deleteComment($this->conn, $user, ['comment_id' => $comment_id]);
|
||||||
|
if (empty($result['success'])) {
|
||||||
|
throw new ToolCallException($this->commentError($result, $comment_id));
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'comment_id' => $comment_id,
|
||||||
|
'ticket_id' => (string)$result['ticket_id'],
|
||||||
|
'deleted' => true,
|
||||||
|
'replies_removed' => (int)($result['replies_removed'] ?? 0),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Change a ticket's status. Transitions follow the Workflow Designer rules;
|
* Change a ticket's status. Transitions follow the Workflow Designer rules;
|
||||||
* some transitions (e.g. closing) require a comment, which is posted as the reason.
|
* some transitions (e.g. closing) require a comment, which is posted as the reason.
|
||||||
@@ -301,6 +350,22 @@ final class TicketWriteTools
|
|||||||
return (int)$match['user_id'];
|
return (int)$match['user_id'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Comment failures: a comment on a ticket the user can't see reads as
|
||||||
|
* "not found", like a missing one. "Not your comment" is passed through:
|
||||||
|
* the user can already see that comment on the ticket.
|
||||||
|
*
|
||||||
|
* @param array<string, mixed> $result
|
||||||
|
*/
|
||||||
|
private function commentError(array $result, int $commentId): string
|
||||||
|
{
|
||||||
|
$error = (string)($result['error'] ?? 'Request failed');
|
||||||
|
if (in_array($error, ['Access denied', 'Comment not found'], true)) {
|
||||||
|
return "Comment {$commentId} not found";
|
||||||
|
}
|
||||||
|
return $error;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Map a shared-service failure to a tool error. A ticket the user can't
|
* Map a shared-service failure to a tool error. A ticket the user can't
|
||||||
* see reads as "not found" (like get_ticket), never "access denied", so a
|
* see reads as "not found" (like get_ticket), never "access denied", so a
|
||||||
|
|||||||
+124
-7
@@ -1,14 +1,20 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adding a comment to a ticket: validation, access check, reply-parent check,
|
* Adding, editing and deleting ticket comments.
|
||||||
* @mention extraction (audit-logged, and notified only to mentioned users who
|
|
||||||
* can see the ticket), comment + watcher notifications.
|
|
||||||
*
|
*
|
||||||
* Shared by the web UI (api/add_comment.php) and the MCP add_comment tool so
|
* addComment: validation, access check, reply-parent check, @mention
|
||||||
* both run one code path (tinker_tickets#111). Extracted verbatim from
|
* extraction (audit-logged, and notified only to mentioned users who can see
|
||||||
* add_comment.php. Returns result arrays; on validation failure the array
|
* the ticket), comment + watcher notifications.
|
||||||
* carries 'http_status' for HTTP callers. Callers own sessions/CSRF/responses.
|
* 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';
|
require_once dirname(__DIR__) . '/models/TicketModel.php';
|
||||||
@@ -157,4 +163,115 @@ class CommentService
|
|||||||
|
|
||||||
return $result;
|
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