2026-09-26 17:08:04 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Listing, adding and removing ticket dependencies (blocks / blocked_by /
|
|
|
|
|
* relates_to / duplicates): access checks on the tickets involved,
|
|
|
|
|
* DependencyModel's duplicate/inverse/cycle checks, and audit logging.
|
|
|
|
|
*
|
2026-11-25 20:22:06 -05:00
|
|
|
* Shared by the web UI (api/ticket_dependencies.php), the MCP
|
|
|
|
|
* link_tickets / unlink_tickets / get_ticket tools and the Bearer
|
|
|
|
|
* api/ticket_link_api.php (#123) so they all run one code path.
|
2026-09-26 17:08:04 -04:00
|
|
|
* Extracted from ticket_dependencies.php with the same checks, order and
|
|
|
|
|
* error messages. Returns result arrays; failures carry 'http_status' for
|
|
|
|
|
* HTTP callers. Callers own sessions/CSRF/responses.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
require_once dirname(__DIR__) . '/models/DependencyModel.php';
|
|
|
|
|
require_once dirname(__DIR__) . '/models/AuditLogModel.php';
|
|
|
|
|
require_once dirname(__DIR__) . '/models/TicketModel.php';
|
|
|
|
|
|
|
|
|
|
class DependencyService
|
|
|
|
|
{
|
|
|
|
|
public const TYPES = ['blocks', 'blocked_by', 'relates_to', 'duplicates'];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Links from and to a ticket, limited to linked tickets the user can see.
|
|
|
|
|
*
|
|
|
|
|
* @param array $currentUser Authenticated user row (user_id, groups, is_admin)
|
|
|
|
|
* @return array ['success' => true, 'dependencies' => [type => rows], 'dependents' => rows]
|
|
|
|
|
*/
|
|
|
|
|
public static function list(mysqli $conn, array $currentUser, $ticketId): array
|
|
|
|
|
{
|
|
|
|
|
if (!$ticketId) {
|
|
|
|
|
return ['success' => false, 'error' => 'Ticket ID required', 'http_status' => 400];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Verify user can access this ticket
|
|
|
|
|
$ticketModel = new TicketModel($conn);
|
|
|
|
|
$ticket = $ticketModel->getTicketById($ticketId);
|
|
|
|
|
if (!$ticket || !$ticketModel->canUserAccessTicket($ticket, $currentUser)) {
|
|
|
|
|
return ['success' => false, 'error' => 'Ticket not found', 'http_status' => 404];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$dependencyModel = new DependencyModel($conn);
|
|
|
|
|
[$userId, $userGroups, $isAdmin] = self::viewer($currentUser);
|
|
|
|
|
try {
|
|
|
|
|
$dependencies = $dependencyModel->getDependencies($ticketId, $userId, $userGroups, $isAdmin);
|
|
|
|
|
$dependents = $dependencyModel->getDependentTickets($ticketId, $userId, $userGroups, $isAdmin);
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
error_log('DependencyService::list query error: ' . $e->getMessage());
|
|
|
|
|
return ['success' => false, 'error' => 'Failed to retrieve dependencies', 'http_status' => 500];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ['success' => true, 'dependencies' => $dependencies, 'dependents' => $dependents];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $data ticket_id, depends_on_id, dependency_type (default "blocks")
|
|
|
|
|
* @return array ['success' => true, 'dependency_id' => int] or an error
|
|
|
|
|
*/
|
|
|
|
|
public static function add(mysqli $conn, array $currentUser, array $data): array
|
|
|
|
|
{
|
|
|
|
|
$ticketId = $data['ticket_id'] ?? null;
|
|
|
|
|
$dependsOnId = $data['depends_on_id'] ?? null;
|
|
|
|
|
$type = $data['dependency_type'] ?? 'blocks';
|
|
|
|
|
|
|
|
|
|
if (!$ticketId || !$dependsOnId) {
|
|
|
|
|
return ['success' => false, 'error' => 'Both ticket_id and depends_on_id are required', 'http_status' => 400];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Verify user can access both tickets before creating dependency
|
|
|
|
|
$ticketModel = new TicketModel($conn);
|
|
|
|
|
$srcTicket = $ticketModel->getTicketById($ticketId);
|
|
|
|
|
if (!$srcTicket || !$ticketModel->canUserAccessTicket($srcTicket, $currentUser)) {
|
|
|
|
|
return ['success' => false, 'error' => 'Ticket not found', 'http_status' => 404];
|
|
|
|
|
}
|
|
|
|
|
$tgtTicket = $ticketModel->getTicketById($dependsOnId);
|
|
|
|
|
if (!$tgtTicket || !$ticketModel->canUserAccessTicket($tgtTicket, $currentUser)) {
|
|
|
|
|
return ['success' => false, 'error' => 'Target ticket not found', 'http_status' => 404];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$result = (new DependencyModel($conn))->addDependency($ticketId, $dependsOnId, $type, $currentUser['user_id']);
|
|
|
|
|
if (!$result['success']) {
|
|
|
|
|
return ['success' => false, 'error' => $result['error'], 'http_status' => 400];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(new AuditLogModel($conn))->log($currentUser['user_id'], 'create', 'dependency', (string)$result['dependency_id'], [
|
|
|
|
|
'ticket_id' => $ticketId,
|
|
|
|
|
'depends_on_id' => $dependsOnId,
|
|
|
|
|
'type' => $type
|
2026-11-25 20:22:06 -05:00
|
|
|
] + ($data['audit_extra'] ?? []));
|
2026-09-26 17:08:04 -04:00
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove one link, by dependency_id or by (ticket_id, depends_on_id, dependency_type).
|
|
|
|
|
*
|
|
|
|
|
* @return array ['success' => true, 'removed' => rows deleted] or an error
|
|
|
|
|
*/
|
|
|
|
|
public static function remove(mysqli $conn, array $currentUser, array $data): array
|
|
|
|
|
{
|
|
|
|
|
$dependencyModel = new DependencyModel($conn);
|
|
|
|
|
$ticketModel = new TicketModel($conn);
|
|
|
|
|
$auditLog = new AuditLogModel($conn);
|
|
|
|
|
$dependencyId = $data['dependency_id'] ?? null;
|
|
|
|
|
|
|
|
|
|
// Alternative: delete by ticket IDs
|
|
|
|
|
if (!$dependencyId && isset($data['ticket_id']) && isset($data['depends_on_id'])) {
|
|
|
|
|
$ticketId = $data['ticket_id'];
|
|
|
|
|
$dependsOnId = $data['depends_on_id'];
|
|
|
|
|
$type = $data['dependency_type'] ?? 'blocks';
|
|
|
|
|
|
|
|
|
|
// Validate dependency type
|
|
|
|
|
if (!in_array($type, self::TYPES, true)) {
|
|
|
|
|
return ['success' => false, 'error' => 'Invalid dependency type', 'http_status' => 400];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Verify user can access the source ticket
|
|
|
|
|
$srcTicket = $ticketModel->getTicketById($ticketId);
|
|
|
|
|
if (!$srcTicket || !$ticketModel->canUserAccessTicket($srcTicket, $currentUser)) {
|
|
|
|
|
return ['success' => false, 'error' => 'Ticket not found', 'http_status' => 404];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$removed = $dependencyModel->removeDependencyByTickets($ticketId, $dependsOnId, $type);
|
|
|
|
|
if ($removed === false) {
|
|
|
|
|
return ['success' => false, 'error' => 'Failed to remove dependency', 'http_status' => 400];
|
|
|
|
|
}
|
|
|
|
|
// Only audit a removal that happened (the web UI treats removing
|
|
|
|
|
// a link that is already gone as success).
|
|
|
|
|
if ($removed > 0) {
|
|
|
|
|
$auditLog->log($currentUser['user_id'], 'delete', 'dependency', null, [
|
|
|
|
|
'ticket_id' => $ticketId,
|
|
|
|
|
'depends_on_id' => $dependsOnId,
|
|
|
|
|
'type' => $type
|
2026-11-25 20:22:06 -05:00
|
|
|
] + ($data['audit_extra'] ?? []));
|
2026-09-26 17:08:04 -04:00
|
|
|
}
|
|
|
|
|
return ['success' => true, 'removed' => $removed];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$dependencyId) {
|
|
|
|
|
return ['success' => false, 'error' => 'Dependency ID or ticket IDs required', 'http_status' => 400];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Look up dependency to verify ticket access before deletion
|
|
|
|
|
$depLookupStmt = $conn->prepare("SELECT ticket_id FROM ticket_dependencies WHERE dependency_id = ?");
|
|
|
|
|
$depLookupStmt->bind_param("i", $dependencyId);
|
|
|
|
|
$depLookupStmt->execute();
|
|
|
|
|
$depRow = $depLookupStmt->get_result()->fetch_assoc();
|
|
|
|
|
$depLookupStmt->close();
|
|
|
|
|
|
|
|
|
|
if (!$depRow) {
|
|
|
|
|
return ['success' => false, 'error' => 'Dependency not found', 'http_status' => 404];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$depTicket = $ticketModel->getTicketById($depRow['ticket_id']);
|
|
|
|
|
if (!$depTicket || !$ticketModel->canUserAccessTicket($depTicket, $currentUser)) {
|
|
|
|
|
return ['success' => false, 'error' => 'Access denied', 'http_status' => 403];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$removed = $dependencyModel->removeDependency($dependencyId);
|
|
|
|
|
if ($removed === false) {
|
|
|
|
|
return ['success' => false, 'error' => 'Failed to remove dependency', 'http_status' => 400];
|
|
|
|
|
}
|
|
|
|
|
$auditLog->log($currentUser['user_id'], 'delete', 'dependency', (string)$dependencyId);
|
|
|
|
|
return ['success' => true, 'removed' => $removed];
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-25 20:22:06 -05:00
|
|
|
/**
|
|
|
|
|
* Link two tickets, read as "ticket_id <relation> other_ticket_id". Used
|
|
|
|
|
* by MCP link_tickets and the Bearer link API, so both validate and word
|
|
|
|
|
* errors the same way. A missing or hidden ticket is "Ticket N not found".
|
|
|
|
|
*
|
|
|
|
|
* @param array $auditExtra Extra audit details, e.g. ['api_key' => 'thor']
|
|
|
|
|
* @return array success: ticket_id, relation, other_ticket_id; failure: error, http_status
|
|
|
|
|
*/
|
|
|
|
|
public static function link(mysqli $conn, array $currentUser, $ticketId, $relation, $otherId, array $auditExtra = []): array
|
|
|
|
|
{
|
|
|
|
|
$args = self::linkArgs($ticketId, $relation, $otherId);
|
|
|
|
|
if (isset($args['error'])) {
|
|
|
|
|
return $args;
|
|
|
|
|
}
|
|
|
|
|
[$ticketId, $otherId, $relation] = $args;
|
|
|
|
|
|
|
|
|
|
$result = self::add($conn, $currentUser, [
|
|
|
|
|
'ticket_id' => $ticketId,
|
|
|
|
|
'depends_on_id' => $otherId,
|
|
|
|
|
'dependency_type' => $relation,
|
|
|
|
|
'audit_extra' => $auditExtra,
|
|
|
|
|
]);
|
|
|
|
|
if (empty($result['success'])) {
|
|
|
|
|
$error = (string)($result['error'] ?? 'Request failed');
|
|
|
|
|
if ($error === 'Ticket not found') {
|
|
|
|
|
return ['success' => false, 'error' => "Ticket {$ticketId} not found", 'http_status' => 404];
|
|
|
|
|
}
|
|
|
|
|
if ($error === 'Target ticket not found') {
|
|
|
|
|
return ['success' => false, 'error' => "Ticket {$otherId} not found", 'http_status' => 404];
|
|
|
|
|
}
|
|
|
|
|
// Already linked (either side) or a circular blocking chain.
|
|
|
|
|
return ['success' => false, 'error' => $error, 'http_status' => 409];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ['success' => true, 'ticket_id' => $ticketId, 'relation' => $relation, 'other_ticket_id' => $otherId];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove a link stated as in link(). The same relationship can be stored
|
|
|
|
|
* from either ticket ("B blocked_by A" for "A blocks B"); both are tried.
|
|
|
|
|
* duplicates has no inverse type, so it is only ever stored one way.
|
|
|
|
|
*
|
|
|
|
|
* @param array $auditExtra Extra audit details, e.g. ['api_key' => 'thor']
|
|
|
|
|
* @return array success: ticket_id, relation, other_ticket_id, removed; failure: error, http_status
|
|
|
|
|
*/
|
|
|
|
|
public static function unlink(mysqli $conn, array $currentUser, $ticketId, $relation, $otherId, array $auditExtra = []): array
|
|
|
|
|
{
|
|
|
|
|
$args = self::linkArgs($ticketId, $relation, $otherId);
|
|
|
|
|
if (isset($args['error'])) {
|
|
|
|
|
return $args;
|
|
|
|
|
}
|
|
|
|
|
[$ticketId, $otherId, $relation] = $args;
|
|
|
|
|
|
|
|
|
|
$inverse = ['blocks' => 'blocked_by', 'blocked_by' => 'blocks', 'relates_to' => 'relates_to'];
|
|
|
|
|
$attempts = [[$ticketId, $otherId, $relation]];
|
|
|
|
|
if (isset($inverse[$relation])) {
|
|
|
|
|
$attempts[] = [$otherId, $ticketId, $inverse[$relation]];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$removed = 0;
|
|
|
|
|
foreach ($attempts as [$from, $to, $type]) {
|
|
|
|
|
$result = self::remove($conn, $currentUser, [
|
|
|
|
|
'ticket_id' => $from,
|
|
|
|
|
'depends_on_id' => $to,
|
|
|
|
|
'dependency_type' => $type,
|
|
|
|
|
'audit_extra' => $auditExtra,
|
|
|
|
|
]);
|
|
|
|
|
if (empty($result['success'])) {
|
|
|
|
|
$error = (string)($result['error'] ?? 'Request failed');
|
|
|
|
|
if ($error === 'Ticket not found') {
|
|
|
|
|
return ['success' => false, 'error' => "Ticket {$from} not found", 'http_status' => 404];
|
|
|
|
|
}
|
|
|
|
|
return ['success' => false, 'error' => $error, 'http_status' => $result['http_status'] ?? 400];
|
|
|
|
|
}
|
|
|
|
|
$removed += (int)$result['removed'];
|
|
|
|
|
}
|
|
|
|
|
if ($removed === 0) {
|
|
|
|
|
return ['success' => false, 'error' => "No {$relation} link between {$ticketId} and {$otherId}", 'http_status' => 404];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ['success' => true, 'ticket_id' => $ticketId, 'relation' => $relation, 'other_ticket_id' => $otherId, 'removed' => true];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Normalise and check link arguments.
|
|
|
|
|
*
|
|
|
|
|
* @return array [ticketId, otherId, relation], or an error result
|
|
|
|
|
*/
|
|
|
|
|
private static function linkArgs($ticketId, $relation, $otherId): array
|
|
|
|
|
{
|
|
|
|
|
$ticketId = trim((string)$ticketId);
|
|
|
|
|
$otherId = trim((string)$otherId);
|
|
|
|
|
$relation = strtolower(trim((string)$relation));
|
|
|
|
|
if (!in_array($relation, self::TYPES, true)) {
|
|
|
|
|
return ['success' => false, 'error' => 'relation must be one of: ' . implode(', ', self::TYPES), 'http_status' => 400];
|
|
|
|
|
}
|
|
|
|
|
foreach ([$ticketId, $otherId] as $id) {
|
|
|
|
|
if (!ctype_digit($id)) {
|
|
|
|
|
return ['success' => false, 'error' => "Ticket {$id} not found", 'http_status' => 404];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ($ticketId === $otherId) {
|
|
|
|
|
return ['success' => false, 'error' => 'A ticket cannot be linked to itself', 'http_status' => 400];
|
|
|
|
|
}
|
|
|
|
|
return [$ticketId, $otherId, $relation];
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-26 17:08:04 -04:00
|
|
|
/**
|
|
|
|
|
* users.groups is a comma-separated string; the dependency model expects an array.
|
|
|
|
|
*
|
|
|
|
|
* @return array{0:mixed,1:array,2:bool}
|
|
|
|
|
*/
|
|
|
|
|
private static function viewer(array $currentUser): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
$currentUser['user_id'] ?? null,
|
|
|
|
|
array_values(array_filter(array_map('trim', explode(',', $currentUser['groups'] ?? '')))),
|
|
|
|
|
(bool)($currentUser['is_admin'] ?? false),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|